修改参数传递到脚本(击) [英] Modifying a parameter pass to a script (Bash)

查看:128
本文介绍了修改参数传递到脚本(击)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找在谷歌相当长一段时间,现在并不能找到任何匹配的是什么,我需要/想这样做。

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.

我的目标是编写一个脚本,有两个参数。它将通过第一个参数查询(这是一个列表),并检测是否第二个参数是已经在它。例如:

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

添加到列表:/斌/ foobar的

to add to list: /bin/foobar

调用脚本会产生/斌/ foo的结果:/ bin中/随机:随机:/斌/ foobar的

Calling the script will produce the result of /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 $1
else
    $1="$NEWLIST"
fi
exit 0

然而,这产生了错误。它说,该命令没有找到,给我的行号$ 1 =$ NEWLIST上。我在做什么错在这里?如何修改$ 1吗?谢谢!

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!

编辑:

$ 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.

推荐答案

adymitruk已经说过了,但是你为什么要分配给一个参数。 Woudln't该做的伎俩?

adymitruk already said it, but why do you want to assign to a parameter. Woudln't this do the trick?

if `echo :$1: | grep ":$2:" 1>/dev/null 2>&1`
then
  echo $1
else
  echo $1:$2
fi

这也许:

list="1:2:3:4"
list=`./script $list 5`;echo $list

BIG编辑:

使用这个脚本(称为listadd举例):

Use this script (called listadd for instance):

if ! `echo :${!1}: | grep ":$2:" 1>/dev/null 2>&1`
then
  export $1=${!1}:$2
fi

和源从你的shell。结果如下(我希望这是WSA意):

And source it from your shell. Result is the following (I hope this is what wsa intended):

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

这篇关于修改参数传递到脚本(击)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆