I have an options menu function:
function()
{
echo "1 Option 1"
echo "2 Option 2"
echo "3 Option 3"
echo "q Exit"
read -p "Select 1-3 ή \"q\" to quit: " i
case "$i" in
1)
echo "option 1"
echo;;
2)
echo "option 2"
echo;;
3)
echo "option 3"
echo;;
q) echo -e "\033[01;33mexit!!!\033[39m"
sleep 1
clear
exit ;;
*)
echo "Unknown command"
read -s -n 1 -p "Press any key to continue…"
echo
esac
}
while:
do function
done
The above works fine, but need to press enter after I input the number before the command run. Is there any way to immediately run the command when I press the key?
You've got the answer right in your sample code (in the second read). You want to take advantage of bash's read -n 1 capability (note, this is not POSIX compliant, so it won't reliably work in /bin/sh unless that happens to map to bash):
read -n 1 -p "Select 1-3 ή \"q\" to quit: " i
Related
I am writing a menu.
This is the script I have.
when I type q it dose not echo "done"
what I need to change in the script to get it to echo "done"?
#!/bin/bash
rap '' 2
while true
do
clear
echo "=============================="
echo "menu"
echo "=============================="
echo "Enter 1 to for option 1t: "
echo "Enter q to exit:"
echo -e "\n"
echo -e "Enter your selection"
read answer
case "$answer" in
1) echo "option 1"
echo "Option 1"
done;;
q) exit
echo "done" ;;
esac
echo -e "Enter return to continue \c"
read input
done
exit 0
there were some minor bugs in your code. 1) case does must not have done. 2) exit before calling echo does not make sense.
while true; do
clear
echo "=============================="
echo "menu"
echo "=============================="
echo "Enter 1 to for option 1t: "
echo "Enter q to exit:"
echo -e "\n"
echo -e "Enter your selection"
read answer
case "$answer" in
1) echo "option 1"
echo "Option 1";;
q) echo "done"
exit
;;
esac
echo -e "Enter return to continue \c"
read input
done
exit 0
#!/bin/bash
function advancedMenu() {
ADVSEL=$(whiptail --title "Advanced Menu" --fb --menu "Choose an option" 15 60 4 \
"1" "Delete group" \
"2" "Create login name" \
"3" "Exit" 3>&1 1>&2 2>&3)
case "$ADVSEL" in
1)
echo "Option 1"
whiptail --title "Option 1" --msgbox "You chose group deleting. Exit status $?" 8 45
bash ./script3;;
2)
echo "Option 2"
whiptail --title "Option 1" --msgbox "You chose login name creating. Exit status $?" 8 45
bash ./script;;
3)
echo "Option 3"
whiptail --title "Option 1" --msgbox "You chose exit. Exit status $?" 8 45
;;
esac
}
advancedMenu
I don't know how to loop my case statement. I have user friendly interface here. Someone chooses 1\2 or exit. 1 and 2 runs another scripts. So when 1 is clicked and scripted is finished, I need the main menu back until case 3 is clicked. I will be grateful for your help!
#!/bin/bash
export NEWT_COLORS='
window=white,blue
border=white,green
textbox=white,green
button=black,white
'
{
for ((i = 0 ; i <= 100 ; i += 1)); do
sleep 1
echo $i
done
} | whiptail --gauge "Wait..." 6 60 0
touch 35_2.csv
while read line; do
IFS=","
set -- $line
group_name=$1
student_name=$2
student_name=`echo $student_name | tr -d '\r\n' `
login_name=`echo $student_name | sed 's/[b\`]//g;'`
login_name=`echo $login_name | sed 'y/абвгдеєзиіїйклмнопрстуфхь/abvgdeeziiijklmnoprstufh_/'`
login_name=`echo $login_name | sed 's/ /_/g; s/С†/ts/g; s/Р¶/zh/g; s/С‡/ch/g; s/С€/sh/g; s/С‰/sh/g; s/СЋ/yu/g; s/СЏ/ya/;'`
echo "$group_name, $student_name, $login_name" >> 35_2.csv
done < 35_2.csv
Don't look at wrong encoding. Your decision seemed to be right. The first script runs then menu goes back. However, when I start the second script the menu does not go back. Attached the 2nd's script code.
Simply put a while loop around it.
#!/bin/bash
function advancedMenu() {
while :; do
ADVSEL=$(whiptail --title "Advanced Menu" --fb --menu "Choose an option" 15 60 4 \
"1" "Delete group" \
"2" "Create login name" \
"3" "Exit" 3>&1 1>&2 2>&3)
case "$ADVSEL" in
1)
echo "Option 1"
whiptail --title "Option 1" --msgbox "You chose group deleting. Exit status $?" 8 45
bash ./script3;;
2)
echo "Option 2"
whiptail --title "Option 1" --msgbox "You chose login name creating. Exit status $?" 8 45
bash ./script;;
3)
echo "Option 3"
whiptail --title "Option 1" --msgbox "You chose exit. Exit status $?" 8 45
return
;;
esac
done
}
advancedMenu
I'm not sure what you expect Exit status $? to show. $? is the exit status of the echo statement on the previous line, so it will almost always just be 0.
I have created a bash menu script, using options 1-8. For my second menu option I am using the while command to display the current time and date. However, when I press Ctrl+C to end the display of the date it jumps out of the entire script rather than redisplay the menu. Is there a way around this?
yeah, make functions
#more code here ...
display_main_menu() {
while true; do
echo -e "Please select an action:"
echo -e " "
echo -e "1 - Transfer a local file to all servers."
echo -e "2 - Execute a command(s) on all servers."
echo -e "9 - Display all servers."
echo -e "0 - Exit"
echo ""
read -p "" action
case $action in
"1" ) function_to_transfer_file ;;
"2" ) function_to_execute_command ;;
"9" ) function_to_show_server;;
"0" ) exit;;
* ) echo "Please select a valid action.";;
esac
done
}
#more code here ...
Make multiple functions, one for each option in primary menu. Here option 0 exits the script completely.
For secondary menu functions ... option 0 should execute function display_main_menu
You want catch the signal SIGINT (Ctrl+C) and break
trap 'break' SIGINT
For example:
#!/bin/bash
trap 'break' SIGINT
while :
do
sleep 1
echo "foo"
done
echo "bar"
Take a look here to have more details:
http://tldp.org/LDP/Bash-Beginners-Guide/html/chap_12.html
#!/bin/bash
#more code here ...
display_main_menu() {
while true; do
echo -e "Please select an action:"
echo -e " "
echo -e "1 - display time"
echo -e "2 - Execute a command(s) on all servers."
echo -e "9 - Display all servers."
echo -e "0 - Exit"
echo ""
read -p "" action
case $action in
"1" ) trap 'break' SIGINT; while true; do echo "$(date '+%D %T' )"; sleep 1; done ;;
"2" ) function_to_execute_command ;;
"9" ) function_to_show_server;;
"0" ) exit;;
* ) echo "Please select a valid action.";;
esac
done
}
display_main_menu
so I am trying to create a script which gives the user some options to do things, one of that option is to exit the script. However, I want to prevent the user from exiting the script using Control c so that the only way to exit is to select the right option. Is it possible to make the script so that when the user hits control c, the program will not exit, but rather it would echo something like "enter 0 to exit"?
#!/bin/bash
# Acts as a simple administration menu
OPTION=0
echo "-----------------Admin Menu-------------------"
echo "Please select one of the following options: "
echo ""
echo "1 - Run top."
echo "2 - Show system uptime."
echo "3 - Show logged in users."
echo "4 - Exit Menu."
echo "5 - Reprint this menu."
echo "----------------------------------------------"
echo "Please choose an option (5 to reprint menu):"
read OPTION
while [ "$OPTION" -ne 4 ]
do
if [ "$OPTION" -eq 1 ]; then
clear
top -n1
elif [ "$OPTION" -eq 2 ]; then
clear
uptime
elif [ "$OPTION" -eq 3 ]; then
clear
who
elif [ "$OPTION" -eq 4 ]; then
clear
OPTION=4
elif [ "$OPTION" -eq 5 ]; then
clear
echo "------------COSC 2306 Admin Menu--------------"
echo "Please select one of the following options: "
echo ""
echo "1 - Run top."
echo "2 - Show system uptime."
echo "3 - Show logged in users."
echo "4 - Exit Menu."
echo "5 - Reprint this menu."
echo "----------------------------------------------"
else
clear
echo "ERROR: Incorrect Input."
fi
echo "Please choose an option (5 to reprint menu):"
read OPTION
done
clear
You can use the trap built-in to catch SIGINT ( ctrl + C generates the signal SIGINT) and print your message:
trap 'echo "enter 0 to exit"' SIGINT
Similarly you can catch other signals too. To get the list of signals use kill -l.
My script
#!/bin/bash
while :
do
echo "[*] 1 - Create a gnome-terminal"
echo "[*] 2 - Display Ifconfig"
echo -n "Pick an Option > "
read Input
if [ "$Input" = "1" ]; then
gnome-terminal
sleep 3
echo "[*] 1 - Create a gnome-terminal"
echo "[*] 2 - Display Ifconfig"
echo -n "Pick an Option > "
read Input
fi
if [ "$Input" = "2" ]; then
clear
ifconfig
echo "[*] 1 - Create a gnome-terminal"
echo "[*] 2 - Display Ifconfig"
echo -n "Pick an Option > "
read Input
fi
done
Doesn't reuse the Input variable, say if you want to run the ifconfig option multiple times in a row. It rechecks the first variable string to see if it matches "1", and when it doesn't it echoes back the list of options. Any ideas on how to make any input variable accessible regardless of when it is used?
The problem is that you're writing out the menu twice on each iteration. Stop doing that:
#!/bin/bash
while :
do
echo "[*] 1 - Create a gnome-terminal"
echo "[*] 2 - Display Ifconfig"
echo -n "Pick an Option > "
read Input
if [ "$Input" = "1" ]; then
gnome-terminal
sleep 3
fi
if [ "$Input" = "2" ]; then
clear
ifconfig
fi
done
Use select instead of rolling your own menu:
choices=(
"Create a gnome-terminal"
"Display Ifconfig"
)
PS3="Pick an Option > "
select action in "${choices[#]}"; do
case $action in
"${choices[0]}") gnome-terminal; sleep 3;;
"${choices[1]}") clear; ifconfig;;
*) echo invalid selection ;;
esac
done
select gives you an infinite loop by default. If you want to break out of it, put a break statement into the case branch.