我运行一个脚本。/script.sh。
./script config.txt 480哪一张发票
command --crf "${crf##*=}"它从配置文件中读取。配置文件包含几个参数,以模式命名:<parameter1><number>=<value>,例如。
crf480=18.2
crf720=18.5
(…)现在,我在脚本的开头包括一些行:
<An IFS that reads the config>
crf=$(cat "$config"|grep crf|grep $2)
qcomp=$(cat "$config"|grep qcomp|grep $2)
aqmode=$(cat "$config"|grep aqmode|grep $2)
…因此,使用./script config.txt 480 $crf具有所需的值(crf480的值)。
我想在开始时避免使用这个长长的列表,并在内联中进行替换/扩展,这样就可以根据$2将"$crf“扩展到"$crf480”。我在https://mywiki.wooledge.org/BashFAQ上花了一段时间在这里搜索这个站点,但由于我不是以英语为母语的人,而且对bash的了解也很有限,所以我没能找到解决方案。
在bash中可以进行这样的内联替换吗?如果可以,如何进行?
发布于 2016-11-25 17:42:33
只需读取配置的每一行并设置变量,如果后缀是您要查找的内容:
$ cat script
#!/bin/bash
suffix="$1"
# Read each name/value pair
while IFS="=" read -r name value
do
# Check if the name ends with our chosen suffix
if [[ $name == *"$suffix" ]]
then
# Set the variable name without the suffix
declare "${name%"$suffix"}=$value"
fi
done < config
echo "\$var contains $var"如果config包含以下内容:
$ cat config
var480=four eighty
var720=seven twenty您可以这样运行脚本:
$ ./script 480
$var contains four eighty
$ ./script 720
$var contains seven twentyhttps://stackoverflow.com/questions/40809875
复制相似问题