[英]Bash menu script - Cannot execute commands
我正在建立一個最小的標准bash菜單。 選擇工作正常,但命令未執行。 我懷疑我可能會包裝命令,但是不確定如何完成。 我經常會收到2條命令,因此需要以bash將其理解為命令行的方式來分隔命令。
我發現了這樣的標題類似的問題,但沒有回答為什么未執行bash腳本中的命令:
什么有效,什么無效?
按1:不更改以更正cd。
按2:在正確的文件夾中創建文件。
按3:工作。
按4:Works(R文件准備為:a <-1)。
想要的行為:
我需要執行bash菜單腳本中的命令。
#!/bin/bash
# -----
# Menu:
# -----
while :
do
echo "Menu"
echo "1 - Change directory to /tmp"
echo "2 - Create file test1.sh with hello message, in /tmp"
echo "3 - Execute test1.sh"
echo "4 - Execute R-script [/tmp/test2.R]"
echo "Exit - any kind but not [1-4]"
read answer;
case $answer in
1)
echo "Change directory to [\tmp]"
cd /tmp # Command to be executed.
break
;;
2)
echo "Create file [test1.sh] in [\tmp]"
# Commands to be executed.
cd /tmp
touch test1.sh
chmod +x test1.sh
echo echo "hello" > test1.sh
break
;;
3)
echo "Execute file [test1.sh]"
/tmp/./test1.sh # Command to be executed.
break
;;
4)
echo "Execute R-script [/tmp/test2.R]"
cd /tmp && /usr/bin/Rscript test2.R # Command to be executed.
break
;;
*)
# Command goes here
echo "Exit"
break
;;
esac
done
要執行所有情況時,請勿使用break。
並且在情況3中,您使用的是/tmp/./test1.sh
它應該是./tmp/test1.sh
或sh /tmp/test1.sh
您的代碼應為:
#!/bin/bash
# -----
# Menu:
# -----
while :
do
echo "Menu"
echo "1 - Change directory to /tmp"
echo "2 - Create file test1.sh in /tmp"
echo "3 - Execute test1.sh"
echo "4 - Execute R-script [/tmp/test2.R]"
echo "Exit - any kind but not [1-4]"
read answer;
case $answer in
1)
echo "Change directory to [\tmp]"
cd /tmp # Command to be executed.
pwd
;;
2)
echo "Create file [test1.sh] in [\tmp]"
touch /tmp/test1.sh # Command to be executed.
;;
3)
echo "Execute file [test1.sh]"
./tmp/test1.sh # Command to be executed.
;;
4)
echo "Execute R-script [/tmp/test2.R]"
/usr/bin/Rscript /production/20_front_trader/build/x_run_front_trader.R # Command to be executed.
;;
*)
# Command goes here
echo "Exit"
;;
esac
done
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.