Debian测试,bash ......
我试图从现有程序中加载变量。
设置程序变量
xPROGS="$(echo -e "exiftool\nrsync\nxsel")"
尝试使用x(程序名称)
创建变量echo "$xPROGS" | while read z; do x$z="$(whereis -b "$z" | awk '{print $2}')" ; done
错误;
bash: xexiftool=/usr/bin/rsync: No such file or directory
bash: xrsync=/usr/bin/rsync: No such file or directory
bash: xxsel=/usr/bin/rsync: No such file or directory
这有效;
$ whereis -b rsync | awk '{print $2}'
我无法成功改变变量名称。
有人可以帮忙。
答案 0 :(得分:2)
$ cat t.sh
#!/bin/bash
progs=(exiftool rsync xsel)
for prog in "${progs[@]}"; do
read -r _ "x${prog}" _ <<< "$(whereis -b "${prog}")"
done
echo "exiftool: [${xexiftool}]"
echo "rsync: [${xrsync}]"
echo "xsel: [${xxsel}]"
$ ./t.sh
exiftool: []
rsync: [/usr/bin/rsync]
xsel: []
答案 1 :(得分:1)
@Etan Reisner提供了以下代码所在的链接:
echo "$xPROGS" | while read z; do IFS= read -r "x$z" <<<$(whereis -b rsync | awk '{print $2}') ; done
但我觉得rsync命令不会改变它在文件系统树中的位置
RSYNC=$(whereis -b rsync); RSYNC="${RSYNC#* }"; echo "$xPROGS" | while read z; do IFS= read -r "x$z" <<<"$RSYNC" ; done