0

HI guys i am new to scripting, i am doing small project in python and wxpython, which does automation. Now i have a problem while sending variable from python to shell script. I have to connect ftp ,then download a file which is written in BASH(1st script). That bash script call one more bash script(2nd script) which is also in ftp server. BUT the 2 script name is not hard-coded in 1st script. so i have to send that "script name" from my python script which is i am not able to do right now. so i need yours help to solve my issue, have gone through google but couldn't find anything.

Thanks in advance.

I have tried directly to run this but still it don't take 2nd argument,so i have gone through the script once. found that,

when $2 $3 $7 already configured inside the 1st script (those are in built in myproject.sh)

please help me to solve this issue..

biz
  • 11
  • 1
  • 1
  • 6
  • how are you running your bash script from python? Are you using system()? – Memento Mori Mar 14 '13 at 06:28
  • No.I am using subprocess – biz Mar 14 '13 at 06:32
  • The answer so far is very good. But you could also just add a command line argument to your first script. Then the name of the 2nd bash script would just be $1 in your bash script (assuming you don't already have any other command line arguments) – Memento Mori Mar 14 '13 at 06:33
  • Possible duplicate of [How to pass variables from python script to bash script](https://stackoverflow.com/q/4257098/608639) – jww Sep 23 '18 at 00:34
  • Not sure why the question is downvoted, I needed one of the answer here – Arefe Apr 06 '19 at 06:06

2 Answers2

1

The quickest and easiest way is to let your Python script create a file that sets a variable name using bash syntax, and then let your first bash script source it. Sourcing it will basically set that variable.

So in Python, you want to output that file somewhere:

name_of_script="/path/to/second/script.sh"

with open("/my/path/scriptnamevar.sh", "w") as f:
    #Output to bash-recognized variable setting syntax
    f.write("SCRIPT_NAME={}".format(name_of_script))

Then in your first script, you just need to source scriptnamevar.sh (or whatever you decide to call it).

source /my/path/scriptnamevar.sh

# This should output whatever name_of_script was in python
echo $SCRIPT_NAME 
Jordan
  • 31,971
  • 6
  • 56
  • 67
  • @ jordan I did not understand what you said ,can u please explain or have any useful links? – biz Mar 14 '13 at 06:33
  • @ jordan @nd script is user should enter so i used TextCtrl in GUI so it take value from GUI. – biz Mar 14 '13 at 06:35
0

You could make use of command line arguments. Since you're using subprocess to call the first bash script, you could modify it to look like this:

subprocess.call(["bashScript1Name", "bashScript2Name"])

Then in bashScript1Name the name of the second script will be in the variable $1

Memento Mori
  • 3,327
  • 2
  • 22
  • 29
  • bashScript2Name user should enter i cant hard-code it for security purpose :( – biz Mar 14 '13 at 06:41
  • Well in that example the user could still input it in python, then you could run that command with the variable holding the script name. Unfortunately though, my example still wouldn't work if security is what you're looking for. The name of the 2nd bash script will be visible while the 1st script is running using ps (or other methods after it has finished to a user who knows what they're doing and can be root). As far as I know, there is no easy way to get hidden information to a bash script. – Memento Mori Mar 14 '13 at 06:47
  • @ Memento Mori can we make bash script to take arguments from python? – biz Mar 14 '13 at 06:50
  • yeah, the example in my answer should do it. It's running the first bash script with the command line argument: name of the second bash script – Memento Mori Mar 14 '13 at 06:51
  • NO ,i am not able to call second script. – biz Mar 14 '13 at 07:02
  • i got thi9s error when i executed my python script: subprocess.call(["echo xxxxxxx | sudo -S ./file1sh -n ", "fname"])............. File "/usr/lib/python2.7/subprocess.py", line 493, in call return Popen(*popenargs, **kwargs).wait() ................. File "/usr/lib/python2.7/subprocess.py", line 679, in __init__ errread, errwrite)........... File "/usr/lib/python2.7/subprocess.py", line 1239, in _execute_child.............. raise child_exception.......... OSError: [Errno 2] No such file or directory – biz Mar 14 '13 at 07:10
  • @biz hmm, I'm sorry I don't really know why. Do you normally have that '-n' there when you're calling the shell script? I don't know exactly what you've done, but in the shell script the command line argument representing your filename in your example should be $2 because of that '-n'. That would also make me think that the shell script might already be expecting arguments? So you'd have to make sure the rest of the code isn't doing anything to the new argument you're sending to it. – Memento Mori Mar 14 '13 at 07:19
  • @ Memento Mori: ya in first script it ask for option "-n or -h", so i am sending it.(just for the trial i always include -n)...In the shell script (frst script), second script was called like this:"filename=' ' ", i have changed it to "filename = "$2" now. – biz Mar 14 '13 at 07:24
  • @Momento Mori: how to pass if $2, $3, $7 are already used in built in script? – biz Mar 15 '13 at 06:12