passing python variable into shell script

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











up vote
1
down vote

favorite












Here is my script:



cmd=(''' ssh $servername /usr/local/bin/pstat $1|awk 'FNR==5 print $9' %s ''' %int(var)) 
p=subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err=p.communicate()
print "err", err


Here is the error:



err awk: cmd. line:1: fatal: cannot open file (No such file or directory)



I believe I am not writing cmd properly. What am I missing?







share|improve this question





















  • What would the cmd string, tuple, whatever, be when you use it in Popen()? I'm not a Python programmer.
    – Kusalananda
    Jul 10 at 17:40










  • cmd output from shell command should be job submission time in the form of HH:MM:SS
    – Akand
    Jul 10 at 17:51










  • I asked about what actually got executed, not what the output of it would be.
    – Kusalananda
    Jul 10 at 18:15










  • When I print output ("out"), doesn't show anything.
    – Akand
    Jul 10 at 19:11










  • Sure, but what is actually the value of cmd?
    – Kusalananda
    Jul 10 at 19:11














up vote
1
down vote

favorite












Here is my script:



cmd=(''' ssh $servername /usr/local/bin/pstat $1|awk 'FNR==5 print $9' %s ''' %int(var)) 
p=subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err=p.communicate()
print "err", err


Here is the error:



err awk: cmd. line:1: fatal: cannot open file (No such file or directory)



I believe I am not writing cmd properly. What am I missing?







share|improve this question





















  • What would the cmd string, tuple, whatever, be when you use it in Popen()? I'm not a Python programmer.
    – Kusalananda
    Jul 10 at 17:40










  • cmd output from shell command should be job submission time in the form of HH:MM:SS
    – Akand
    Jul 10 at 17:51










  • I asked about what actually got executed, not what the output of it would be.
    – Kusalananda
    Jul 10 at 18:15










  • When I print output ("out"), doesn't show anything.
    – Akand
    Jul 10 at 19:11










  • Sure, but what is actually the value of cmd?
    – Kusalananda
    Jul 10 at 19:11












up vote
1
down vote

favorite









up vote
1
down vote

favorite











Here is my script:



cmd=(''' ssh $servername /usr/local/bin/pstat $1|awk 'FNR==5 print $9' %s ''' %int(var)) 
p=subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err=p.communicate()
print "err", err


Here is the error:



err awk: cmd. line:1: fatal: cannot open file (No such file or directory)



I believe I am not writing cmd properly. What am I missing?







share|improve this question













Here is my script:



cmd=(''' ssh $servername /usr/local/bin/pstat $1|awk 'FNR==5 print $9' %s ''' %int(var)) 
p=subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err=p.communicate()
print "err", err


Here is the error:



err awk: cmd. line:1: fatal: cannot open file (No such file or directory)



I believe I am not writing cmd properly. What am I missing?









share|improve this question












share|improve this question




share|improve this question








edited Jul 10 at 17:27









SivaPrasath

3,68311636




3,68311636









asked Jul 10 at 17:20









Akand

426




426











  • What would the cmd string, tuple, whatever, be when you use it in Popen()? I'm not a Python programmer.
    – Kusalananda
    Jul 10 at 17:40










  • cmd output from shell command should be job submission time in the form of HH:MM:SS
    – Akand
    Jul 10 at 17:51










  • I asked about what actually got executed, not what the output of it would be.
    – Kusalananda
    Jul 10 at 18:15










  • When I print output ("out"), doesn't show anything.
    – Akand
    Jul 10 at 19:11










  • Sure, but what is actually the value of cmd?
    – Kusalananda
    Jul 10 at 19:11
















  • What would the cmd string, tuple, whatever, be when you use it in Popen()? I'm not a Python programmer.
    – Kusalananda
    Jul 10 at 17:40










  • cmd output from shell command should be job submission time in the form of HH:MM:SS
    – Akand
    Jul 10 at 17:51










  • I asked about what actually got executed, not what the output of it would be.
    – Kusalananda
    Jul 10 at 18:15










  • When I print output ("out"), doesn't show anything.
    – Akand
    Jul 10 at 19:11










  • Sure, but what is actually the value of cmd?
    – Kusalananda
    Jul 10 at 19:11















What would the cmd string, tuple, whatever, be when you use it in Popen()? I'm not a Python programmer.
– Kusalananda
Jul 10 at 17:40




What would the cmd string, tuple, whatever, be when you use it in Popen()? I'm not a Python programmer.
– Kusalananda
Jul 10 at 17:40












cmd output from shell command should be job submission time in the form of HH:MM:SS
– Akand
Jul 10 at 17:51




cmd output from shell command should be job submission time in the form of HH:MM:SS
– Akand
Jul 10 at 17:51












I asked about what actually got executed, not what the output of it would be.
– Kusalananda
Jul 10 at 18:15




I asked about what actually got executed, not what the output of it would be.
– Kusalananda
Jul 10 at 18:15












When I print output ("out"), doesn't show anything.
– Akand
Jul 10 at 19:11




When I print output ("out"), doesn't show anything.
– Akand
Jul 10 at 19:11












Sure, but what is actually the value of cmd?
– Kusalananda
Jul 10 at 19:11




Sure, but what is actually the value of cmd?
– Kusalananda
Jul 10 at 19:11










1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










The command you have the shell run is something like this, with 1234 being a value from var (this is passed as argument to sh -c):



ssh $servername /usr/local/bin/pstat $1|awk 'FNR==5 print $9' 1234


There, $1 is expanded into the first argument of the shell, but it doesn't have have any, so that disappears. 1234 is an argument to awk, which takes it as the name of an input file, tries to open it and fails.



I'm assuming you want the value from var as an argument to pstat instead. You have two options: either place the number where you have $1 now, with %s as you did above; or use $1, and pass var as an argument to the shell that Popen runs.



With %s:



cmd=(''' ssh "$servername" /usr/local/bin/pstat '%s' | awk 'FNR==5 print $9' '''
% int(var))
p=subprocess.Popen(cmd, shell=True, ... )


Note that this drops the variable value in the middle of the shell command, so you do need to know it doesn't contain anything dangerous. With single-quotes around it, only single-quotes will cause issues. Since you only have a number, this should be safe.



With $1:



cmd=(''' ssh "$servername" /usr/local/bin/pstat "$1" | awk 'FNR==5 print $9' ''') 
p=subprocess.Popen([cmd, "sh", str(var)], shell=True, ... )


Here, we pass an array as the first argument of Popen, which makes it use the first array element as the command line, and the rest as arguments to the shell. (i.e. it runs sh with the arguments -c, cmdline..., sh and 1234. The sh string goes to the shell's $0.)



This is safer than the previous in that variable value is never mixed with the code the shell runs (but note that you need the double quotes around "$1" for the usual reasons).



Note that you need to have servername set in the environment for that variable to be expanded by the shell Popen runs.






share|improve this answer





















  • Thanks! They work fine. One clarification, when I execute the command in cmd from a separate "cmd.sh" file and then calling as commands.getoutput(" ./cmd %s" %(var)), works fine. The "cmd.sh" file contains ssh $servername /usr/local/bin/pstat $1 | awk 'FNR==5 print $9'. Why in this case the awk doesn't show problem?
    – Akand
    Jul 11 at 13:53










  • @Akand, You're passing one argument to the script ./cmd.sh, it then shows up as $1 within the script. In the script, you also have only one argument to awk (the string in single quotes), not two as in your question. The arguments to the script and the arguments to a command within the script are different.
    – ilkkachu
    Jul 11 at 18:51










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%2f454534%2fpassing-python-variable-into-shell-script%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
0
down vote



accepted










The command you have the shell run is something like this, with 1234 being a value from var (this is passed as argument to sh -c):



ssh $servername /usr/local/bin/pstat $1|awk 'FNR==5 print $9' 1234


There, $1 is expanded into the first argument of the shell, but it doesn't have have any, so that disappears. 1234 is an argument to awk, which takes it as the name of an input file, tries to open it and fails.



I'm assuming you want the value from var as an argument to pstat instead. You have two options: either place the number where you have $1 now, with %s as you did above; or use $1, and pass var as an argument to the shell that Popen runs.



With %s:



cmd=(''' ssh "$servername" /usr/local/bin/pstat '%s' | awk 'FNR==5 print $9' '''
% int(var))
p=subprocess.Popen(cmd, shell=True, ... )


Note that this drops the variable value in the middle of the shell command, so you do need to know it doesn't contain anything dangerous. With single-quotes around it, only single-quotes will cause issues. Since you only have a number, this should be safe.



With $1:



cmd=(''' ssh "$servername" /usr/local/bin/pstat "$1" | awk 'FNR==5 print $9' ''') 
p=subprocess.Popen([cmd, "sh", str(var)], shell=True, ... )


Here, we pass an array as the first argument of Popen, which makes it use the first array element as the command line, and the rest as arguments to the shell. (i.e. it runs sh with the arguments -c, cmdline..., sh and 1234. The sh string goes to the shell's $0.)



This is safer than the previous in that variable value is never mixed with the code the shell runs (but note that you need the double quotes around "$1" for the usual reasons).



Note that you need to have servername set in the environment for that variable to be expanded by the shell Popen runs.






share|improve this answer





















  • Thanks! They work fine. One clarification, when I execute the command in cmd from a separate "cmd.sh" file and then calling as commands.getoutput(" ./cmd %s" %(var)), works fine. The "cmd.sh" file contains ssh $servername /usr/local/bin/pstat $1 | awk 'FNR==5 print $9'. Why in this case the awk doesn't show problem?
    – Akand
    Jul 11 at 13:53










  • @Akand, You're passing one argument to the script ./cmd.sh, it then shows up as $1 within the script. In the script, you also have only one argument to awk (the string in single quotes), not two as in your question. The arguments to the script and the arguments to a command within the script are different.
    – ilkkachu
    Jul 11 at 18:51














up vote
0
down vote



accepted










The command you have the shell run is something like this, with 1234 being a value from var (this is passed as argument to sh -c):



ssh $servername /usr/local/bin/pstat $1|awk 'FNR==5 print $9' 1234


There, $1 is expanded into the first argument of the shell, but it doesn't have have any, so that disappears. 1234 is an argument to awk, which takes it as the name of an input file, tries to open it and fails.



I'm assuming you want the value from var as an argument to pstat instead. You have two options: either place the number where you have $1 now, with %s as you did above; or use $1, and pass var as an argument to the shell that Popen runs.



With %s:



cmd=(''' ssh "$servername" /usr/local/bin/pstat '%s' | awk 'FNR==5 print $9' '''
% int(var))
p=subprocess.Popen(cmd, shell=True, ... )


Note that this drops the variable value in the middle of the shell command, so you do need to know it doesn't contain anything dangerous. With single-quotes around it, only single-quotes will cause issues. Since you only have a number, this should be safe.



With $1:



cmd=(''' ssh "$servername" /usr/local/bin/pstat "$1" | awk 'FNR==5 print $9' ''') 
p=subprocess.Popen([cmd, "sh", str(var)], shell=True, ... )


Here, we pass an array as the first argument of Popen, which makes it use the first array element as the command line, and the rest as arguments to the shell. (i.e. it runs sh with the arguments -c, cmdline..., sh and 1234. The sh string goes to the shell's $0.)



This is safer than the previous in that variable value is never mixed with the code the shell runs (but note that you need the double quotes around "$1" for the usual reasons).



Note that you need to have servername set in the environment for that variable to be expanded by the shell Popen runs.






share|improve this answer





















  • Thanks! They work fine. One clarification, when I execute the command in cmd from a separate "cmd.sh" file and then calling as commands.getoutput(" ./cmd %s" %(var)), works fine. The "cmd.sh" file contains ssh $servername /usr/local/bin/pstat $1 | awk 'FNR==5 print $9'. Why in this case the awk doesn't show problem?
    – Akand
    Jul 11 at 13:53










  • @Akand, You're passing one argument to the script ./cmd.sh, it then shows up as $1 within the script. In the script, you also have only one argument to awk (the string in single quotes), not two as in your question. The arguments to the script and the arguments to a command within the script are different.
    – ilkkachu
    Jul 11 at 18:51












up vote
0
down vote



accepted







up vote
0
down vote



accepted






The command you have the shell run is something like this, with 1234 being a value from var (this is passed as argument to sh -c):



ssh $servername /usr/local/bin/pstat $1|awk 'FNR==5 print $9' 1234


There, $1 is expanded into the first argument of the shell, but it doesn't have have any, so that disappears. 1234 is an argument to awk, which takes it as the name of an input file, tries to open it and fails.



I'm assuming you want the value from var as an argument to pstat instead. You have two options: either place the number where you have $1 now, with %s as you did above; or use $1, and pass var as an argument to the shell that Popen runs.



With %s:



cmd=(''' ssh "$servername" /usr/local/bin/pstat '%s' | awk 'FNR==5 print $9' '''
% int(var))
p=subprocess.Popen(cmd, shell=True, ... )


Note that this drops the variable value in the middle of the shell command, so you do need to know it doesn't contain anything dangerous. With single-quotes around it, only single-quotes will cause issues. Since you only have a number, this should be safe.



With $1:



cmd=(''' ssh "$servername" /usr/local/bin/pstat "$1" | awk 'FNR==5 print $9' ''') 
p=subprocess.Popen([cmd, "sh", str(var)], shell=True, ... )


Here, we pass an array as the first argument of Popen, which makes it use the first array element as the command line, and the rest as arguments to the shell. (i.e. it runs sh with the arguments -c, cmdline..., sh and 1234. The sh string goes to the shell's $0.)



This is safer than the previous in that variable value is never mixed with the code the shell runs (but note that you need the double quotes around "$1" for the usual reasons).



Note that you need to have servername set in the environment for that variable to be expanded by the shell Popen runs.






share|improve this answer













The command you have the shell run is something like this, with 1234 being a value from var (this is passed as argument to sh -c):



ssh $servername /usr/local/bin/pstat $1|awk 'FNR==5 print $9' 1234


There, $1 is expanded into the first argument of the shell, but it doesn't have have any, so that disappears. 1234 is an argument to awk, which takes it as the name of an input file, tries to open it and fails.



I'm assuming you want the value from var as an argument to pstat instead. You have two options: either place the number where you have $1 now, with %s as you did above; or use $1, and pass var as an argument to the shell that Popen runs.



With %s:



cmd=(''' ssh "$servername" /usr/local/bin/pstat '%s' | awk 'FNR==5 print $9' '''
% int(var))
p=subprocess.Popen(cmd, shell=True, ... )


Note that this drops the variable value in the middle of the shell command, so you do need to know it doesn't contain anything dangerous. With single-quotes around it, only single-quotes will cause issues. Since you only have a number, this should be safe.



With $1:



cmd=(''' ssh "$servername" /usr/local/bin/pstat "$1" | awk 'FNR==5 print $9' ''') 
p=subprocess.Popen([cmd, "sh", str(var)], shell=True, ... )


Here, we pass an array as the first argument of Popen, which makes it use the first array element as the command line, and the rest as arguments to the shell. (i.e. it runs sh with the arguments -c, cmdline..., sh and 1234. The sh string goes to the shell's $0.)



This is safer than the previous in that variable value is never mixed with the code the shell runs (but note that you need the double quotes around "$1" for the usual reasons).



Note that you need to have servername set in the environment for that variable to be expanded by the shell Popen runs.







share|improve this answer













share|improve this answer



share|improve this answer











answered Jul 10 at 20:35









ilkkachu

47.3k668130




47.3k668130











  • Thanks! They work fine. One clarification, when I execute the command in cmd from a separate "cmd.sh" file and then calling as commands.getoutput(" ./cmd %s" %(var)), works fine. The "cmd.sh" file contains ssh $servername /usr/local/bin/pstat $1 | awk 'FNR==5 print $9'. Why in this case the awk doesn't show problem?
    – Akand
    Jul 11 at 13:53










  • @Akand, You're passing one argument to the script ./cmd.sh, it then shows up as $1 within the script. In the script, you also have only one argument to awk (the string in single quotes), not two as in your question. The arguments to the script and the arguments to a command within the script are different.
    – ilkkachu
    Jul 11 at 18:51
















  • Thanks! They work fine. One clarification, when I execute the command in cmd from a separate "cmd.sh" file and then calling as commands.getoutput(" ./cmd %s" %(var)), works fine. The "cmd.sh" file contains ssh $servername /usr/local/bin/pstat $1 | awk 'FNR==5 print $9'. Why in this case the awk doesn't show problem?
    – Akand
    Jul 11 at 13:53










  • @Akand, You're passing one argument to the script ./cmd.sh, it then shows up as $1 within the script. In the script, you also have only one argument to awk (the string in single quotes), not two as in your question. The arguments to the script and the arguments to a command within the script are different.
    – ilkkachu
    Jul 11 at 18:51















Thanks! They work fine. One clarification, when I execute the command in cmd from a separate "cmd.sh" file and then calling as commands.getoutput(" ./cmd %s" %(var)), works fine. The "cmd.sh" file contains ssh $servername /usr/local/bin/pstat $1 | awk 'FNR==5 print $9'. Why in this case the awk doesn't show problem?
– Akand
Jul 11 at 13:53




Thanks! They work fine. One clarification, when I execute the command in cmd from a separate "cmd.sh" file and then calling as commands.getoutput(" ./cmd %s" %(var)), works fine. The "cmd.sh" file contains ssh $servername /usr/local/bin/pstat $1 | awk 'FNR==5 print $9'. Why in this case the awk doesn't show problem?
– Akand
Jul 11 at 13:53












@Akand, You're passing one argument to the script ./cmd.sh, it then shows up as $1 within the script. In the script, you also have only one argument to awk (the string in single quotes), not two as in your question. The arguments to the script and the arguments to a command within the script are different.
– ilkkachu
Jul 11 at 18:51




@Akand, You're passing one argument to the script ./cmd.sh, it then shows up as $1 within the script. In the script, you also have only one argument to awk (the string in single quotes), not two as in your question. The arguments to the script and the arguments to a command within the script are different.
– ilkkachu
Jul 11 at 18:51












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f454534%2fpassing-python-variable-into-shell-script%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?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?