How to Properly Set Up If in While Loop - linux

I'm going to add the rest of the functionality to the program later it's still in it's early stages, but I cant seem to get out of the while loop for some reason even though the if statement is within it and under the menu. Here's the code so far.
continue="true"
#while loop to keep the code running till the user inputs Q
while [ $continue = "true" ]
#loop start
do
#clearing screen before showing the menu
clear
echo "A to Create a user account"
echo "B to Delete a user account"
echo "C to Change Supplementary Group for a user account"
echo "D to Create a user account"
echo "E to Delete a user account"
echo "F to Change Supplementary Group for a user account"
echo "Q to Quit"
read -p "What would you like to do?:" choice
#Test to end the program
if [ $choice = 'Q' ] || [ $choice = 'q']
then
$continue="false"
fi
#loop end
done```

As pointed out by Gordon, you need continue="false" not $continue="false".
Also, I advise using if [ "$choice" = 'Q' ] || [ "$choice" = 'q' ] so your script won't break if the user hits CR and did not enter anything. (Also note that you need a space before last ] in that statement.)

Although the syntax error has already been highlighted in the comments, I would suggest avoiding the continue variable entirely by using break. And you can also combine the two checks using a regex. Something like this
while true; do
read -p "What would you like to do?: " choice
if [[ "$choice" =~ [Q|q] ]]; then
break
fi
done
Though when I look at the echo statements in your question it seems like you would probably be better off avoiding the if entirely, and instead use a case statement
while true; do
read -p "What would you like to do?: " choice
case "$choice" in
q|Q) break
;;
a|A) echo "Creating a user account"
;;
#This catches anything else
*) echo "unknown option"
;;
esac
done

Related

Bash script that allows one word as user input

Made a script that the user gives a "parameter" and it prints out if it is a file, directory or non of them. This is it :
#!/bin/bash
read parametros
for filename in *
do
if [ -f "$parametros" ];
then
echo "$parametros is a file"
elif [ -d "$parametros" ];
then
echo "$parametros is a directory"
else
echo " There is not such file or directory"
fi
exit
done
Altough i want the user to be allowed to give only one word as a parameter. How do i make this happen ? (For example if user press space after first word there would be an error message showing "wrong input")
#!/bin/bash
read parametros
if [[ "$parametros" = *[[:space:]]* ]]
then
echo "wrong input"
elif [[ -f "$parametros" ]]
then
echo "$parametros is a file"
elif [[ -d "$parametros" ]]
then
echo "$parametros is a directory"
else
echo " There is not such file or directory"
fi
See http://mywiki.wooledge.org/BashFAQ/031 for the difference between [...] and [[...]].
You have to use the $#. It gives the number of the parameters.
The code will be something like:
if [ "$#" -ne 1 ]; then
printf 'ERROR!\n'
exit 1
fi
First, I'm curious why you want to restrict to one word - a file or directory could have spaces in it, but maybe you are preventing that somehow in your context.
Here are a few ways you could approach it:
Validate the input after they enter it - check if it has any spaces, eg: if [[ "parametros" == *" " ]]; then...
Get one character at a time in a while loop, eg with: read -n1 char
Show an error if it's a space
Break the loop if it's 'enter'
Build up the overall string from the entered characters
1 is obviously much simpler, but maybe 2 is worth the effort for the instant feedback that you are hoping for?

Yes or No in Bash Script

I have a set of 100 questions. My requirement is when a user enter "yes", then question 1 should appear. If not, directly it go to question 2. Like that it should go on till 100 questions. Any lead would be appreciated.
This is what I tried, but it is failing.
#!/bin/bash
echo "Execute question1 "
select result in Yes No
do
echo "How to see apache config file"
exit
done
echo "execute question2"
select result in Yes No Cancel
do
echo "Command for listing processes"
exit
done
Thanks in advance
Here is a way to do this with an array.
#!/bin/bash
questions=(
"How to see apache config file"
"Command for listing processes"
"My hovercraft is full of eels"
)
for((q=0; q<${#questions[#]}; q++)); do
echo "Execute question $q?"
select result in Yes No; do
case $result in
Yes)
echo "${questions[q]}";;
esac
break
done
done
Using select for this seems rather clumsy, though. Perhaps just replace it with
read -p "Execute question $q? " -r result
case $result in
[Yy]*) echo "${questions[q]}";;
esac
Having just a list of questions still seems weird. With Bash 5+ you could have an associative array, or you could have a parallel array with the same indices with answers to the questions. But keeping each question and answer pair together in the source would make the most sense. Maybe loop over questions and answers and assign every other one to an answers array, and only increment the index when you have read a pair?
pairs=(
"How to see Apache config file"
"cat /etc/httpd.conf"
"Command for listing processes"
"ps"
"My hovercraft is full of what?"
"eels"
)
questions=()
answers=()
for((i=0; i<=${#pairs[#]}/2; ++i)); do
questions+=("${pairs[i*2]}")
answers+=("${pairs[1+i*2]}")
done
This ends up with two copies of everything, so if you are really strapped for memory, maybe refactor to just a for loop over the strings and get rid of the pairs array which is only useful during initialization.
Use an array of questions and loop over it, like this:
#!/bin/bash
n=1
questions=(
'How to see apache config file'
'Command for listing processes'
)
check_user_input(){
read -p "y/n " input
case $input in
[Yy]*) return 0;;
[Nn]*) return 1;;
*) check_user_input;;
esac
}
for question in "${questions[#]}"; {
echo "Execute question $n"
check_user_input && echo "$question"
((n++))
}
Here is a straight forward example. Play with it.
#!/bin/bash
echo "Type 'y' for yes, 'n' to skip or 'q' to quit and press Enter!"
for((i=1; i < 101; ++i)); do
echo 'Execute question '$i
while read user_input; do
if [[ "$user_input" = 'q' ]]; then
break 2
elif [[ "$user_input" = 'n' ]]; then
break
elif [[ $i -eq 1 ]]; then
echo 'How to see apache config file?'
break 2 # Change from "break 2" to "break" for the next question.
elif [[ $i -eq 2 ]]; then
echo 'Command for listing processes.'
break 2 # Change from "break 2" to "break" for the next question.
else
echo "Wrong input: $user_input"
echo "Type 'y' for yes, 'n' to skip or 'q' to quit and press Enter!"
fi
done
done
echo 'Finished'

Optional commands in UNIX

I'm trying to create a session in unix that will allow my to ask a simple question and then ask different questions varying on the answer.
For example if I were to ask
'Enter a choice (quit/order)'
if 'quit' is entered then program should close
if 'order' is entered then the program should continue asking further questions.
If you can help that would be great! Thanks!
#!/bin/bash
echo "Lots of choices.."
read -p "What is your choice? " choice
echo "Your choice was $choice"
if [ $choice == "quit" ]
then
echo "Exiting.."; exit 0
fi
if [ $choice == "order" ]
then
echo "Doing some other stuff.."
fi
This is where the shell's select command comes in handy. I'm going to assume you're using bash
PS3="Enter a choice: "
select answer in Quit Order; do
case $answer in
Quit) echo "Thanks for playing."; exit ;;
Order)
# select is like an infinite loop: you need to break out of it
break
;;
*) echo "Use the numbers to select your answer." ;;
esac
done
# carry on with the next question

Don't want to fun function if parameters are entered on the command line

I've been looking around but can't find a solution...
Basically, I have a program that creates user accounts, and I can also create an account using the command line interface.
What I want, is when I use the command line, the rest of the program doesn't run. Because at the moment, it creates the account, then returns to the menu.
function menu {
echo "Welcome to OP-SYS Account creation. Please choose which mode you would like to continue in."
echo
echo "[1] Basic Account Creation"
echo "[2] Advanced Account Creation"
echo "[3] Exit"
echo
#Change user input to functions, reject others
read CHOICE #Load user input to variable
if [ "$CHOICE" = 1 ] || [ "$CHOICE" = 2 ] || [ "$CHOICE" = 3 ]; then
#Goto function opt#
opt"$CHOICE"
else
echo "That is not a valid entry. Returning you to the menu."
sleep 2
clear
menu
fi
}
#other code#
konsoleend() {
clear
#popd
}
username=$1
firstname=$2
surname=$3
password=$4
echo "$firstname" "$surname" >> "$username".log
echo "$password" >> "$username".log
curdate=$(date +'%d/%m/%Y %H:%M:%S')
echo "$curdate" >> "$username".log
#menu
konsoleend
menu
It's not at all clear what you mean by the distinction between a
"command line interface" and a "program", but perhaps you simply mean that if no arguments are given to the program you want to use the menu. If that is the case, simply write:
test $# = 0 && menu
in the last line of the script rather than just menu

Bash if string = this or that [duplicate]

This question already has answers here:
How do I prompt for Yes/No/Cancel input in a Linux shell script?
(37 answers)
Closed 2 years ago.
Trying to write a script which will read what a user has imput... I know it's basic but im stuck on the first if..
echo "Please enter yes or no (y/n)?"
read string
if [ $string = "y" -o "n" ]
then
echo "User selected $string"
else
echo "You didn't enter y/n !!!"
fi
I would like it to be if [ $backup = "y" or "n" ]
Any ideas?
Use this syntax in bash :
if [ "a string" = "another one" ] ; then
# Whatever
fi
For multiple conditional statements such as OR, use:
if [ "a string" = "another one" ] || [ "$foo" = "bar" ] ; then
# Whatever
fi
bash also supports the non-standard [[ ... ]] expression, which can process a compound comparison using a single command, rather than 2 [ commands:
if [[ "a string" = "another one" || $foo = "bar" ]]; then
# Whatever
fi
Not the question you actually asked, but... You told the user to enter "yes" or "no" but only test for y or n - sure, you gave them a hint but users are hint-resistant. So maybe a looser test is in order:
echo "Please enter yes or no (y/n)"
read string
case "$string" in
[yY]* | [nN]*) echo "User entered $string" ;;
*) echo "I don't understand '$string'" ;;
esac
That will recognize any variation that begins with Y or N - usually that's good enough, but you could tighten up the tests. Also, since you'll probably want to do something different with a yes or no response you can expand the case (I've also tightened the tests in this one):
case "$string" in
[yY] | [yY][eE][sS]) echo "Here's where you process yes" ;;
[nN] | [nN][oO]) echo "And here you deal with no" ;;
*) echo "I don't understand '$string'" ;;
esac
You could do this with if statements but I find case more readable when more than two alternatives are possible and the test is appropriate to case syntax.
You can also try:
echo "Please enter yes or no (y/n)?"
read string
if [[ "$string" =~ ^(y|n)$ ]]
then
echo "User selected $string"
else
echo "You didn't enter y/n !!!"
fi
I nice solution is would be with case, which is easier to extend if you want to make your input more complex
case $string in
y|n) echo "User selected $string"
;;
*) echo "You didn't enter y/n !!!"
;;
esac
From there you can easily modify it to accept Uppercase or whatever:
case $string in
y|Y) echo "yes, Sir!"
;;
n|N) echo "No, can't do"
;;
*) echo "Say what?"
;;
esac
Check case statements for more info.

Resources