Modifying a parameter pass to a script (Bash)

前端 未结 3 535
醉酒成梦
醉酒成梦 2021-01-12 13:41

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 tw

3条回答
  •  星月不相逢
    2021-01-12 14:41

    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
    

    Maybe this:

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

    BIG EDIT:

    Use this script (called listadd for instance):

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

    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
    

提交回复
热议问题