简体   繁体   English

如何从另一个 shell 脚本的数组变量替换一个 shell 脚本的数组变量?

[英]How to replace an array variable of one shell script from another shell script's array variable?

I have two shell scripts, fruits_original.sh and appending_fruits.sh .我有两个 shell 脚本, fruits_original.shappending_fruits.sh In the fruits_original.sh I have one array variable: fruits=('Apple' 'Mango' 'Guava') .fruits_original.sh我有一个数组变量: fruits=('Apple' 'Mango' 'Guava')

What I want to do is I have to write a shell script appending_fruits.sh that will take an argument some new fruits name is Orange and will append that new fruit name to the fruits_original.sh fruits array variable.我想要做的是我必须编写一个 shell 脚本appending_fruits.sh ,它将接受一个参数,一些新的水果名称是Orange ,并将 append 新水果名称添加到fruits_original.sh水果数组变量中。

After script run fruits array should be remain an array only and its value should be fruits=('Apple' 'Mango' 'Guava' 'Orange') .脚本运行之后,fruits 数组应仅保留为数组,其值应为fruits=('Apple' 'Mango' 'Guava' 'Orange')

The file fruits_original.sh has this.文件fruits_original.sh有这个。 Below is the appending_fruits.sh script by this my variable is changing into this fruits= ('Apple' 'Mango' 'Guava' 'Orange') .下面是appending_fruits.sh脚本,我的变量正在变成这个fruits= ('Apple' 'Mango' 'Guava' 'Orange') But when I am trying to do echo "${fruits[@]}" I am getting this error:但是当我试图做echo "${fruits[@]}"我收到这个错误:

line 1: syntax error near unexpected token `('

Any luck?运气好的话?

fruits= ('Apple' 'Mango' 'Guava') 
echo "${fruits[@]}"
    declare -a var=$(awk -F'=' '/^fruits=/ {print $2}' fruits_original.sh)
    echo "${var[@]}"
    var[${#var[@]}]='Orange'
    joined=$(printf " '%s'" "${var[@]}")
    echo ${joined:1}
    echo "${joined[@]}"
    sed -i "s/fruits=.*/fruits= ($( echo ${joined:1})) /" fruits_original.sh

Do not modify the script file.不要修改脚本文件。 Instead, create another file and source the dynamic data from it.相反,创建另一个文件并从中获取动态数据。 I have chosen the location of configuration to be in /tmp directory.我选择了配置的位置在/tmp目录中。

# fruits_original.sh
fruits=()
if [[ -e /tmp/fruits_original.rc ]]; then
        . /tmp/fruits_original.rc 
fi
some stuff

Then generate the config file.然后生成配置文件。 Use declare -p to safely output properly quoted variables.使用declare -p安全地 output 正确引用变量。

# appending_fruits.sh
fruits=()
if [[ -e /tmp/fruits_original.rc ]]; then
        . /tmp/fruits_original.rc 
fi   
fruits+=("new fruit")
decalre -p fruits > /tmp/fruits_original.rc

Put a uuid inside fruits_original.sh to recognize where is your snippet that you want to work with.fruits_original.sh中放置一个 uuid 以识别您想要使用的代码片段在哪里。

# fruits_original.sh

# snip 419d0df3-5f08-4511-ad5a-ad24db45aa6c
fruits=()
# snip 419d0df3-5f08-4511-ad5a-ad24db45aa6c

some stuff

Then extract the relevant parts with sed or other tool, declare "$part" it into a variable, append normally and then capture output from declare -p and replace the content between the marks again.然后用sed或其他工具提取相关部分,将declare "$part"为变量,append 正常捕获 output 从declare -p中再次替换标记之间的内容。


If not going with any of the above and this is only a very toy example to test some stuff, you could:如果采用上述任何方法,并且这只是一个测试某些东西的玩具示例,您可以:

# read the line from another script
declare "$(sed '/fruits=/!d' fruits_original.sh)"
# append element
fruits+=(Orange)
# create source-able output
new="$(declare -p fruits)"
# remove declare -- in front
new="fruits=${new%*fruits=}"
# Replace the line with declare -p output.
sed -i "s/fruits=.*/fruits=$new/" fruits_original.sh

Notes:笔记:

  • var[${#var[@]}]='Orange' - just var+=(Orange) . var[${#var[@]}]='Orange' - 只是var+=(Orange) No need for ${# .不需要${#
  • $( echo ${joined:1}) is a useless use of echo (unless you want word splitting and filename expansion). $( echo ${joined:1})是对echo的无用使用(除非您想要分词和文件名扩展)。
  • check your scripts with https://shellcheck.net使用https://shellcheck.net检查您的脚本
  • fruits= ( is not an assignment and will run a subshell and could cause syntax error. There is no space in assignment around = . fruits= (不是一个赋值,将运行一个子shell,可能会导致语法错误。在=周围的赋值中没有空格。
  • declare -a var=$( - var is not an array (or, it's an array with one element). declare -a var=$( - var不是一个数组(或者,它是一个只有一个元素的数组)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM