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
Related
I have a problem with this bash script:
#!/bin/bash
G=100
echo $G
main_menu()
{
while :
do
clear
echo "Select from menu"
echo "[1] Press 1 to show savings"
echo "[2] Press 2 to withdraw savings"
echo "[3] Press 3 to exit"
echo "Enter your menu choice [1-3]: \c "
read -r m_menu
case "$m_menu" in
1) option_1;;
2) option_2;;
3) exit 0;;
*) echo "\nERROR: Please select a valid menu choice";
echo "Press ENTER To Continue..." ; read ;;
esac
done
}
option_1()
{
clear
echo "Your balance is $G"
echo "\nPress ENTER to return to menu..."
read
return
}
option_2()
{
clear
echo "Withdraw savings"
read -rp "Enter amount to withdraw: " num
if [ $num -le $G ]; then
answer=$(echo $(( G - num )))
echo "Your new balance is: $answer"
echo "$answer" | tee "$G"
elif [ $num -gt $G ]; then
echo "No: not eough money in your balance"
fi
read
return
}
main_menu
The problem is the following: how do I make sure that once I withdraw savings, my saving count will be updated? Because if I withdraw $90, obviously when I return to the balance it should be $10 and yet this doesn't work for me (it still says saving balance is $100)
What can I do?
Thank you (sorry for my poor English)
You have to update G in your code.
if [ $num -le $G ]; then
G=$(( G - num ))
echo "Your new balance is: $G"
Here is what I am talking about how do I get the script to jump back to the first echo statement if No is selected, Thanks?
echo "Please enter some input: "
read input_variable
echo "You entered: $input_variable"
read -r -p "Is this Correct? [y/N] " response
case $response in
[yY][eE][sS]|[yY]
do_something
;;
*)
{return to first line - echo "Please enter some input: "}
;;
esac
Wrap your prompts in a while loop that you only break out of once the input is confirmed:
while :; do # same as: `while true; do` - keep looping until exited with `break`
echo "Please enter some input: "
read -r input_variable
echo "You entered: $input_variable"
read -r -p "Is this Correct? [y/N] " response
case $response in
[yY][eE][sS]|[yY])
break
;;
esac
done
do_something
Note that not only N, but any input other than what is matched by your case handler will cause the loop to remain active.
use a loop around the whole code:
quit=0
while [ $quit == 0 ]; do
echo "Please enter some input: "
read input_variable
echo "You entered: $input_variable"
read -r -p "Is this Correct? [y/N] " response
case $response in
[yY])
echo "do_something"
quit=1
;;
*)
;;
esac
done
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
I want to make an argument as optional in getopt bash so that if the user didn't specify it, then it still runs without killing the program. How can i do that. Here is my previous code
while getopts ":l:q:s:e:hg:" opt; do
case $opt in
l)
lincRNAfasta=$OPTARG
;;
q)
query_species=$OPTARG
;;
s)
subject_species=$OPTARG
;;
e)
subject_gff=$OPTARG
;;
h)
echo "USAGE : open script in text editor"
exit 1
;;
g)
subject_genome=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
Sorry, had to update my answer, I misunderstood the question.
I was looking at the documentation for getopts.c which does support ::
But the code that you have works whether I specify 1, 2, or 3 arguments, and in any order.
If it is exiting with an error one reason could be that the variables need to be defined:
#! /bin/bash
lincRNAfasta=
query_species=
subject_species=
subject_gff=
subject_genome=
help='''
USAGE : open script in text editor
-l lincRNAfasta
-q query_species
-s subject_species
-e subject_gff
-g subject_genome
'''
while getopts ":l:q:s:e:hg:" opt; do
case $opt in
l)
lincRNAfasta=$OPTARG
;;
q)
query_species=$OPTARG
;;
s)
subject_species=$OPTARG
;;
e)
subject_gff=$OPTARG
;;
h)
printf "$help"
exit 1
;;
g)
subject_genome=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
echo "doing something else"
if [ -z "$lincRNAfasta" ];then
echo "its empty"
echo "lincRNAfasta: $lincRNAfasta"
echo
else
echo "not empty"
echo "lincRNAfasta: $lincRNAfasta"
echo
fi
if [ -z "$query_species" ];then
echo "its empty"
echo "query_species: $query_species"
echo
else
echo "not empty"
echo "query_species: $query_species"
echo
fi
if [ -z "$subject_species" ];then
echo "its empty"
echo "subject_species: $subject_species"
echo
else
echo "not empty"
echo "subject_species: $subject_species"
echo
fi
if [ -z "$subject_gff" ];then
echo "its empty"
echo "subject_gff: $subject_gff"
echo
else
echo "not empty"
echo "subject_gff: $subject_gff"
echo
fi
if [ -z "$subject_genome" ];then
echo "its empty"
echo "subject_genome: $subject_genome"
echo
else
echo "not empty"
echo "subject_genome: $subject_genome"
echo
fi
# do something once variables have been set
# any variable not set will be empty
echo "doing something else"
Output:
bob#squids:~/Desktop$ ./1.sh -h
USAGE : open script in text editor
-l lincRNAfasta
-q query_species
-s subject_species
-e subject_gff
-g subject_genome
bob#squids:~/Desktop$ ./1.sh -s a -g h -e e -q q
doing something else
its empty
lincRNAfasta:
not empty
query_species: q
not empty
subject_species: a
not empty
subject_gff: e
not empty
subject_genome: h
doing something else
bob#squids:~/Desktop$ ./1.sh -s a -g h -e e
doing something else
its empty
lincRNAfasta:
its empty
query_species:
not empty
subject_species: a
not empty
subject_gff: e
not empty
subject_genome: h
doing something else
bob#squids:~/Desktop$ ./1.sh -s a -e e -g 123
doing something else
its empty
lincRNAfasta:
its empty
query_species:
not empty
subject_species: a
not empty
subject_gff: e
not empty
subject_genome: 123
doing something else
Or instead of initializing the variables as empty, you could initialize them as a string like "NOTSPECIFIEDONSTART". And when starting the script you could pass an empty string like -g ''
I have an annoying issue that seems to cause and infinite loop and I can't work out why. If I call the following function, it keeps repeating the yes/no options infinitely down the screen until I crash out.
AuditUpload() {
clear
echo "Audit report generated successfully"
echo " "
echo "Do you wish to upload qhub_audit.csv? (1 = Yes/2 = No):"
sleep 1
select yn in "Yes" "No"; do
case $yn in
Yes ) AuditUploader; Auditvi; exit;;
No ) echo "Upload cancelled"; Auditvi; exit;;
esac
done
}
I put the sleep in to see if it would remedy the issue but it still does the same. This issue seems to be very intermittent and doesn't happen every time. This script is written in korn shell (ksh).
AuditUploader function:
AuditUploader() {
echo "Uploading qhub_audit.csv to $HOST..."
curl -v -T qhub_audit.csv -# ftp://xxxxxxxx:xxxxxxxxx#xxxxxxxxxxxxx.com/
if [ "$?" -ne "0" ]
then
echo "ERROR: Cannot upload qhubload.csv"
exit
else
clear
echo "qhub_audit.csv has been put on $HOST successfully"
tput cup 5 5
echo "Copy and paste this link into internet explorer to download:"
tput cup 7 5
echo "ftp://xxxxxxxx:xxxxxxxxx#xxxxxxxxxxxxx.com/qhub_audit.csv"
read LINK
fi
}
Auditvi function:
Auditvi() {
clear
echo "Do you wish to view qhub_audit.csv? (1 = Yes/2 = No):"
sleep 1
select yn in "Yes" "No"; do
case $yn in
Yes ) vi qhub_audit.csv; exit;;
No ) exit;;
esac
done
}
After a bit of playing around it looks like it was looping whenever the 'curl' command returned a specific error which stopped the kill $$ from working properly. I replaced kill $$ with exit 1 and amended the other functions accordingly. I also put in a contingency to use kermit in case the FTP failed. Anyway, this is what my code looks like now:
#########################################
# Upload quotehub audit report function #
#########################################
AuditUploader() {
echo "Uploading qhub_audit.csv to $HOST..."
curl -v -T qhub_audit.csv -# ftp://$USER:$PASSWD#$HOST/ -m 10
if [ "$?" -ne "0" ]
then
echo "ERROR: Cannot upload qhubload.csv via FTP"
if [ ${term} = "tty1A" ]
then
echo "Attempting to download to modems server..."
wermit -s qhub_audit.csv
if [ $? -ne 0 ]
then
echo "Cannot upload to modems either!"
echo "This file will have to be downloaded manually"
exit 1
else
clear
echo "qhub_audit.csv has been put on modems server successfully"
tput cup 5 5
echo "Copy and paste this link into START -> RUN to download:"
tput cup 7 5
echo "\\\\\\xxxxxxxx\download\general\qhub_audit.csv"
read LINK
fi
else
echo "Upload failed!"
exit 1
fi
else
clear
echo "qhub_audit.csv has been put on $HOST successfully"
tput cup 5 5
echo "Copy and paste this link into internet explorer to download:"
tput cup 7 5
echo "ftp://$USER:$PASSWD#$HOST/qhub_audit.csv"
read LINK
fi
}
#######################################################
# Function to prompt user to upload qhub audit report #
#######################################################
AuditUpload() {
clear
echo "Audit report generated successfully"
echo ""
echo "Do you wish to upload qhub_audit.csv? (y/n):"
read REPLY
case "$REPLY" in
Y) AuditUploader; Auditvi; exit;;
y) AuditUploader; Auditvi; exit;;
N) Auditvi; exit;;
n) Auditvi; exit;;
*) echo "invalid option";;
esac
}
######################################
# Function to view qhub audit report #
######################################
Auditvi() {
if [ "$?" -ne "0" ]
then
exit 1
else
clear
echo "Do you wish to view qhub_audit.csv? (y/n):"
read REPLY
case "$REPLY" in
Y) vi qhub_audit.csv; exit;;
y) vi qhub_audit.csv; exit;;
N) exit;;
n) exit;;
*) echo "invalid option"; Pause; Auditvi;;
esac
fi
}
Thanks again guys for all your help.