I have two files at the same location.
The first file is sh file and other is python file.
a.sh file
#!/bin/bash
a=1
b=1
if [[ $a -eq $b ]]
then
call the python file function named callable with command line argument as value of a
# python b.py $a
fi
b.py file
This file has many functions but I need to call specifically function callable:
arg = sys.argv[1]
def callable():
print(arg)
How can I do that in script?
add this at the bottom of your python script:
if __name__ == '__main__':
globals()[sys.argv[1]]()
then in the bash script call the function you want
python b.py callable
# or
python b.py $a
Are you simply looking for this?
if __name__ == '__main__':
callable()
By the by, you want to fix the quoting in your shell script.
#!/bin/bash
a=1
b=1
[[ "$a" -eq "$b" ]] && python b.py "$a"
See also When to wrap quotes around a shell variable .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.