[英]Is there a way to make a shell become interactive in the middle of a script?
[英]interactive Shell Script
如何創建一個簡單的Shell腳本,該腳本要求用戶提供簡單的輸入,然后僅運行與預定義選項相關聯的命令,例如
IF "ON"
Backup Server
ELSEIF "OFF"
Delete Backups
ELSEIF "GREY"
Send Backups
ENDIF
您可以通過read
從用戶那里獲取輸入,還可以使用case ... esac
塊來執行其他操作。
讀取將其值存儲到的變量名稱作為參數
read foo
將接受用戶的歡迎,並將其存儲在$foo
。
為了提示用戶輸入,您將需要使用echo。
echo "What is your favourite color?"
read color
最后,大多數shell腳本都支持case運算符。 采取的形式
case "value" in
"CHOICE)
# Do stuff
;;
esac
放在一起:
echo "Which choice would you like? \c"
read choice
case "$choice" in
ON)
# Do Stuff
;;
OFF)
# Do different stuff
;;
*)
echo "$choice is not a valid choice"
;;
esac
#!/bin/bash
select choice in "ON" "OFF" "*"; do
case "$choice" in
ON) echo "$choice"; # do something;
break;;
OFF) echo "$choice"; # do something;
break;;
*) echo "$choice other"; # do something;
break;;
esac
done
嗨,有一個簡單的例子,怎么做
while true; do
read -p 'do you want to continue "y" or "n": ' yn
case $yn in
[Yy]* ) echo 'this program continue '; break;;
[Nn]* ) exit;;
* ) echo 'Please answer yes or no: ';;
esac
done
while true; do
read -p 'press "c" to quit this program: ' c
case $c in
[Cc]* ) exit;;
* ) echo 'for quit this program press "c": ' ;;
esac
done
對於源單擊此處源
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.