reading output into variables inside the os.system command

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
-1
down vote

favorite












I have a python script which runs in Linux. I need to capture a command's output and store into variables and then should print the output. Code below -



#!/usr/bin/python
import os, time
systime=os.popen('date +"%m-%d-%y-%T"').read()
os.system("read c1 c2 c3 c4 c5 c6 < <(sar -u 1 1 | awk 'NR==4, NR==4 print $4, $5, $6, $7, $8, $9')")
os.system("echo $systime,$c1,$c2,$c3,$c4,$c5,$c6 >> outputfile.txt")


I'm collecting the output given by the command sar -u 1 1 | awk 'NR==4, NR==4 print $4, $5, $6, $7, $8, $9') into 6 variables - c1, c2, c3, c4, c5 c6 using the read command. When i try to execute the above code, I get the below error -



sh: -c: line 0: syntax error near unexpected token `<'


I even tried using os.popen instead of os.system but still end up getting the same error. Suggest me on how to store the variables using os.system command how to use them in the later stages. My target here is to print all the variables including the time being captured into a output file outputfile.txt. TIA







share|improve this question




















  • process substitution is a bash feature, so you'll need to tell python to use bash.
    – muru
    Feb 8 at 6:57














up vote
-1
down vote

favorite












I have a python script which runs in Linux. I need to capture a command's output and store into variables and then should print the output. Code below -



#!/usr/bin/python
import os, time
systime=os.popen('date +"%m-%d-%y-%T"').read()
os.system("read c1 c2 c3 c4 c5 c6 < <(sar -u 1 1 | awk 'NR==4, NR==4 print $4, $5, $6, $7, $8, $9')")
os.system("echo $systime,$c1,$c2,$c3,$c4,$c5,$c6 >> outputfile.txt")


I'm collecting the output given by the command sar -u 1 1 | awk 'NR==4, NR==4 print $4, $5, $6, $7, $8, $9') into 6 variables - c1, c2, c3, c4, c5 c6 using the read command. When i try to execute the above code, I get the below error -



sh: -c: line 0: syntax error near unexpected token `<'


I even tried using os.popen instead of os.system but still end up getting the same error. Suggest me on how to store the variables using os.system command how to use them in the later stages. My target here is to print all the variables including the time being captured into a output file outputfile.txt. TIA







share|improve this question




















  • process substitution is a bash feature, so you'll need to tell python to use bash.
    – muru
    Feb 8 at 6:57












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I have a python script which runs in Linux. I need to capture a command's output and store into variables and then should print the output. Code below -



#!/usr/bin/python
import os, time
systime=os.popen('date +"%m-%d-%y-%T"').read()
os.system("read c1 c2 c3 c4 c5 c6 < <(sar -u 1 1 | awk 'NR==4, NR==4 print $4, $5, $6, $7, $8, $9')")
os.system("echo $systime,$c1,$c2,$c3,$c4,$c5,$c6 >> outputfile.txt")


I'm collecting the output given by the command sar -u 1 1 | awk 'NR==4, NR==4 print $4, $5, $6, $7, $8, $9') into 6 variables - c1, c2, c3, c4, c5 c6 using the read command. When i try to execute the above code, I get the below error -



sh: -c: line 0: syntax error near unexpected token `<'


I even tried using os.popen instead of os.system but still end up getting the same error. Suggest me on how to store the variables using os.system command how to use them in the later stages. My target here is to print all the variables including the time being captured into a output file outputfile.txt. TIA







share|improve this question












I have a python script which runs in Linux. I need to capture a command's output and store into variables and then should print the output. Code below -



#!/usr/bin/python
import os, time
systime=os.popen('date +"%m-%d-%y-%T"').read()
os.system("read c1 c2 c3 c4 c5 c6 < <(sar -u 1 1 | awk 'NR==4, NR==4 print $4, $5, $6, $7, $8, $9')")
os.system("echo $systime,$c1,$c2,$c3,$c4,$c5,$c6 >> outputfile.txt")


I'm collecting the output given by the command sar -u 1 1 | awk 'NR==4, NR==4 print $4, $5, $6, $7, $8, $9') into 6 variables - c1, c2, c3, c4, c5 c6 using the read command. When i try to execute the above code, I get the below error -



sh: -c: line 0: syntax error near unexpected token `<'


I even tried using os.popen instead of os.system but still end up getting the same error. Suggest me on how to store the variables using os.system command how to use them in the later stages. My target here is to print all the variables including the time being captured into a output file outputfile.txt. TIA









share|improve this question











share|improve this question




share|improve this question










asked Feb 8 at 6:54









Dev

1




1











  • process substitution is a bash feature, so you'll need to tell python to use bash.
    – muru
    Feb 8 at 6:57
















  • process substitution is a bash feature, so you'll need to tell python to use bash.
    – muru
    Feb 8 at 6:57















process substitution is a bash feature, so you'll need to tell python to use bash.
– muru
Feb 8 at 6:57




process substitution is a bash feature, so you'll need to tell python to use bash.
– muru
Feb 8 at 6:57










1 Answer
1






active

oldest

votes

















up vote
2
down vote













<(...) is ksh syntax also recognised by zsh and bash, though when used as the target of a redirection, it's only supported by zsh and bash.



In any case, that is not sh syntax. Python's os.system() and os.popen() do run sh to interpret the given command line. And each invocation of those commands runs a new shell, so one variable defined in one will not be available in a the next shell invocation. Also python variables do not automagically become shell variables.



Here, you could do:



os.system("""
sar -u 1 1 |
awk -v t="$(date +%m-%d-%y-%T)" -v OFS=, '
NR==4
print t, $4, $5, $6, $7, $8, $9
' > outputfile.txt""")


Though it feels silly to invoke date and awk (or even a shell) from within python while python is very well capable of doing their jobs itsef.






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',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    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%2f422716%2freading-output-into-variables-inside-the-os-system-command%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    <(...) is ksh syntax also recognised by zsh and bash, though when used as the target of a redirection, it's only supported by zsh and bash.



    In any case, that is not sh syntax. Python's os.system() and os.popen() do run sh to interpret the given command line. And each invocation of those commands runs a new shell, so one variable defined in one will not be available in a the next shell invocation. Also python variables do not automagically become shell variables.



    Here, you could do:



    os.system("""
    sar -u 1 1 |
    awk -v t="$(date +%m-%d-%y-%T)" -v OFS=, '
    NR==4
    print t, $4, $5, $6, $7, $8, $9
    ' > outputfile.txt""")


    Though it feels silly to invoke date and awk (or even a shell) from within python while python is very well capable of doing their jobs itsef.






    share|improve this answer


























      up vote
      2
      down vote













      <(...) is ksh syntax also recognised by zsh and bash, though when used as the target of a redirection, it's only supported by zsh and bash.



      In any case, that is not sh syntax. Python's os.system() and os.popen() do run sh to interpret the given command line. And each invocation of those commands runs a new shell, so one variable defined in one will not be available in a the next shell invocation. Also python variables do not automagically become shell variables.



      Here, you could do:



      os.system("""
      sar -u 1 1 |
      awk -v t="$(date +%m-%d-%y-%T)" -v OFS=, '
      NR==4
      print t, $4, $5, $6, $7, $8, $9
      ' > outputfile.txt""")


      Though it feels silly to invoke date and awk (or even a shell) from within python while python is very well capable of doing their jobs itsef.






      share|improve this answer
























        up vote
        2
        down vote










        up vote
        2
        down vote









        <(...) is ksh syntax also recognised by zsh and bash, though when used as the target of a redirection, it's only supported by zsh and bash.



        In any case, that is not sh syntax. Python's os.system() and os.popen() do run sh to interpret the given command line. And each invocation of those commands runs a new shell, so one variable defined in one will not be available in a the next shell invocation. Also python variables do not automagically become shell variables.



        Here, you could do:



        os.system("""
        sar -u 1 1 |
        awk -v t="$(date +%m-%d-%y-%T)" -v OFS=, '
        NR==4
        print t, $4, $5, $6, $7, $8, $9
        ' > outputfile.txt""")


        Though it feels silly to invoke date and awk (or even a shell) from within python while python is very well capable of doing their jobs itsef.






        share|improve this answer














        <(...) is ksh syntax also recognised by zsh and bash, though when used as the target of a redirection, it's only supported by zsh and bash.



        In any case, that is not sh syntax. Python's os.system() and os.popen() do run sh to interpret the given command line. And each invocation of those commands runs a new shell, so one variable defined in one will not be available in a the next shell invocation. Also python variables do not automagically become shell variables.



        Here, you could do:



        os.system("""
        sar -u 1 1 |
        awk -v t="$(date +%m-%d-%y-%T)" -v OFS=, '
        NR==4
        print t, $4, $5, $6, $7, $8, $9
        ' > outputfile.txt""")


        Though it feels silly to invoke date and awk (or even a shell) from within python while python is very well capable of doing their jobs itsef.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Feb 8 at 8:08

























        answered Feb 8 at 7:34









        Stéphane Chazelas

        281k53516847




        281k53516847






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f422716%2freading-output-into-variables-inside-the-os-system-command%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay