Bad substitution when running from cron












0















I have a function (notify_dba) that with two string args, a header and a body. (correction) Works well from shell prompt, but not in cron Err, doesn't work in either shell or cron (but should...?):



 53 notify_dba "${FRIENDLY_SERVER_NAME}: New ORA errors in AlertLog" echo ${
54 "Please check full log in ${ALERT_LOG_LOCATION}.
55 =====================================================
56 `grep -A900000 "${LAST_LINE_FROM_ARCHIVE}:" ${TEMPORARY_LOG_WITH_ORA_ERRORS} | grep -v "${LAST_LINE_FROM_ARCHIVE}:"`
57 =====================================================
58 "}


Getting the error:



Wed Jan 16 06:00:01 PST 2019
LAST_LINE_FROM_ARCHIVE=449843
LAST_LINE_FROM_TEMP= 452866
/u01/app/oracle/admin/chk_alertlog.sh: line 53: ${
"Please check full log in ${ALERT_LOG_LOCATION}.
=====================================================
`grep -A900000 "${LAST_LINE_FROM_ARCHIVE}:" ${TEMPORARY_LOG_WITH_ORA_ERRORS} | grep -v "${LAST_LINE_FROM_ARCHIVE}:"`
=====================================================
"}: bad substitution









share|improve this question




















  • 1





    Ok, I was wondering how could it possibly work in a shell... But you just made your edit. The unquoted ${ triggers parameter substitution, which gives you the bad substitution error. Why the curly braces surrounding your block? They seem just wrong there.

    – fra-san
    Jan 16 at 19:43













  • lol, the shell hit a condition and this was the first time this block of code hit... I'll try without the curly braces - the thought was that being a multi-line string argument that I might have to encapsulate with squirrelies ..

    – Dennis Jorgenson
    Jan 16 at 20:05











  • Seems to have done the trick. Thanks a bunch!

    – Dennis Jorgenson
    Jan 16 at 20:48











  • Glad to hear that. Even if I'm late, I'm posting an answer with some examples on handling multi-line and non-fixed text in scripts.

    – fra-san
    Jan 16 at 21:32











  • Pleased to see you're already sorted. In general, please don't post code with line numbers. It adds work in cutting and pasting the example code.

    – roaima
    Jan 18 at 8:39
















0















I have a function (notify_dba) that with two string args, a header and a body. (correction) Works well from shell prompt, but not in cron Err, doesn't work in either shell or cron (but should...?):



 53 notify_dba "${FRIENDLY_SERVER_NAME}: New ORA errors in AlertLog" echo ${
54 "Please check full log in ${ALERT_LOG_LOCATION}.
55 =====================================================
56 `grep -A900000 "${LAST_LINE_FROM_ARCHIVE}:" ${TEMPORARY_LOG_WITH_ORA_ERRORS} | grep -v "${LAST_LINE_FROM_ARCHIVE}:"`
57 =====================================================
58 "}


Getting the error:



Wed Jan 16 06:00:01 PST 2019
LAST_LINE_FROM_ARCHIVE=449843
LAST_LINE_FROM_TEMP= 452866
/u01/app/oracle/admin/chk_alertlog.sh: line 53: ${
"Please check full log in ${ALERT_LOG_LOCATION}.
=====================================================
`grep -A900000 "${LAST_LINE_FROM_ARCHIVE}:" ${TEMPORARY_LOG_WITH_ORA_ERRORS} | grep -v "${LAST_LINE_FROM_ARCHIVE}:"`
=====================================================
"}: bad substitution









share|improve this question




















  • 1





    Ok, I was wondering how could it possibly work in a shell... But you just made your edit. The unquoted ${ triggers parameter substitution, which gives you the bad substitution error. Why the curly braces surrounding your block? They seem just wrong there.

    – fra-san
    Jan 16 at 19:43













  • lol, the shell hit a condition and this was the first time this block of code hit... I'll try without the curly braces - the thought was that being a multi-line string argument that I might have to encapsulate with squirrelies ..

    – Dennis Jorgenson
    Jan 16 at 20:05











  • Seems to have done the trick. Thanks a bunch!

    – Dennis Jorgenson
    Jan 16 at 20:48











  • Glad to hear that. Even if I'm late, I'm posting an answer with some examples on handling multi-line and non-fixed text in scripts.

    – fra-san
    Jan 16 at 21:32











  • Pleased to see you're already sorted. In general, please don't post code with line numbers. It adds work in cutting and pasting the example code.

    – roaima
    Jan 18 at 8:39














0












0








0








I have a function (notify_dba) that with two string args, a header and a body. (correction) Works well from shell prompt, but not in cron Err, doesn't work in either shell or cron (but should...?):



 53 notify_dba "${FRIENDLY_SERVER_NAME}: New ORA errors in AlertLog" echo ${
54 "Please check full log in ${ALERT_LOG_LOCATION}.
55 =====================================================
56 `grep -A900000 "${LAST_LINE_FROM_ARCHIVE}:" ${TEMPORARY_LOG_WITH_ORA_ERRORS} | grep -v "${LAST_LINE_FROM_ARCHIVE}:"`
57 =====================================================
58 "}


Getting the error:



Wed Jan 16 06:00:01 PST 2019
LAST_LINE_FROM_ARCHIVE=449843
LAST_LINE_FROM_TEMP= 452866
/u01/app/oracle/admin/chk_alertlog.sh: line 53: ${
"Please check full log in ${ALERT_LOG_LOCATION}.
=====================================================
`grep -A900000 "${LAST_LINE_FROM_ARCHIVE}:" ${TEMPORARY_LOG_WITH_ORA_ERRORS} | grep -v "${LAST_LINE_FROM_ARCHIVE}:"`
=====================================================
"}: bad substitution









share|improve this question
















I have a function (notify_dba) that with two string args, a header and a body. (correction) Works well from shell prompt, but not in cron Err, doesn't work in either shell or cron (but should...?):



 53 notify_dba "${FRIENDLY_SERVER_NAME}: New ORA errors in AlertLog" echo ${
54 "Please check full log in ${ALERT_LOG_LOCATION}.
55 =====================================================
56 `grep -A900000 "${LAST_LINE_FROM_ARCHIVE}:" ${TEMPORARY_LOG_WITH_ORA_ERRORS} | grep -v "${LAST_LINE_FROM_ARCHIVE}:"`
57 =====================================================
58 "}


Getting the error:



Wed Jan 16 06:00:01 PST 2019
LAST_LINE_FROM_ARCHIVE=449843
LAST_LINE_FROM_TEMP= 452866
/u01/app/oracle/admin/chk_alertlog.sh: line 53: ${
"Please check full log in ${ALERT_LOG_LOCATION}.
=====================================================
`grep -A900000 "${LAST_LINE_FROM_ARCHIVE}:" ${TEMPORARY_LOG_WITH_ORA_ERRORS} | grep -v "${LAST_LINE_FROM_ARCHIVE}:"`
=====================================================
"}: bad substitution






bash variable-substitution






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 16 at 19:40







Dennis Jorgenson

















asked Jan 16 at 19:07









Dennis JorgensonDennis Jorgenson

11




11








  • 1





    Ok, I was wondering how could it possibly work in a shell... But you just made your edit. The unquoted ${ triggers parameter substitution, which gives you the bad substitution error. Why the curly braces surrounding your block? They seem just wrong there.

    – fra-san
    Jan 16 at 19:43













  • lol, the shell hit a condition and this was the first time this block of code hit... I'll try without the curly braces - the thought was that being a multi-line string argument that I might have to encapsulate with squirrelies ..

    – Dennis Jorgenson
    Jan 16 at 20:05











  • Seems to have done the trick. Thanks a bunch!

    – Dennis Jorgenson
    Jan 16 at 20:48











  • Glad to hear that. Even if I'm late, I'm posting an answer with some examples on handling multi-line and non-fixed text in scripts.

    – fra-san
    Jan 16 at 21:32











  • Pleased to see you're already sorted. In general, please don't post code with line numbers. It adds work in cutting and pasting the example code.

    – roaima
    Jan 18 at 8:39














  • 1





    Ok, I was wondering how could it possibly work in a shell... But you just made your edit. The unquoted ${ triggers parameter substitution, which gives you the bad substitution error. Why the curly braces surrounding your block? They seem just wrong there.

    – fra-san
    Jan 16 at 19:43













  • lol, the shell hit a condition and this was the first time this block of code hit... I'll try without the curly braces - the thought was that being a multi-line string argument that I might have to encapsulate with squirrelies ..

    – Dennis Jorgenson
    Jan 16 at 20:05











  • Seems to have done the trick. Thanks a bunch!

    – Dennis Jorgenson
    Jan 16 at 20:48











  • Glad to hear that. Even if I'm late, I'm posting an answer with some examples on handling multi-line and non-fixed text in scripts.

    – fra-san
    Jan 16 at 21:32











  • Pleased to see you're already sorted. In general, please don't post code with line numbers. It adds work in cutting and pasting the example code.

    – roaima
    Jan 18 at 8:39








1




1





Ok, I was wondering how could it possibly work in a shell... But you just made your edit. The unquoted ${ triggers parameter substitution, which gives you the bad substitution error. Why the curly braces surrounding your block? They seem just wrong there.

– fra-san
Jan 16 at 19:43







Ok, I was wondering how could it possibly work in a shell... But you just made your edit. The unquoted ${ triggers parameter substitution, which gives you the bad substitution error. Why the curly braces surrounding your block? They seem just wrong there.

– fra-san
Jan 16 at 19:43















lol, the shell hit a condition and this was the first time this block of code hit... I'll try without the curly braces - the thought was that being a multi-line string argument that I might have to encapsulate with squirrelies ..

– Dennis Jorgenson
Jan 16 at 20:05





lol, the shell hit a condition and this was the first time this block of code hit... I'll try without the curly braces - the thought was that being a multi-line string argument that I might have to encapsulate with squirrelies ..

– Dennis Jorgenson
Jan 16 at 20:05













Seems to have done the trick. Thanks a bunch!

– Dennis Jorgenson
Jan 16 at 20:48





Seems to have done the trick. Thanks a bunch!

– Dennis Jorgenson
Jan 16 at 20:48













Glad to hear that. Even if I'm late, I'm posting an answer with some examples on handling multi-line and non-fixed text in scripts.

– fra-san
Jan 16 at 21:32





Glad to hear that. Even if I'm late, I'm posting an answer with some examples on handling multi-line and non-fixed text in scripts.

– fra-san
Jan 16 at 21:32













Pleased to see you're already sorted. In general, please don't post code with line numbers. It adds work in cutting and pasting the example code.

– roaima
Jan 18 at 8:39





Pleased to see you're already sorted. In general, please don't post code with line numbers. It adds work in cutting and pasting the example code.

– roaima
Jan 18 at 8:39










1 Answer
1






active

oldest

votes


















1
















As pointed out in a comment to your question, the error you see is due to the unquoted ${, which triggers parameter expansion (see "Parameter Expansion" in section "EXPANSION" of man bash).



That said, you can of course pass around a multi-line text block. You have just to quote it properly:



If it is static, single quotes are ok:



$ mlblock='line 1
> line 2'
$ printf '%sn' "$mlblock"
line 1
line 2


If you want it to be built at execution time, you have to use double quotes, which preserve the special meaning of $, ` and , allowing, among other things, parameter expansion and command substitution:



$ text="Sample    text"
$ mlblock="$text
> date: $(date)
> end"
$ printf '%sn' "$mlblock"
Sample text
date: Wed Jan 16 22:18:33 CET 2019
end


Note that expansions appearing anywhere in a string enclosed in double quotes are themselves quoted. Thus, their spacing is preserved (e.g. Sample text).



The most readable way to pass a complex string to a function is probably to use a variable:



$ function notify_dba () {
printf '%sn' "$1" # Print header
printf '%sn' "$2" # Print body
}
$ notify_dba 'Header' "$mlblock"
Header
Sample text
date: Wed Jan 16 22:21:14 CET 2019
end


Note that the second argument has to appear in double quotes because: 1) if unquoted, it would be expanded by the shell and, when resulting in more than one word, be translated into more than one arguments to the function; 2) if surrounded by single quotes, it would be interpreted literally as the string $mlblock.



Alternatively you can, of course, build the argument string as you call your function:



$ notify_dba 'Header' "Multi
> line body
> date: $(date)
> end"
Header
Multi
line body
date: Wed Jan 16 22:29:13 CET 2019
end





share|improve this answer

























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f494903%2fbad-substitution-when-running-from-cron%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1
















    As pointed out in a comment to your question, the error you see is due to the unquoted ${, which triggers parameter expansion (see "Parameter Expansion" in section "EXPANSION" of man bash).



    That said, you can of course pass around a multi-line text block. You have just to quote it properly:



    If it is static, single quotes are ok:



    $ mlblock='line 1
    > line 2'
    $ printf '%sn' "$mlblock"
    line 1
    line 2


    If you want it to be built at execution time, you have to use double quotes, which preserve the special meaning of $, ` and , allowing, among other things, parameter expansion and command substitution:



    $ text="Sample    text"
    $ mlblock="$text
    > date: $(date)
    > end"
    $ printf '%sn' "$mlblock"
    Sample text
    date: Wed Jan 16 22:18:33 CET 2019
    end


    Note that expansions appearing anywhere in a string enclosed in double quotes are themselves quoted. Thus, their spacing is preserved (e.g. Sample text).



    The most readable way to pass a complex string to a function is probably to use a variable:



    $ function notify_dba () {
    printf '%sn' "$1" # Print header
    printf '%sn' "$2" # Print body
    }
    $ notify_dba 'Header' "$mlblock"
    Header
    Sample text
    date: Wed Jan 16 22:21:14 CET 2019
    end


    Note that the second argument has to appear in double quotes because: 1) if unquoted, it would be expanded by the shell and, when resulting in more than one word, be translated into more than one arguments to the function; 2) if surrounded by single quotes, it would be interpreted literally as the string $mlblock.



    Alternatively you can, of course, build the argument string as you call your function:



    $ notify_dba 'Header' "Multi
    > line body
    > date: $(date)
    > end"
    Header
    Multi
    line body
    date: Wed Jan 16 22:29:13 CET 2019
    end





    share|improve this answer






























      1
















      As pointed out in a comment to your question, the error you see is due to the unquoted ${, which triggers parameter expansion (see "Parameter Expansion" in section "EXPANSION" of man bash).



      That said, you can of course pass around a multi-line text block. You have just to quote it properly:



      If it is static, single quotes are ok:



      $ mlblock='line 1
      > line 2'
      $ printf '%sn' "$mlblock"
      line 1
      line 2


      If you want it to be built at execution time, you have to use double quotes, which preserve the special meaning of $, ` and , allowing, among other things, parameter expansion and command substitution:



      $ text="Sample    text"
      $ mlblock="$text
      > date: $(date)
      > end"
      $ printf '%sn' "$mlblock"
      Sample text
      date: Wed Jan 16 22:18:33 CET 2019
      end


      Note that expansions appearing anywhere in a string enclosed in double quotes are themselves quoted. Thus, their spacing is preserved (e.g. Sample text).



      The most readable way to pass a complex string to a function is probably to use a variable:



      $ function notify_dba () {
      printf '%sn' "$1" # Print header
      printf '%sn' "$2" # Print body
      }
      $ notify_dba 'Header' "$mlblock"
      Header
      Sample text
      date: Wed Jan 16 22:21:14 CET 2019
      end


      Note that the second argument has to appear in double quotes because: 1) if unquoted, it would be expanded by the shell and, when resulting in more than one word, be translated into more than one arguments to the function; 2) if surrounded by single quotes, it would be interpreted literally as the string $mlblock.



      Alternatively you can, of course, build the argument string as you call your function:



      $ notify_dba 'Header' "Multi
      > line body
      > date: $(date)
      > end"
      Header
      Multi
      line body
      date: Wed Jan 16 22:29:13 CET 2019
      end





      share|improve this answer




























        1












        1








        1









        As pointed out in a comment to your question, the error you see is due to the unquoted ${, which triggers parameter expansion (see "Parameter Expansion" in section "EXPANSION" of man bash).



        That said, you can of course pass around a multi-line text block. You have just to quote it properly:



        If it is static, single quotes are ok:



        $ mlblock='line 1
        > line 2'
        $ printf '%sn' "$mlblock"
        line 1
        line 2


        If you want it to be built at execution time, you have to use double quotes, which preserve the special meaning of $, ` and , allowing, among other things, parameter expansion and command substitution:



        $ text="Sample    text"
        $ mlblock="$text
        > date: $(date)
        > end"
        $ printf '%sn' "$mlblock"
        Sample text
        date: Wed Jan 16 22:18:33 CET 2019
        end


        Note that expansions appearing anywhere in a string enclosed in double quotes are themselves quoted. Thus, their spacing is preserved (e.g. Sample text).



        The most readable way to pass a complex string to a function is probably to use a variable:



        $ function notify_dba () {
        printf '%sn' "$1" # Print header
        printf '%sn' "$2" # Print body
        }
        $ notify_dba 'Header' "$mlblock"
        Header
        Sample text
        date: Wed Jan 16 22:21:14 CET 2019
        end


        Note that the second argument has to appear in double quotes because: 1) if unquoted, it would be expanded by the shell and, when resulting in more than one word, be translated into more than one arguments to the function; 2) if surrounded by single quotes, it would be interpreted literally as the string $mlblock.



        Alternatively you can, of course, build the argument string as you call your function:



        $ notify_dba 'Header' "Multi
        > line body
        > date: $(date)
        > end"
        Header
        Multi
        line body
        date: Wed Jan 16 22:29:13 CET 2019
        end





        share|improve this answer

















        As pointed out in a comment to your question, the error you see is due to the unquoted ${, which triggers parameter expansion (see "Parameter Expansion" in section "EXPANSION" of man bash).



        That said, you can of course pass around a multi-line text block. You have just to quote it properly:



        If it is static, single quotes are ok:



        $ mlblock='line 1
        > line 2'
        $ printf '%sn' "$mlblock"
        line 1
        line 2


        If you want it to be built at execution time, you have to use double quotes, which preserve the special meaning of $, ` and , allowing, among other things, parameter expansion and command substitution:



        $ text="Sample    text"
        $ mlblock="$text
        > date: $(date)
        > end"
        $ printf '%sn' "$mlblock"
        Sample text
        date: Wed Jan 16 22:18:33 CET 2019
        end


        Note that expansions appearing anywhere in a string enclosed in double quotes are themselves quoted. Thus, their spacing is preserved (e.g. Sample text).



        The most readable way to pass a complex string to a function is probably to use a variable:



        $ function notify_dba () {
        printf '%sn' "$1" # Print header
        printf '%sn' "$2" # Print body
        }
        $ notify_dba 'Header' "$mlblock"
        Header
        Sample text
        date: Wed Jan 16 22:21:14 CET 2019
        end


        Note that the second argument has to appear in double quotes because: 1) if unquoted, it would be expanded by the shell and, when resulting in more than one word, be translated into more than one arguments to the function; 2) if surrounded by single quotes, it would be interpreted literally as the string $mlblock.



        Alternatively you can, of course, build the argument string as you call your function:



        $ notify_dba 'Header' "Multi
        > line body
        > date: $(date)
        > end"
        Header
        Multi
        line body
        date: Wed Jan 16 22:29:13 CET 2019
        end






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 18 at 8:30

























        answered Jan 16 at 21:30









        fra-sanfra-san

        1,3971215




        1,3971215






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f494903%2fbad-substitution-when-running-from-cron%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

            is 'sed' thread safe

            How to make a Squid Proxy server?