How to write an interactive shell script
These are two examples on how to write an interactive shell script, either with a YES/NO prompt and with a LIST among which the user has to choose.As always, start your script file with:
#!/bin/sh
Interactive with a YES/NO prompt:
echo "What do you like me?" select yn in "Yes" "No"; do case $yn in Yes ) echo "So I do $USER!"; break;; No ) echo "Neither do I $USER"; break;; esac done
After Yes ) and No) you can add a series of commands, each one delimited by a ; (semicolon) at the end.
When you finish your command sequence put ;; (two semiclon) (this way the script would be recursive)
If you do not want your script to be recursive and want to go to the next question put break;;
Interactive with a LIST from which to choose:
while : do clear echo "Choose the mode which best fits you" echo "1. AUTO (default)" echo "2. HP generic laptop" echo "3. Fujitsu" echo "4. Acer" echo "5. Dell" echo "6. Lenovo Thinkpad" echo "7. UNDO TWEAK / CHANGE MODE / PRESSED WRONG DIGIT" echo "8. DONE TWEAKING!" echo -n "Please enter option [1 - 8]" read opt case $opt in 1) echo;; 2) sudo echo "options snd-hda-intel model=hp" >> /etc/modprobe.d/alsa-base.conf;; 3) sudo echo "options snd-hda-intel model=fujitsu" >> /etc/modprobe.d/alsa-base.conf;; 4) sudo echo "options snd-hda-intel model=acer" >> /etc/modprobe.d/alsa-base.conf;; 5) sudo echo "options snd-hda-intel model=dell" >> /etc/modprobe.d/alsa-base.conf;; 6) sudo echo "options snd-hda-intel model=thinkpad" >> /etc/modprobe.d/alsa-base.conf;; 7) sudo sed '$d' /etc/modprobe.d/alsa-base.conf /tmp/alsa-base.conf ; mv /tmp/alsa-base.conf /etc/modprobe.d/alsa-base.conf;; 8 ) echo "Bye $USER"; exit 1;; *) echo "$opt is an invaild option. Please select option between 1-17 only"; echo "Press [enter] key to continue. . ."; read enterKey;; esac done
As before remember to end each command with a semicolon ; and to end each menu entry with double semicolon ;;
The end of the script would be an exit 1;;
to display a file as a choice of the interactive script you can use the command more
2) more /etc/resolv.conf
to require user press the Enter key to acknowledge some message:
echo "Press [enter] key to continue. . ."; read enterKey;;
Related posts:
- How to update ALSA to latest version easily
- shell Jobs
- change keyboard layout in shell
- Disk quota management
- fast deployment of secure (APOP) Qpopper email pop3 server
- “bash: ./configure: /bin/sh: bad interpreter: Permission denied”
- Monitoring Real-time user logins in ubuntu
Print This Post
|
Email This Post
[...] How to write an interactive shell script [...]