[問題] shellscript執行指令 command not found

看板LinuxDev作者 (stellvia)時間10年前 (2015/02/21 12:50), 10年前編輯推噓2(203)
留言5則, 2人參與, 最新討論串1/1
目前是想寫一個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

02/22 11:22, , 1F
加個eval吧: log="$(eval "$2")"
02/22 11:22, 1F

02/22 11:24, , 2F
此外,bash可以把exec < 跟while do ... done合併成:
02/22 11:24, 2F

02/22 11:25, , 3F
done < $filename
02/22 11:25, 3F

02/22 11:25, , 4F
然後exec < $filename那行就可以拿掉了
02/22 11:25, 4F

02/23 03:25, , 5F
太感謝您了 解了 謝謝x9999
02/23 03:25, 5F
文章代碼(AID): #1Kw0wbRq (LinuxDev)
文章代碼(AID): #1Kw0wbRq (LinuxDev)