如何使用bash/shell更改argv [0]值? [英] How to change argv[0] value using bash/shell?
问题描述
该脚本应该能够在shell/bash脚本中更改argv [0]的值.我在较旧的帖子上找到了,但无法真正理解它在做什么.有人可以解释一下这条线是如何工作的:
sh -c.'$ 0'" argv0new"$ @"
测试.$ INNERCALL"
也是变量吗?
原始问题:如何更改argv在shell/bash脚本中[0]值?
#!/bin/bash#尝试使用几个参数执行此脚本以查看效果测试.$ INNERCALL" = .YES ||{出口INNERCALL = YES#此方法适用于shell和bash解释器sh -c.'$ 0'" argv0new"$ @"#bash -c.'$ 0'" argv0new"$ @"退出$?}printf"argv [0] = $ 0 \ n"i = 1;为"$ @"中的arg;做printf"argv [$ i] = $ arg \ n";i =`expr $ i + 1`;完成
让我们逐行浏览
测试.$ INNERCALL" =.是
基本上,这可以查看 $ INNERCALL
中是否已经有值 YES
.Shell的"
函数以一种安全的方式进行变量扩展,并将其全部编组为一个值,例如
foo ="hello"bar =,世界!"回声"foobar是:$ foo $ bar"
打印 foobar是:您好,世界!
||{...}
这使用 这就是魔术发生的地方. 基本上 然后 其余代码只是为了证明除 tl; dr ,它将打开一个子shell进程,告诉它读取当前文件,替换参数,然后导出测试值,以使其不会无限循环. The script should be able to change argv[0] value in shell / bash script.
I found on an older post but couldn't really understand what it is doing.
Could someone please explain how the line works: Also is Original question: How to change argv[0] value in shell / bash script?
Let's go through this line by line basically this sees if prints |||
运算符来表示,如果先前的程序返回的值非零(例如测试是一个程序,如果关联的条件为false,则返回1,然后执行此代码块,否则跳过它(请参见 sh -c
打开一个新的shell,然后从后面的字符串中读取其参数..'$ 0'"
对当前位于 $ 0
的值使用 sh
的源函数,该值应该是当前文件./p> sh -c.'$ 0'"
只是在子 sh
进程中再次打开当前文件,然后其余行替换参数: argv0new
成为新的$ 0,并且还通过包含"$ @"
exit $?
返回子进程运行的任何返回值. $ 0
以外的所有参数都相同,并且已替换 $ 0
.sh -c ". '$0'" argv0new "$@"
test ".$INNERCALL"
meant to be a variable ?#! /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
test ".$INNERCALL" = .YES
$INNERCALL
already has the value YES
in it. Shell's ""
function does variable expansion in a safe way, and marshalls it all into a value, e.g.foo="hello"
bar=", world!"
echo "foobar is: $foo$bar"
foobar is: hello, world!
|| {
...
}
This uses the ||
operator to say, if the previous program returned with a non-zero value (e.g. test is a program that returns 1 if the associated conditional is false), then execute this code block, otherwise skip it (see this link for more on ||
)
export INNERCALL=YES
This sets INNERCALL
to YES
, which means that this only executes for the first level
sh -c ". '$0'" argv0new "$@"
exit $?
This is where the magic happens. sh -c
opens a new shell, which then reads its arguments from the string that follows. ". '$0'"
uses sh
's source function on the value currently at $0
, which is supposed to be this current file.
Basically sh -c ". '$0'"
just opens the current file again in a child sh
process, then the rest of the line replaces the arguments:
argv0new
becomes the new $0, and you keep the original arguments to the file by also including "$@"
then exit $?
returns whatever return value the child process runs.
the rest of the code is just to prove that all of the arguments except for $0
is the same, and $0
has been replaced.
tl;dr it opens up a child shell process, tells it to read the current file, replaces the arguments, and then exports a test value so that it doesn't infinite loop.
这篇关于如何使用bash/shell更改argv [0]值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!