作者stellvia7 (stellvia)
看板LinuxDev
標題[問題] shellscript執行指令 command not found
時間Sat Feb 21 12:50:43 2015
目前是想寫一個script 讀取檔案a
此檔案a有寫要執行的指令以及要下的參數,且每個參數會用"," 隔開
但是會出現command not found
目前卡在有空白的指令就會 command not found
a:
#arg0 execute or not
#arg1 command
#arg2 match pattern
#arg3 timeout
1,ls,test,2
1,ls -al,test,2
--------------------------------------
test.sh:
#!/bin/bash
IFS=","
filename='commands'
exec < $filename
while read line
do
set $line
if [ "$1" == "1" ]; then
echo "Now Execute command $2, compare with $3, set timeout $4"
log="$($2)" ==> 問題在這邊會出現command not found : ls -al
==> 假如指令只有 ls 就沒問題,多了一個空白就不行
....
....
done
麻煩各位幫忙一下 卡了我好久
謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.129.100.128
※ 文章網址: https://www.ptt.cc/bbs/LinuxDev/M.1424494245.A.6F4.html
※ 編輯: stellvia7 (220.129.100.128), 02/21/2015 12:52:39
※ 編輯: stellvia7 (220.129.100.128), 02/21/2015 12:53:01
推 goldie: 加個eval吧: log="$(eval "$2")" 02/22 11:22
推 goldie: 此外,bash可以把exec < 跟while do ... done合併成: 02/22 11:24
→ goldie: done < $filename 02/22 11:25
→ goldie: 然後exec < $filename那行就可以拿掉了 02/22 11:25
→ stellvia7: 太感謝您了 解了 謝謝x9999 02/23 03:25