[英]How to write a bash script that runs a python program that restarts with parameters
I've written a bash script based on this and this . 我已经基于this和this编写了一个bash脚本。 The script is: 脚本是:
#!/bin/bash
until python data.py $1 $2 ; do
echo "'data.py' crashed with exit code $?. Restarting..." >> errors.txt
sleep 1
done
I run it: 我运行它:
./run.sh ‘a’ ‘n’
but in data.py 但在data.py中
if sys.argv[1] == 'a':
returns false. 返回false。 How do I pass $1 and $2 to my Python program? 如何将$ 1和$ 2传递给我的Python程序? TIA!!! TIA !!!
EDIT: SOLUTION 编辑:解决方案
Thanks to Jandom and andlrc: I call it: 感谢Jandom和andlrc:我称之为:
./run.sh "a" "n"
and change the script to: 并将脚本更改为:
until python data.py "$@" ; do
script.sh (make sure it allow execute) script.sh(确保它允许执行)
#!/bin/sh
python script.py "$@"
script.py (first param is ./script.sh if run as ./script.sh from terminal), so, script.py: script.py(如果从终端以./script.sh运行,则第一个参数是./script.sh),因此script.py:
import sys
print(sys.argv[1:])
# sys.argv[1] == a, sys.argv[2] == b, etc..
You are sending special quotes to your shell script: 您正在向shell脚本发送特殊引号:
% charinfo ‘’
U+2018 SINGLE TURNED COMMA QUOTATION MARK [Pi]
U+2019 SINGLE COMMA QUOTATION MARK [Pf]
Run it with single or double quotes: 用单引号或双引号运行它:
./run.sh "a" "b"
Also remember quotes in your bash script or $1
and $2
will undergo word splitting: 还要记住bash脚本中的引号,否则$1
和$2
将进行分词:
#!/bin/bash
until python data.py "$1" "$2"; do
echo "'data.py' crashed with exit code $?. Restarting..." >> errors.txt
sleep 1
done
As pointed out before you can use "$@"
which will refer to all arguments send to your bash script. 如前所述,可以使用"$@"
来引用发送到bash脚本的所有参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.