有一个类似busybox的shell脚本,它根据调用它的符号链接来决定该做什么。
我想直接调用它,传递符号链接的名称,而不实际创建符号链接。
发布于 2015-09-02 11:51:41
如果它是一个脚本,如果它可以被引用:
bash -c '. script.sh' overridden-name param1 param2为我的案子工作。
发布于 2015-09-02 10:58:02
虽然这个想法永远不会出现在我的脑海中,但我尝试了一下,只是为了好玩。我的结论是:你不能(至少,我不能)。
这里是如何和为什么,假装你会用$1作为潜在的符号链接。
#!/bin/bash
# not surprisingly, you cannot do this:
# 0 = $1
# '0' is not a valid variable name, so bash try to use it as a command, hence:
# 0: command not found
# you CAN do this, as meaningless as it seem:
# $0 = $1
# but say your script is called as ./foobar.sh, this will actually result in:
# ./foobar.sh = whatever_$1_is
# ...which is a nice fork bomb, since you'll call your script again,
# which will call your script again,
# which will call your script again,
# which will call ...etc etc etc
# Try it! :-)
# I've just learned about $BASH_SOURCE from here:
# http://www.tldp.org/LDP/abs/html/debugging.html#BASHSOURCEREF
echo BASH_SOURCE $BASH_SOURCE
# you CAN do this:
BASH_SOURCE = $1
# ...but to no use, since the following will show that BASH_SOURCE
# rightfuly behave as read-only:
echo BASH_SOURCE $BASH_SOURCE #unmodified因此,如果您的想法是在不影响创建符号链接的情况下建立"a la busybox“行为的话,我建议:
#!/bin/bash
called_as = $1 # to be replaced with called_as = $0 once you're done prototyping
# if called_as such name do this
# if called_as such other name do thathttps://stackoverflow.com/questions/32349871
复制相似问题