Shell脚本将参数传递给另一个Shell脚本



我正在编写一个shell脚本,该脚本调用另一个调用python脚本的shell脚本,并在整个链中传递一个参数,如下所示:

目录结构:

first
scripts
|- second
|- third

第一个

#!/usr/bin/env bash
date=$1
p=$2
(cd ./scripts/ && ./second "$p")

第二

#!/usr/bin/env bash
function myFunction() {
./third $1
}
myFunction

第三

#!/usr/bin/env python
import sys
p = sys.argv[1]
print("I got " + p)

我知道这是一团糟,但我现在需要这样做。

当我尝试./first 20180716 0.5

我预计这段代码会打印出"我得到了0.5",但python脚本似乎抱怨道:

Traceback (most recent call last):
File "./third", line 4, in <module>
p = sys.argv[1]
IndexError: list index out of range

这意味着似乎什么都没有发生/第三有什么想法吗?

作为一个尝试的复制者(一旦用MCVE编辑问题,它就会被删除(:

# create a temporary directory
tempdir=$(mktemp -d "${TMPDIR:-/tmp}/repro.XXXXXX") || exit
# create our "first" script
cat >"$tempdir/first" <<'EOF'
#!/usr/bin/env bash
date=$1
p=$2
(cd ./scripts/ && ./second "$p")
EOF
# create a "scripts" directory    
mkdir "$tempdir/scripts" || exit
# create our "second" script
cat >"$tempdir/scripts/second" <<'EOF'
#!/usr/bin/env bash
./third $1
EOF
# create our "third" script
cat >"$tempdir/scripts/third" <<'EOF'
#!/usr/bin/env python
import sys
p = sys.argv[1]
print("I got " + p)
EOF
chmod +x "$tempdir"/{first,scripts/{second,third}}  # make the scripts all executable...
cd "$tempdir" && ./first 20180716 0.5               # and actually run the first one

正确地发射CCD_ 2。

最新更新