====== Perl 呼叫外部程式命令差異 ====== ==== system() : 呼叫後會回來繼續往下執行 ==== * Exp1. system("/usr/local/share/ca-certificates/; sudo update-ca-certificates; sudo systemctl restart docker.service"); * Exp2. system("sh", "script.sh", "--help" ); * Exp3. system("sh script.sh --help"); ==== exec() : 呼叫後就不會回來 ==== * Exp1. exec("vi abc.txt"); ==== backticks(` `) 或是 qx/ / : 呼叫後會回來, 可將執行的結果傳入變數 ==== * Exp1. $cmd_msg 內就是回傳執行的結果 $cmd_msg = `ls -l`; * Exp2. 搭配 2>&1 可讓所有訊息都導回來 $cmd_msg = `sudo apt update -y 2>&1`; * Exp3. 也可使用 qx/ / 語法 $cmd_msg = qx/script.sh --option/; ===== 參考網址 ===== * https://stackoverflow.com/questions/3200801/how-can-i-call-a-shell-command-in-my-perl-script {{tag>perl}}