这在bash中应该是非常直接的,我不知道为什么我在努力。我是一个bash新手,所以请温柔。
伪代码:
read a configuration file, extract the first line beginning with a key/value pair
in the format exec=/path/to/myprog -opt1 -opt2 $var1 $var2 ...
check that the /path/to/myprog is executable
if executable then
replace $var1, ... with the contents of the same bash variables in the script
check that all variables were replaced with existing bash variables
if aok
execute the command and be happy
else
complain echoing the partially-substituted command string
fi
else
complain echoing the un-substituted command string
fi
我尝试的任何东西似乎都没有正常工作。我已经杀了足够的时间尝试各种事情。建议,有人吗?
答案 0 :(得分:1)
conf文件:
exec=/bin/ls -l $var1 $var2
bash文件:
#!/bin/bash
CONFIG="tmp.conf"
var1=./
var2=helo
function readconf() {
args=()
while IFS=' ' read -ra argv; do
exec=${argv[0]#*=}
`command -v ${exec} >/dev/null 2>&1 || { echo >&2 "I require ${exec} but it's not installed. Aborting."; exit 1; }`
for i in "${argv[@]:1}"; do
if [[ $i == \$* ]]; then
sub=${i:1}
args+=(${!sub})
fi
done
done < $CONFIG
echo ${args[@]}
}
readconf
上面的代码提供了实现所需内容所需的关键组件。至少我是这么认为的。您可以根据此骨架添加逻辑。
以下网址可能会有所帮助:
Check if a program exists from a Bash script
In bash, how can I check if a string begins with some value?
How do I split a string on a delimiter in Bash?