how to pass echo statement output as argument to function?












-1















I just want to pass the echo statement output as argument(${1}) to the logger function in the following example:



logfile=./testlog

`touch ${logfile}`

function logger(){
echo "[${USER}] [$nowTimestamp] [INFO] ${1}" >> ${logfile}
}

echo "started executing script" | logger









share|improve this question

























  • logger "started executing script"

    – Kamaraj
    Oct 27 '16 at 5:50











  • Insider logger function replace $1 to $@

    – Kamaraj
    Oct 27 '16 at 5:52











  • my actual intention is to log the all echo statements output in a large shell script to logfile by calling this function. so in every call of this funciton can't write like (logger "started executing script")....

    – user197279
    Oct 27 '16 at 6:09
















-1















I just want to pass the echo statement output as argument(${1}) to the logger function in the following example:



logfile=./testlog

`touch ${logfile}`

function logger(){
echo "[${USER}] [$nowTimestamp] [INFO] ${1}" >> ${logfile}
}

echo "started executing script" | logger









share|improve this question

























  • logger "started executing script"

    – Kamaraj
    Oct 27 '16 at 5:50











  • Insider logger function replace $1 to $@

    – Kamaraj
    Oct 27 '16 at 5:52











  • my actual intention is to log the all echo statements output in a large shell script to logfile by calling this function. so in every call of this funciton can't write like (logger "started executing script")....

    – user197279
    Oct 27 '16 at 6:09














-1












-1








-1








I just want to pass the echo statement output as argument(${1}) to the logger function in the following example:



logfile=./testlog

`touch ${logfile}`

function logger(){
echo "[${USER}] [$nowTimestamp] [INFO] ${1}" >> ${logfile}
}

echo "started executing script" | logger









share|improve this question
















I just want to pass the echo statement output as argument(${1}) to the logger function in the following example:



logfile=./testlog

`touch ${logfile}`

function logger(){
echo "[${USER}] [$nowTimestamp] [INFO] ${1}" >> ${logfile}
}

echo "started executing script" | logger






shell-script function






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 12 '17 at 23:43









рüффп

77831529




77831529










asked Oct 27 '16 at 5:44









user197279user197279

11




11













  • logger "started executing script"

    – Kamaraj
    Oct 27 '16 at 5:50











  • Insider logger function replace $1 to $@

    – Kamaraj
    Oct 27 '16 at 5:52











  • my actual intention is to log the all echo statements output in a large shell script to logfile by calling this function. so in every call of this funciton can't write like (logger "started executing script")....

    – user197279
    Oct 27 '16 at 6:09



















  • logger "started executing script"

    – Kamaraj
    Oct 27 '16 at 5:50











  • Insider logger function replace $1 to $@

    – Kamaraj
    Oct 27 '16 at 5:52











  • my actual intention is to log the all echo statements output in a large shell script to logfile by calling this function. so in every call of this funciton can't write like (logger "started executing script")....

    – user197279
    Oct 27 '16 at 6:09

















logger "started executing script"

– Kamaraj
Oct 27 '16 at 5:50





logger "started executing script"

– Kamaraj
Oct 27 '16 at 5:50













Insider logger function replace $1 to $@

– Kamaraj
Oct 27 '16 at 5:52





Insider logger function replace $1 to $@

– Kamaraj
Oct 27 '16 at 5:52













my actual intention is to log the all echo statements output in a large shell script to logfile by calling this function. so in every call of this funciton can't write like (logger "started executing script")....

– user197279
Oct 27 '16 at 6:09





my actual intention is to log the all echo statements output in a large shell script to logfile by calling this function. so in every call of this funciton can't write like (logger "started executing script")....

– user197279
Oct 27 '16 at 6:09










2 Answers
2






active

oldest

votes


















1














Aside: logger is a standard Unix (POSIX) utility. You might choose a different name to avoid confusion.



For a program you could use | xargs -d 'n' logger. This would include the case where you rewrite your function as a script file, with 'shebang' line if necessary; however getting all the correct environment to that script could be difficult.



For a function, the only way to pass an argument is to pass an argument. You could write a second function that wraps your first function, which I'll rename logger1:



logger1(){ echo "... $1" >>$file; }
logger2(){ read -r x; logger1 "$x"; }

echo this is some log info | logger2


or you could just have your function read its input in the first place. You could even code a loop and have it handle multiple lines if provided, like the real logging facilities in most systems do.






share|improve this answer































    0














    put the echo statement inside the function.



    logfile=./testlog

    `touch ${logfile}`

    function logger()
    {
    echo "$@"
    echo "[${USER}] [$nowTimestamp] [INFO] ${1}" >> ${logfile}

    }

    logger "started executing scirpt"





    share|improve this answer
























    • thanks,its working fine... But is there anyway to automatically redirect the output of echo statement as argument of this function like i have given using "|" ? please suggest..

      – user197279
      Oct 27 '16 at 7:21













    • Try logger <(echo "started executing script)

      – Valentin B.
      Oct 27 '16 at 8:27













    • it's not working....

      – user197279
      Oct 27 '16 at 10:00











    • its is working logger echo "started executing script"

      – user197279
      Oct 27 '16 at 10:24













    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%2f319214%2fhow-to-pass-echo-statement-output-as-argument-to-function%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Aside: logger is a standard Unix (POSIX) utility. You might choose a different name to avoid confusion.



    For a program you could use | xargs -d 'n' logger. This would include the case where you rewrite your function as a script file, with 'shebang' line if necessary; however getting all the correct environment to that script could be difficult.



    For a function, the only way to pass an argument is to pass an argument. You could write a second function that wraps your first function, which I'll rename logger1:



    logger1(){ echo "... $1" >>$file; }
    logger2(){ read -r x; logger1 "$x"; }

    echo this is some log info | logger2


    or you could just have your function read its input in the first place. You could even code a loop and have it handle multiple lines if provided, like the real logging facilities in most systems do.






    share|improve this answer




























      1














      Aside: logger is a standard Unix (POSIX) utility. You might choose a different name to avoid confusion.



      For a program you could use | xargs -d 'n' logger. This would include the case where you rewrite your function as a script file, with 'shebang' line if necessary; however getting all the correct environment to that script could be difficult.



      For a function, the only way to pass an argument is to pass an argument. You could write a second function that wraps your first function, which I'll rename logger1:



      logger1(){ echo "... $1" >>$file; }
      logger2(){ read -r x; logger1 "$x"; }

      echo this is some log info | logger2


      or you could just have your function read its input in the first place. You could even code a loop and have it handle multiple lines if provided, like the real logging facilities in most systems do.






      share|improve this answer


























        1












        1








        1







        Aside: logger is a standard Unix (POSIX) utility. You might choose a different name to avoid confusion.



        For a program you could use | xargs -d 'n' logger. This would include the case where you rewrite your function as a script file, with 'shebang' line if necessary; however getting all the correct environment to that script could be difficult.



        For a function, the only way to pass an argument is to pass an argument. You could write a second function that wraps your first function, which I'll rename logger1:



        logger1(){ echo "... $1" >>$file; }
        logger2(){ read -r x; logger1 "$x"; }

        echo this is some log info | logger2


        or you could just have your function read its input in the first place. You could even code a loop and have it handle multiple lines if provided, like the real logging facilities in most systems do.






        share|improve this answer













        Aside: logger is a standard Unix (POSIX) utility. You might choose a different name to avoid confusion.



        For a program you could use | xargs -d 'n' logger. This would include the case where you rewrite your function as a script file, with 'shebang' line if necessary; however getting all the correct environment to that script could be difficult.



        For a function, the only way to pass an argument is to pass an argument. You could write a second function that wraps your first function, which I'll rename logger1:



        logger1(){ echo "... $1" >>$file; }
        logger2(){ read -r x; logger1 "$x"; }

        echo this is some log info | logger2


        or you could just have your function read its input in the first place. You could even code a loop and have it handle multiple lines if provided, like the real logging facilities in most systems do.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Oct 27 '16 at 13:32









        dave_thompson_085dave_thompson_085

        2,09211111




        2,09211111

























            0














            put the echo statement inside the function.



            logfile=./testlog

            `touch ${logfile}`

            function logger()
            {
            echo "$@"
            echo "[${USER}] [$nowTimestamp] [INFO] ${1}" >> ${logfile}

            }

            logger "started executing scirpt"





            share|improve this answer
























            • thanks,its working fine... But is there anyway to automatically redirect the output of echo statement as argument of this function like i have given using "|" ? please suggest..

              – user197279
              Oct 27 '16 at 7:21













            • Try logger <(echo "started executing script)

              – Valentin B.
              Oct 27 '16 at 8:27













            • it's not working....

              – user197279
              Oct 27 '16 at 10:00











            • its is working logger echo "started executing script"

              – user197279
              Oct 27 '16 at 10:24


















            0














            put the echo statement inside the function.



            logfile=./testlog

            `touch ${logfile}`

            function logger()
            {
            echo "$@"
            echo "[${USER}] [$nowTimestamp] [INFO] ${1}" >> ${logfile}

            }

            logger "started executing scirpt"





            share|improve this answer
























            • thanks,its working fine... But is there anyway to automatically redirect the output of echo statement as argument of this function like i have given using "|" ? please suggest..

              – user197279
              Oct 27 '16 at 7:21













            • Try logger <(echo "started executing script)

              – Valentin B.
              Oct 27 '16 at 8:27













            • it's not working....

              – user197279
              Oct 27 '16 at 10:00











            • its is working logger echo "started executing script"

              – user197279
              Oct 27 '16 at 10:24
















            0












            0








            0







            put the echo statement inside the function.



            logfile=./testlog

            `touch ${logfile}`

            function logger()
            {
            echo "$@"
            echo "[${USER}] [$nowTimestamp] [INFO] ${1}" >> ${logfile}

            }

            logger "started executing scirpt"





            share|improve this answer













            put the echo statement inside the function.



            logfile=./testlog

            `touch ${logfile}`

            function logger()
            {
            echo "$@"
            echo "[${USER}] [$nowTimestamp] [INFO] ${1}" >> ${logfile}

            }

            logger "started executing scirpt"






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Oct 27 '16 at 6:13









            KamarajKamaraj

            2,9641513




            2,9641513













            • thanks,its working fine... But is there anyway to automatically redirect the output of echo statement as argument of this function like i have given using "|" ? please suggest..

              – user197279
              Oct 27 '16 at 7:21













            • Try logger <(echo "started executing script)

              – Valentin B.
              Oct 27 '16 at 8:27













            • it's not working....

              – user197279
              Oct 27 '16 at 10:00











            • its is working logger echo "started executing script"

              – user197279
              Oct 27 '16 at 10:24





















            • thanks,its working fine... But is there anyway to automatically redirect the output of echo statement as argument of this function like i have given using "|" ? please suggest..

              – user197279
              Oct 27 '16 at 7:21













            • Try logger <(echo "started executing script)

              – Valentin B.
              Oct 27 '16 at 8:27













            • it's not working....

              – user197279
              Oct 27 '16 at 10:00











            • its is working logger echo "started executing script"

              – user197279
              Oct 27 '16 at 10:24



















            thanks,its working fine... But is there anyway to automatically redirect the output of echo statement as argument of this function like i have given using "|" ? please suggest..

            – user197279
            Oct 27 '16 at 7:21







            thanks,its working fine... But is there anyway to automatically redirect the output of echo statement as argument of this function like i have given using "|" ? please suggest..

            – user197279
            Oct 27 '16 at 7:21















            Try logger <(echo "started executing script)

            – Valentin B.
            Oct 27 '16 at 8:27







            Try logger <(echo "started executing script)

            – Valentin B.
            Oct 27 '16 at 8:27















            it's not working....

            – user197279
            Oct 27 '16 at 10:00





            it's not working....

            – user197279
            Oct 27 '16 at 10:00













            its is working logger echo "started executing script"

            – user197279
            Oct 27 '16 at 10:24







            its is working logger echo "started executing script"

            – user197279
            Oct 27 '16 at 10:24




















            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%2f319214%2fhow-to-pass-echo-statement-output-as-argument-to-function%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?