修改传递给脚本的参数 (Bash)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6261126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Modifying a parameter pass to a script (Bash)
提问by yaegerbomb
I have been looking on Google for quite a while now and can't find anything that is matching what I need/want to do.
我已经在 Google 上搜索了很长时间,但找不到与我需要/想做的事情相匹配的任何内容。
My objective is to write a script that takes two arguments. It will search through the first argument (which is a list) and detect if the second argument is already in it. For example:
我的目标是编写一个带有两个参数的脚本。它将搜索第一个参数(它是一个列表)并检测第二个参数是否已经在其中。例如:
list = /bin/foo:/bin/random:random
列表 = /bin/foo:/bin/random:random
to add to list: /bin/foobar
添加到列表:/bin/foobar
Calling the script will produce the result of /bin/foo:/bin/random:random:/bin/foobar.
调用脚本会产生 /bin/foo:/bin/random:random:/bin/foobar 的结果。
If the part to add to the list is already in the list then nothing will be changed of the original.
如果要添加到列表中的零件已经在列表中,那么原始零件将不会发生任何变化。
I have everything working up until the point where I want to modify the parameter I passed.
在我想修改我传递的参数之前,我一切都在工作。
...
if [ $RUN = 1 ]; then
echo
else
="$NEWLIST"
fi
exit 0
This however produced an error. It says that the command isn't found and gives me the line number that $1="$NEWLIST" is on. What am I doing wrong here? How do I modify $1? Thanks!
然而,这产生了错误。它说没有找到该命令,并给了我 $1="$NEWLIST" 所在的行号。我在这里做错了什么?我如何修改 $1?谢谢!
edit:
编辑:
$ PATH=/opt/bin:$PATH
$ ./scrip.sh PATH /user/opt/bin
$ /opt/bin:/user/opt/bin
This is what I would want as a result of the script.
这就是我想要的脚本结果。
采纳答案by Hyperboreus
adymitruk already said it, but why do you want to assign to a parameter. Woudln't this do the trick?
adymitruk 已经说过了,但是你为什么要赋值给一个参数。这难道不是诀窍吗?
if `echo :: | grep "::" 1>/dev/null 2>&1`
then
echo
else
echo :
fi
Maybe this:
也许这个:
list="1:2:3:4"
list=`./script $list 5`;echo $list
BIG EDIT:
大编辑:
Use this script (called listadd for instance):
使用此脚本(例如称为 listadd):
if ! `echo :${!1}: | grep "::" 1>/dev/null 2>&1`
then
export =${!1}:
fi
And source it from your shell. Result is the following (I hope this is what wsa intended):
并从您的外壳中获取它。结果如下(我希望这是 wsa 的意图):
lorenzo@enzo:~$ list=1:2:3:4
lorenzo@enzo:~$ source listadd list 3
lorenzo@enzo:~$ echo $list
1:2:3:4
lorenzo@enzo:~$ source listadd list 5
lorenzo@enzo:~$ echo $list
1:2:3:4:5
lorenzo@enzo:~$ list2=a:b:c
lorenzo@enzo:~$ source listadd list2 a
lorenzo@enzo:~$ echo $list2
a:b:c
lorenzo@enzo:~$ source listadd list2 d
lorenzo@enzo:~$ echo $list2
a:b:c:d
回答by glenn Hymanman
To set the positional parameters $1, $2, ..., use the setcommand:
要设置位置参数$1, $2, ...,请使用以下set命令:
set foo bar baz
echo "$*" # ==> foo bar baz
echo # ==> foo
set abc def
echo "$*" # ==> abc def
If you want to modify one positional parameter without losing the others, first store them in an array:
如果你想修改一个位置参数而不丢失其他参数,首先将它们存储在一个数组中:
set foo bar baz
args=( "$@" )
args[1]="BAR"
set "${args[@]}"
echo "$*" # ==> foo BAR baz
回答by Adam Dymitruk
First copy your parameters to local variables:
首先将参数复制到局部变量:
Arg1=
Then, where you are assigning leave off the $ on the variable name to the left of the =
然后,您在 = 左侧的变量名称上分配 $ 的位置
You can't have a $ on the left hand side of an assignment. If you do, it's interpreting the contents of $1 as a command to run
作业的左侧不能有 $。如果这样做,它会将 $1 的内容解释为要运行的命令
hope this helps
希望这可以帮助

