Im running Chrome OS in dev mode, and I am creating a bash file. The code looks like so:
echo -n "Username: "
read username: "
if [ $username == "mycroft" ]; then
echo "Correct!"
fi
My problem is that when I run the code in crosh with /bin/sh ./login.sh, after I type
in mycroft, I get this error:
[: 4: mycroft: unexpected operator
What did I do wrong?
Your problem is on the second line
echo -n "Username: "
read username
if [ $username = "mycroft" ]; then
echo "Correct!"
fi
Related
Hello guys i am new in shell scripting and I tried run the script below but have following error message:
#!/bin/bash
echo "Enter the name of the image whose container you want to run: "
read container
echo "Enter a name for this container: "
read name
echo "do you want to run in detatch mode? "
read d
if [ $d -eq yes ]
then
docker run --name $name -P -d $container
elif [ $d -eq no ]
then
docker run --name $name -P $container
else
echo "invalid input"
fi
This produces the following error messages:
./main.sh: line 9: [: yes: integer expression expected
./main.sh: line 12: [: yes: integer expression expected
-eq compares two integers
use = in order to compare two strings
for more info please refer to: https://stackoverflow.com/a/20449556/9881735
for example:
[ $d = "yes" ]
I'm trying to create a bash script that asks for password when you try to see the password file, but I'm stucked. This is my code:
#!/bin/bash
# Read Password
echo -n Password:
read -s PASSWORD
passwords() {
echo "
PASSWORDS
"
}
if [ "$PASSWORD"="root" ]; then
passwords
exit
else
echo "Wrong password"
exit
fi
I've tried a lot of things, for example if [ "$PASSWORD"!="root" ] instead of else but none of them worked.
Here is a shorter version:
#!/bin/bash
passwords(){
echo "PASSWORDS"
}
## Read Password
read -p "Enter password: " -s PASSWORD
desired_password="root"
[ "$PASSWORD" == "$desired_password" ] && passwords || echo "Wrong password"
As #vdavid said, you can add a space around the equal sign or even better, as you have bash shell, it is recommended to use double-bracket for your if statement. Check this: Is there any difference between '=' and '==' operators in bash or sh
Also you can add:
printf "/n" so your script will behave like a typical Linux prompt for password - information will output in new line
non-zero exit code in case of wrong password (exit 1)
Basically, after those improvements code looks like this:
#!/bin/bash
# Read Password
echo -n Password:
read -s PASSWORD
printf "\n"
passwords() {
echo "PASSWORDS"
}
if [[ "$PASSWORD" == "root" ]]; then
passwords
exit 0
else
echo "Wrong password"
exit 1
fi
Note that I used "==" instead of "=", but for double-bracket they both do the same job.
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 :-)
read command;
if[ $command = "make"]
then
echo "Hello"
elif[ $command = "make run"]
then
echo "Goodbye"
fi
I have looked at similar questions and their solutions are not helping here.
You need to respect spaces in bash scripts like #Jonny Henly said. Here is a modified version of your code, try it and see if it works.
read command;
if [ $command = "make" ]
then
echo "Hello"
elif [ $command = "make run" ]
then
echo "Goodbye"
fi
Hope it helps.
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.