我希望能夠寫一個函數su_mt_user
是(目前)是這樣的:擺脫使用bash函數參數蘇-c
su_mt_user() {
su someuser -c "$*"
}
的目標是能夠使用它像這樣的:
su_mt_user mt-auth --stuff etc
這將運行命令mt-auth --stuff etc
爲用戶someuser
。當前版本適用於這個特定的命令,但它不能像這樣的命令:
some_filename_with_spaces="/home/user/hello there i like spaces in filenames.txt"
su_mt_user stat "$some_filename_with_spaces"
這失敗,出現以下錯誤:
stat: cannot stat '/home/user/hello': No such file or directory
stat: cannot stat 'there': No such file or directory
stat: cannot stat 'i': No such file or directory
stat: cannot stat 'like': No such file or directory
stat: cannot stat 'spaces': No such file or directory
stat: cannot stat 'in': No such file or directory
stat: cannot stat 'filenames.txt': No such file or directory
我認爲這個錯誤是因爲即使$some_filename_with_spaces
是正確的作爲一個參數傳遞給su_mt_user
函數,該函數將其展開爲多個參數,其中"$*"
。
我也試過這個,試錯:
su_mt_user() {
su someuser -c "$0 [email protected]"
}
但還是失敗,那麼(/usr/bin/stat: cannot execute binary file
(是什麼)?)
當然stat "$some_filename_with_spaces"
作品如預期,從兩個當前用戶和someuser
用戶。
這看起來像一些逃避的需要做,但bash知道如何做到這一點?它是否需要手動取締?如果是這樣,哪些字符需要轉義?
謝謝!爲了防止'su'解析選項參數,必須稍加修改才能工作:'su someuser -c'「$ 0」「$ @」' - 「$ @」';否則,'su'試圖將'-a'等選項參數解釋爲自身的參數,導致錯誤。 –
我也必須修改成'su suuser -c'「$ 0」「$ @」' - 「$ 0」「$ @」'因爲命令'/ bin/su -l root -c'echo args「$ @「這個」$ 0「' - abc'顯示'args bc this a'。 – HoverHell