【发布时间】:2018-01-24 02:32:56
【问题描述】:
脚本应该能够更改 shell / bash 脚本中的 argv[0] 值。 我在较旧的帖子上找到了,但无法真正理解它在做什么。 有人可以解释一下这条线是如何工作的吗:
sh -c ". '$0'" argv0new "$@"
test ".$INNERCALL" 也是一个变量吗?
原问题:How to change argv[0] value in shell / bash script?
#! /bin/bash # try executing this script with several arguments to see the effect
test ".$INNERCALL" = .YES || {
export INNERCALL=YES
# this method works both for shell and bash interpreters
sh -c ". '$0'" argv0new "$@"
# bash -c ". '$0'" argv0new "$@"
exit $?
}
printf "argv[0]=$0\n"
i=1 ; for arg in "$@" ; do printf "argv[$i]=$arg\n" ; i=`expr $i + 1` ;done
【问题讨论】:
-
您已将您的问题标记为bash,但您正在调用
sh并在您的shebang 中使用/bin/sh。你能澄清一下你对什么外壳感兴趣吗?重击? POSIX? (提示:更新您的问题,不要只在 cmets 中回答。) -
顺便说一句,
sh -c ". '$0'"-- 或 any 使用非常量的sh -c '...'值调用sh -c '...'的地方 -- 是 非常 b> 从安全角度来看是个坏主意。与sh -c '. "$0" "$@"' argv0new "$@"进行比较,这是安全的。 -
顺便说一句,如果你的 shell 是 bash -- 而不是
sh-- 你可以在启动一个新进程时设置argv[0](或者用不同的进程替换 shell就地实例)使用exec的-a参数。