I have trying to have a unix shell script ask the user whether they want to delete an item.
I have the following code
I keep getting the following error
./menu.sh 73: [: missing ]
echo "Confirm deletion: (y)es or (n)o: "
read confirmDeletion
if [ "$confirmDeletion"="y"];
then
echo "YES"
else
echo "NO"
pause
fi
I cannot seem to work out what is wrong
Any help will be appreciated
Thanks
You need spaces around the = operator:
if [ "$conformDeletion" = "y" ];
and before the ]
Related
I'm having issues with a script I'm writing in bash with regards to backing up or restoring. What I'm trying to do is check for parameters and then if none are presented, loop until a name is provided or they quit. I can check for parameters and loop to quit but the problem I am having is getting the user input and then using that for the backup file name. Here's my script so far, can someone advise on how to loop for filename/q and how to get said filename input to work with the rest of the script?
#!/bin/bash
# Purpose - Backup world directory
# Author - Angus
# Version - 1.0
FILENAME=$1.tar.gz
SRCDIR=World
DESDIR=backupfolder
if [ $# -eq 0 ]
then
echo "No filename detected. To use this utility a filename is
required. Usage:tarscript filename"
else
echo "The filename to be used will be $filename"
fi
while [ $# -eq 0 ]
do
echo "Please provide a filename or press q to exit."
read response
if [ $response == 'q' ]
then
exit
else [ $response == '$FILENAME' ]
echo -n 'Would you like to backup or restore? (B/R)'
read response
if [ $response == 'B' ]
then
tar cvpzf $DESDIR/$FILENAME $SRCDIR
echo 'Backup completed'
exit
fi
fi
done
I finally managed to get it working in the end. I realised what my mistakes were thanks to Jens and changed things enough that it now responds to input and supplied parameters. Of course the code is nearly twice as big now with all my changes but hey ho.
I'm trying to make a shell script (.sh) on my mac, but somehow I can't get the 'read' function to work. This is my code:
READ -p "What now? " var
ECHO $var
ECHO " "
if [ "$var" == "getfiles" ]
then
ECHO "Test1"
elif [ "$var" == "help" ]
then
ECHO "Test2"
else
ECHO "Please enter a valid command (type 'help' for a list of valid commands)"
fi
As you can see, I'm trying to get user input by using 'READ -p "What now? " var'. However, no matter what I type, it returns an empty variable. I've searched everywhere, but no one seems to have the same problem as I do. I checked the code a thousand times, but can't find any irregularities. Can anyone see what I'm doing wrong? Thanks in advance.
Use lowercase read and echo.
echo "${var}"is better than echo $var
I'm trying to write a bash script to convert a user inputted answer to MD5 and compare it to the MD5 hash I already have to see if it's the same, but I'm getting a syntax error
Syntax error near unexpected token 'fi'
The code:
#!/bin/bash
# Check if you're right!
declare -x answer=6f22bf74918703932091343ba9c64402
echo "Enter your answer, follow by [ENTER]:"
read userAnswer
md5sum userAnswer > hashedAnswer
if [ answer == hashedAnswer ]
then
echo "Correct! You're right! It was him! Maybe I shou----"
echo "-You get run over, silenced before you can present your proof-"
else
echo "Wrong, try again!"
fi
Any help would be greatly appreciated.
Change these two lines:
md5sum userAnswer > hashedAnswer
if [ answer == hashedAnswer ]
to:
hashedAnswer=$(printf "%s" "$userAnswer" |md5sum)
if [ "$answer" == "$hashedAnswer" ]
Example:
#!/bin/bash
# Check if you're right!
answer="b326b5062b2f0e69046810717534cb09 -"
echo "Enter your answer, follow by [ENTER]:"
read userAnswer
hashedAnswer=$(printf "%s" "$userAnswer" |md5sum)
if [ "$answer" == "$hashedAnswer" ]
then
echo "Correct! You're right! It was him! Maybe I shou----"
echo "-You get run over, silenced before you can present your proof-"
else
echo "Wrong, try again!"
fi
N.B: the answer here is true
I am trying to write a shell script that will either start or stop openvpn, depending on what the user enters. When I run the script it correctly prompts me for what I want to do. But when I type "1" and hit [Enter] it outputs this and terminates:
./control-openvpn.sh: 27: ./control-openvpn.sh: Syntax error: "fi" unexpected
Here is the main part of my code. The functions are above it in the script.
# Main
echo -n "What to do? 1.Start or 2.Stop. [1/2]"
read action
if [ "$action" == "1" ]
then
start_openvpn()
elif [ "$action" == "2" ]
then
stop_openvpn()
fi
Thank you in advance
In bash, when you do start_openvpn(), you are declaring a new function. Thus, bash gets confused when the next thing it sees is fi. Something like this should work for you:
read -p 'What to do? 1.Start or 2.Stop. [1/2] ' action
if [ $action -eq 1 ]; then
start_openvpn
elif [ $action -eq 2 ]; then
stop_openvpn
fi
I have written this script as a module in installing postgres and this is to show the user that the database has been created and get his/her input as to they see the database was created. When I run the script, I get the error
./dbtest: line 39: [: missing `]'
I have tried adding " " around yes and '' around yes and I can't figure out what is missing. The script is as follows
#
#
# Check to make sure the database was created and who is the owner
#
#
if [ -f showdb ];
then
cp showdb /home
else
echo " show database file is missing "
fi
if [ -f /home/showdb ];
then
su - postgres -c '/home/showdb'
echo " Do you see the data name created listed above? "
echo " "
echo " Type yes or no (type out the whole word please) "
echo " "
read dbawr
if [ $dbawr == yes && $dbawr == Yes ];
then
echo "Great!"
exit
else
echo " Please contact tech support "
pause " Press [CTRL] [Z] to exit the program now "
fi
else
echo " File Missing!!"
fi
What am I missing in this script?
Thanks!
You can't use the boolean && operator withing the single brackets conditional [ test = test ]. If you're using bash (or a similar shell), the preferred syntax is to use the double brackets:
[[ this == this && that == that ]]
If you're worried about portability, then you should stick with the single brackets, but use them like so:
[ this = this ] && [ that = that ]
Note that I didn't use the double equals (==). That's not posix compliant either.