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
Related
When I run this script the first question it asks is "Would you like to use curl? [Y/N]:" to which my reply is often Y or y. However, when I do that the instant output I get is "Unknown!". I'm expecting to see the next question from the code which is "Great, do you want to ignore certificates [Y/N]: "
Can anyone edit my code to make it work as expected? And tell me why?
#!/bin/bash
echo "Build command"
read -r -e -p "Would you like to use curl? [Y/N]: "
curlstring="curl"
if [[ "${input,,}" == "y" ]]; then
read -r -e -p "Great, do you want to ignore certificates [Y/N]: " input
if [[ "$input" == "y" ]]; then
curlstring=$curlstring" -k"
else
curlstring=$curlstring" "
fi
echo "$curlstring"
elif [[ "${input,,}" == "n" ]]; then
echo "Bye"
else
echo "Unknown!"
exit 0
fi
The error is your are not actually capturing the user input from the first read command, change it,
read -r -e -p "Would you like to use curl? [Y/N]: " input
Also bash is quite sensitive about the constructs used, the line number 15
fi echo "$curlstring"
will likely throw a syntax error saying below when you give a Y or a y as one of the options,
Would you like to use curl? [Y/N]: Y
script.sh: line 15: syntax error near unexpected token `echo'
script.sh: line 15: ` fi echo "$curlstring" '
change it to separate lines.
I have been trying to make a shell script in bash that will display the following:
You are the super user (When I run the script as root).
You are the user: "user" (When I run the script as a user).
#!/bin/bash/
if { whoami | grep "root" }; then
echo $USER1
else
echo $USER2
fi
I keep recieving these syntax error messages:
script.sh: line 2: syntax error near unexpected token `then'
script.sh: line 2: `if { whoami | grep "root" }; then'
Could someone help me out?
If braces are used to chain commands then the last command must have a command separator after it.
{ foo ; bar ; }
userType="$(whoami)"
if [ "$userType" = "root" ]; then
echo "$USER1"
else
echo "$USER2"
fi
pay attention with your first line, the correct syntax for she-bang is:
#!/bin/bash
everything you put there, is the interpreter of your script, you can also put something like #!/usr/bin/python for python scripts, but your question is about the if statement, so you can do this in two ways in shell script using
if [ test ] ; then doSomething(); fi
or
if (( test )) ; then doSomething(); fi
so to answer your question basically you need to do this
#!/bin/bash
if [ `id -u` -eq 0 ] ; then
echo "you are root sir";
else
echo "you are a normal user"
fi
if (( "$USER" = "root" )); then
echo "you are root sir";
else
echo "you are a normal user"
fi
note that you could use a command using `cmd` or $(cmd) and compare using -eq (equal) or = (same), hope this help you :-)
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 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 ]
I'm rather new to bash scripting, and Google isn't as useful as I'd like for this. I'm just playing around with a little password entry program in my .bash_profile and have something like this:
read PASSWORD
if $PASSWORD != 'pass'; then
echo "wrong. exiting"
exit
fi
Unfortunately, this doesn't work. I get these errors (darwin on 10.6)...
EDIT Sorry about this posting. My browser crashed and I didn't even realize this posted. I ended up figuring it out on my own – again sorry. But thanks for the answers!
You are missing square brackets. The if line should be:
if [ $PASSWORD != 'pass' ]; then
or even better:
if [ "$PASSWORD" != 'pass' ]; then
Which will avoid failure if $PASSWORD is empty.
Try:
read PASSWORD
if [ "x$PASSWORD" != "xpass" ] ; then
echo "Wrong. Exiting."
exit 1
fi
exit 0
You might like to know about two options to the read command:
-p string
Display a prompt without a trailing newline
and
-s
Silent mode. The characters typed by the user are not echoed to the screen.
So for prompting for a password you could do:
read -sp "Please enter your password: " PASSWORD
echo
This is an excellent resource.
use case/esac construct
read -p "enter: " PASSWORD
case "$PASSWORD" in
"pass") echo "ok;;
* ) echo "not ok";;
esac
Edit: For Dennis's qns
x=10
y=5
z=1
options=3
expression="$((x> y)) $((y> z)) $((options<=4))"
case "$expression" in
"1 1 1")
echo "x > y and y>z and options <=4";;
*) echo "Not valid";;
esac