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
Related
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
I recently started writing BASH scripts, and I am currently trying to practice using while loops. However, when I run the following block of code, the command prompt responds with:
run.command: line 12: syntax error near unexpected token `done'
run.command: `done'
Then the program shuts off.
This is the code I am running.
#!/bin/bash
echo -e "text"
c=false
while true; do
printf ">> "
i=read
if [$i = "exit"]; then
exit
else if [$i = "no"]; then
echo "no"
else
echo -e "Error: $i is undefined"
fi
done
I did some research on while loops, however my loop syntax seems correct. When I remove the done at the end, and Unexpected end of file error occurs. Any help would be appreciated!
You can use the -p option of read for the prompt and a case ... esac construction:
while true; do
read -r -p ">> " i
case "$i" in
"exit") exit 0 ;;
"no") echo "no" ;;
*) echo -e "Error: $i is undefined";;
esac
done
I fixed it myself!
#!/bin/bash
echo -e "text"
c=false
while true; do
printf ">> "
read i
if [ "$i" = "exit" ]; then
exit
elif [ "$i" = "no" ]; then
echo "no"
else
echo -e "Error: $i is undefined"
fi
done
I'm quite new to mac terminal, not so much on bash, but I've been trying to correct these errors for a while, and I haven't been able of finding them.
This is my code:
#!/bin/bash
#change password of any mac user
# show users and select a valid one
Users=$(ls -1 /Users)
ok=0
while [ "$ok" == "0" ]; do
echo "Users"
echo "----------------"
echo $Users
echo "----------------"
echo "Select user"
read -e user
foreach i in $Users; do
if [ "$user" == "$i" ]; then
clear
echo 'User chosen:'
echo $user
echo "---------------"
ok=1
break
fi
done
if [ "$ok" == "0" ]; then
clear
echo "There is no such user"
echo 'Try again'
fi
done
# get password and comfirmation
password=0
password1=1
while [ "$password" != "$password1" ]; do
echo "Enter password"
read -e password
clear
echo "Confirm password"
read -e password1
clear
echo "Passwords are not the same"
echo "Try again"
done
clear
echo "Password saved"
echo "--------------"
# get version and change password
vrs=$(sw_vers -productVersion)
version=${vrs:0:4}
if [ $version -ge 10.7 ]; then
#LION
launchctl load /System/Library/LaunchDaemons/com.apple.opendirectoryd.plist
dscl . -passwd /Users/"$user" "$pass"
else
#SNOW LEOPARD
launchctl load /System/Library/LaunchDaemons/com.apple.DirectoryServices.plist
dscl . -passwd /Users/"$user" "$pass"
fi
echo "Password for user"
echo $user
echo "Succesfully changed"
echo "Press enter to end"
read -e end
exit 0
and the output that I get is:
:command not found
:command not found
mac.sh: line 65: syntax error: unexpected end of file
I've already checked my $PATH and its correct.
Also I've tried running instead of "sh mac.sh" "sh ./mac.sh"
Change foreach to for
Change $pass to $password, because that's where you saved the password
If it still doesn't work, then change the first line to #!/bin/bash -x, run again and paste the output into your question.
As a matter of fact, you shouldn't implement all the password input stuff. You should just let the dscl utility do that, like this:
dscl . -passwd /Users/"$user"
I would rewrite your script like this:
#!/bin/bash
userhomes=/Users
echo Users
echo "----------------"
ls -1 $userhomes
echo "----------------"
echo "Select user"
while :; do
read -e user
if test "$user" -a -d $userhomes/$user; then
clear
echo 'User chosen:'
echo $user
echo "---------------"
break
else
clear
echo "There is no such user"
echo 'Try again'
fi
done
vrs=$(sw_vers -productVersion)
case "$vrs" in
10.[7-9]*)
#LION
launchctl load /System/Library/LaunchDaemons/com.apple.opendirectoryd.plist
;;
*)
#older
launchctl load /System/Library/LaunchDaemons/com.apple.DirectoryServices.plist
;;
esac
dscl . -passwd $userhomes/"$user"
One problem is the syntax of your foreach loop. Bash doesn't have a foreach command, but it does have for loops that do the same thing:
for i in $Users ; do
#do something
done;
I'm getting the following error:
./adduser.sh: line 21: syntax error near unexpected token `else'
./adduser.sh: line 21: ` else'
I have been stuck here for an hour and I just can not figure it out.
#!/bin/bash
#==========================================================================================================================================
# Script Name: adduser.sh
# By: Tim mayo
# Date: 3/2013
# Purpose: Add a user to the Linux system
# Command line: adduser.sh
#
#==========================================================================================================================================
read -p "Enter username : " username
read -s -p "Enter password : " password
egrep "^$username" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo "$username exists!"
exit 1
else
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
useradd -m -p $pass $username
[ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
fi
else
echo "Root user can only add a user to the Linux system"
exit 2
fi
The else keyword isn't associated with any if statement; you ended the if statement with the fi keyword on line 20. Perhaps you are missing an if just before the two read statements?
Another 'run as root only' example:
#!/bin/bash
if [ $UID -eq "0" ]
then
read -p "Enter username : " username
read -s -p "Enter password : " password
egrep "^$username" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo "$username exists!"
exit 1
else
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
useradd -m -p $pass $username
[ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
fi
else
echo "Root user can only add a user to the Linux system"
exit 2
fi
I have a shell script variable
$a = "Hello i am pass"
now i want to search for "pass" in variable $a.
if ["$a" == "pass"]; then
echo "`Success`"
else
echo "`fail`"
fi
Please give me shell script for searching pass keyword to use in above code.
Try with this,
#!/bin/bash
a="Hello i am pass";
if [ `echo $a | grep -c "pass" ` -gt 0 ]
then
echo "Success"
else
echo "Fail";
fi
flag=`echo $a|awk '{print match($0,"pass")}'`;
if [ $flag -gt 0 ];then
echo "Success";
else
echo "fail";
fi
Here is a more compact way to write this:
echo "$a" | grep "pass" && echo "Found." || echo "Not found."
You can use braces to put in multiple instructions instead of a simple echo:
echo "$a" | grep "pass" && {echo "Found.";exit 0} || {echo "Not found.";exit 1}
today i used extended choice parameter and using execute shell for the string match.
if someone will find same problem anyone can use this
if [ `echo $test| grep -c "abc" ` -gt 0 ]
then
echo "Success"
else
echo "Fail";
fi
Try this
a="Hello I am Pass";
a1="Hello, Passed my First Attempt"
a2="Passed in First Attempt"
if [[ ${a,,} =~ 'pass' ]]; then echo 'Success'; else echo 'First Attempt in Learning'; fi
Short command:
OPC="test"
PROD="This is a test"
[[ ${PROD,,} =~ $OPC ]] && echo -n 1 || echo -n 0
The answer will be "1" when find "OPC" in "PROD".