Bash if else statement syntax error - bash

So I have this little problem. I'm not sure where it went wrong because I'm pretty sure I got the code right.
Here's the code:
#!/bin/bash
playerHP=100
echo "Hello World"
echo "HP: $playerHP"
echo "Continue? (Y/N):"
read -p $confirm
if [ "$confirm" = "y" ]
then
echo "Yes"
elif [ "$confirm" = "n" ]
then
echo "No"
else
echo "No such command"
fi
Here's the result:

Unrelated: read needs a prompt after -p. Blend the previous echo into it, and while at it, remove the $ from the variable name there.
read -p "Continue? (Y/N):" confirm
The error message is confusing. Don't you have MSWin line ends in the script?

Hi I have modified your script below use it. Working fine for me
#!/bin/bash
playerHP=100
echo "Hello World"
echo "HP: $playerHP"
read -p "Continue? (Y/N): " confirm
echo $confirm
if [ "$confirm" = "y" ]
then
echo "Yes"
elif [ "$confirm" = "n" ]
then
echo "No"
else
echo "No such command"
fi

Related

if else condition in shell script to check empty input by user

I am taking input from the user, and wants that if user give empty input , it should display the error message , I am trying below code , please correct me where I am wrong .
#!/bin/bash
echo "please enter previous version number"
read prevversion
echo "please enter new version number"
read newversion
echo "please enter environment [DEV2/QAT2/PPE2/PRD]"
read env
if[["$env"=" " &&"$prevversion"=" " &&"$newversion"= " "]]
then
echo "Please enter the details and try again"
else
if [ "$env" = "QAT2" ]
then
prevversion="$prevversion"_QAT2
echo "previous version is $prevversion"
newversion="$newversion"_QAT2
echo "new version is $newversion"
env="$env"
echo "enviornment is $env"
elif [ "$env" = "PPE2" ]
then
prevversion="$prevversion"
echo "previous version is $prevversion"
newversion="$newversion"
echo "new version is $newversion"
env="$env"
echo "enviornment is $env"
elif [ "$env" = "PRD" ]
then
prevversion="$prevversion"
echo "previous version is $prevversion"
newversion="$newversion"
echo "new version is $newversion"
env="$env"
echo "enviornment is $env"
elif [ "$env" = "DEV2" ]
then
prevversion="$prevversion"_DEV2
echo "previous version is $prevversion"
newversion="$newversion"_DEV2
echo "new version is $newversion"
env="$env"
echo "enviornment is $env"
fi
fi
please provide me with correct solution
your are using '&&' in your if-condition which means, that the error-message is only displayed when every(!) input is empty:
if[["$env"=" " &&"$prevversion"=" " &&"$newversion"= " "]]
You should change it like that:
if [ -z "${env}" ] || [ -z "${prevversion}" ] || [ -z "${newversion}" ]; then
echo "either one of the vars is empty"
exit 1
fi
You need to use ||, not &&. And you should be testing against an empty string, not a string with a single space.
if [[ "$env" = "" || "$prevversion" = "" || "$newversion" = "" ]]
Change your code in the following way:
if [ -z "$env" ] || [ -z "$prevversion" ] || [ -z "$newversion" ]; then
echo "either one of the vars is empty"
exit 1
fi
this will check that at least one of the tree var is empty.
If this is the case print your error message and exit the script with return code 1 to show that the script exit in error.
Use -z to check for an empty string, and don't repeat so much code.
#!/bin/bash
# Prompt for what should be command line arguments. If
# the script is going to be used in a batch environment to read
# from a stream, then prompting is unhelpful. If it is used
# in an interactive environment, it is easier to pass this
# data on the command line.
echo "please enter previous version number"
read prevversion
echo "please enter new version number"
read newversion
echo "please enter environment [DEV2/QAT2/PPE2/PRD]"
read env
if test -z "$env" || test -z "$prevversion" || test -z "$newversion"; then
echo "Please enter the details and try again" >&2
exit 1
fi
case "$env" in
QAT2)
prevversion="$prevversion"_QAT2
newversion="$newversion"_QAT2
;;
PPE2)
prevversion="$prevversion"
newversion="$newversion"
;;
PRD)
prevversion="$prevversion"
newversion="$newversion"
;;
DEV2)
prevversion="$prevversion"_DEV2
newversion="$newversion"_DEV2
;;
esac
echo "previous version is $prevversion"
echo "new version is $newversion"
echo "enviornment is $env"

Return to previous commands in bash script?

I'm trying to implement a prev option in my bash script to go back to the previous "menu", as well as a way for the script to ask for user input again if no variable is set for $name.
Heres my bash script:
#!/bin/bash
#Menu() {
for (( ; ; ))
do
beginORload=
echo "Choose option:"
echo "1 - Begin"
echo "2 - Load"
read -p "?" beginORload
#}
#Begin() {
if [ "$beginORload" -eq "1" ]
then
clear
for (( ; ; ))
do
echo "Beginning. What is your name?"
read -p "?" name
#If "prev" specified, go back to #Menu()
if [ "$name" -eq "prev" ]
then
Menu
fi
#If nothing specified, return to name input
if [ -z ${name+x} ]
then
Begin
else
break
fi
echo "Hi $name !"
done
fi
done
In batch, I could simply do:
:menu
echo Choose option:
echo 1 - Begin
echo 2 - Load
[...]
:begin
[...]
if "%name%==prev" goto menu
if "%name%==" goto begin
the issue is I keep running into errors all over the place, and I can't figure out what to type to get it to work
im running Yosemite btw. Thankyou
Something like this is close to what you expect:
while [[ $answer -ne '3' ]];do
echo "Choose option:"
echo "1 - Begin"
echo "2 - Load"
echo "3 - Exit"
read -p "Enter Answer [1-2-3]:" answer
case "$answer" in
1) while [[ "$nm" == '' ]];do read -p "What is your Name:" nm;done # Keep asking for a name if the name is empty == ''
if [[ $nm == "prev" ]];then nm=""; else echo "Hello $nm" && break; fi # break command breaks the while wrapper loop
;;
2) echo 'Load' ;;
3) echo 'exiting...' ;; # Number 3 causes while to quit.
*) echo "invalid selection - try again";; # Selection out of 1-2-3 , menu reloaded
esac # case closing
done # while closing
echo "Bye Bye!"
As a general idea you can wrap up your case selection in a while loop which will break under certain circumstances (i.e If Option 3 is selected or if a valid name is given (not blank - not prev)
PS1: In bash you compare integers with -eq , -ne, etc but you compare strings with == or !=
PS2: Check the above code online here

Bash programming looping in if and case statement

Why is it the code on "if" statement will keep looping if null is input to "title" variable but for the case statement, my script will have error?
echo -n "Title :"
read title
if [ -z "$title" ]; then
echo "Please input a title"
while [[ -z "$title" ]] ; do
echo -n "Title: "
read title
done
fi
read author
case "$author" in
*[0-9,\""\!##$%\(\)]*) echo "Please enter a name" ;;
while *[1-9,\""\!##$%\(\)]*)"$author" ]] ; do
echo -n "author "
read author
esac
The line
while *[1-9,\""\!##$%\(\)]*)"$author" ]] ; do
wouldn't be allowed there even if there weren't a bunch a syntax errors in it. Try this idiom instead:
title=
while [ -z "$title ]
do
read -p "What is the title? " title
done
You have \"" and should be only \" . That's the syntax error
Also, you can't put a While as a case. Try this and add any cases you want
while [ -z $author ]; do
echo "Please enter an author: "
read author
case "$author" in
*[0-9,\"\!##$%\(\)]*) echo "First case - $author"
;;
esac
done

Weird behavior using the read command in a infinite loop

I have a script that checks if a file exists or not using the ls command. If there is not a file I ask the user if he would like to continue with the script.
What I am finding is that the read command excepts input from the terminal instead of the keyboard?
Here is my script:
function isfileThere(){
output=$(ls ${1} 2>&1 >/dev/null)
case $output in
*"No such file or directory"*)
echo "DS not found: $output";
option_exitprog; $output >> DSNotFound.txt ;;
*) echo "DS found: $output";;
esac
}
function option_exitprog(){
while :
do
echo -n "Would you like to continue (y/n)?"
read Answer
#read -n1 -p "Would you like to continue (y/n)?" Answer
if [ ! -z "$Answer" ] ; then
if [ "$Answer" == "y" ] ; then
echo "Exiting script. Goodbye"
exit 1
elif [ "$Answer" == "n" ] ; then
echo "Continue With Program"
break
else
echo "We only accept (y/n)"
fi
else
echo "You have entered a null string. We only accept (y/n)"
fi
done
}
function get_TotalEventEntries(){
cat<<EOF
####################################
# #
# #
# get Total Entries #
# #
# #
####################################
EOF
while read LINE
do
let total_DSNumber=total_DSNumber+1
#Check if files exist
isfileThere ${FileDir}/*${LINE}*/*.root*
#print to file
#printf "${LINE}=" >> ${Filename}
#getEntries ${LINE} >> ${Filename}
done < ${DSWildCardFile}
echo "Finished running over $total_DSNumber file(s)"
}
get_TotalEventEntries
The problem is at this line: done < ${DSWildCardFile}. You cannot read lines from this file and read user at the same time with read and simple redirection . To fix it, use more complex redirection and a new file descriptor:
while read -u 3 LINE
do
...
done 3< ${DSWildCardFile}

Nested-If statements in Shell-scripting

This is my script:
echo "Name"
read name
if [ "$name" == "abcd" ]; then
echo "Password"
read password
if [ "$password == "pwd" ]; then
echo "Hello"
else
echo "Wrong password"
fi
else
echo "wrong username"
fi
And this is the output I get when I run it:
sh hello.sh
Name
abcd
hello.sh: line 14: unexpected EOF while looking for matching `"'
hello.sh: line 16: syntax error: unexpected end of file
Any idea whats wrong here? This could be a very silly one, but i wasted almost an hour on it.
if [ "$password == "pwd" ]; then
You have an unmatched " before $password
you can use case. Here's an example. You won't want to tell anybody which component is wrong, for security reason.
echo "Name"
read name
echo "Password"
read password
case "${name}${password}" in
"abcdpwd" )
echo "hello";;
*) echo "User or password is wrong";;
esac
In Shell script.
if condition
then
if condition
then
.....
..
do this
else
....
..
do this
fi
else
...
.....
do this
fi
if [ "$name" == "abcd" ];
then
echo "Password"
read password
if [ "$password" == "pwd" ];
then
echo "Hello"
else
echo "Wrong password"
fi
else
echo "wrong username"
fi

Resources