看板 Linux 關於我們 聯絡資訊
直覺上會使用 function 解決,如果一定要用 alias 處理的話,可以用 alias 包裝 fun ction 使用如下: alias abc='F(){ test -n "$2" && echo "--message $1 --file $2" || echo "--messa ge $1" ; } ; F' chronos@localhost ~ $ abc 123 --message 123 chronos@localhost ~ $ abc 123 /tmp/filename --message 123 --file /tmp/filename ※ 引述《chang0206 (Eric Chang)》之銘言: : 我有一串指令想要用 alias 簡化 : 指令的順序會影響結果 : 指令如下 : command arg_1 --message "123" --file /tmp/123.txt arg_2 : 其中 arg_1 , arg_2 都是固定的 : 但是 /tmp/123.txt 這個檔案非必要條件 : command arg_1 --message "123" arg_2 : 這樣子也是可以的 : 如果我想要用 abc "123" "/tmp/123.txt" 這樣的格式下指令的話 : 應該要怎麼做alias ? : 有沒有辦法當使用者沒有指定 /tmp/123.txt 的時候 : 指令就不要帶 --file 進去? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.238.128.193 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Linux/M.1669714972.A.262.html ※ 編輯: dyoll (36.238.128.193 臺灣), 11/29/2022 17:44:43
rickieyang: 不建議用 and + or。如果 and 後面的指令回傳值是 f 11/29 23:25
rickieyang: alse,那會再呼叫一次 or 後面的指令。 11/29 23:25
dyoll: 一樓說的沒錯。改用 if else 或 and + or 只做變數設定。 11/30 09:33
chang0206: 感謝回覆 已經用function處理 謝謝各位指教 11/30 12:27
rickieyang: 只做變數設定, 或是 and / or 單獨使用,不要混用 11/30 13:57
rickieyang: 話說變數設定也不排除有機率設定失敗.還是不要混用吧 11/30 14:00