簡體   English   中英

bash腳本中缺少與su -c匹配的引號

[英]Missing matching quote with su -c in bash script

我想寫點東西

EXEC="sudo su -m root -c \"java Something\""
$EXEC &

但我得到以下錯誤:

Something": -c: line 0: unexpected EOF while looking for matching `"'
Something": -c: line 1: syntax error: unexpected end of file

如果我在命令行上寫命令,它將執行。 如果我將其存儲在變量中並嘗試對其進行推斷,則不會。 為什么?

嘗試這個:

exec="ls -l \"/a b c\""
$exec

您將看到類似以下內容:

ls: cannot access "/a: No such file or directory
ls: cannot access b: No such file or directory
ls: cannot access c": No such file or directory

它確切地顯示了問題所在,即, 單詞拆分完成了變量的擴展。

要使其工作,可以使用eval:

=$ eval "$exec"
ls: cannot access /a b c: No such file or directory

甚至:

=$ sh -c "$exec"

或者更好的是,不要使這些命令運行。 不確定其目的是什么,但請考慮避免在變量中構建完整的命令行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM