line [: missing `]' debugging help please - shell

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.

Related

shell script if condition giving error message in openshift platform . if[ == ] : not found [No such file or directory ]

I've implemented script like below:
podName=someValue ; // Here value is dynamic (empty or non empty)
if[ $podName == " "] then
echo "Empty"
Even though I got empty an output but still could see:
if[ == ] : not found [No such file or directory ]
error message while running the script.
Seems like there is a small formatting issue which is causing this error. This should be the correct code.
podName=someValue
if [ "$podName" == " " ]; then
echo "Empty"
fi
You can check if the string is empty in bash / sh like this (some containers don't have bash shell):
podName="someValue"
if [ -z "$podName" ]; then
echo "Empty"
fi
or:
podName="someValue"
if [ "$podName" = "" ]; then
echo "Empty"
fi
The following syntax works only in bash [ "$podName" == "" ]

Shell script syntax error (if, then, else)

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 :-)

Trying to get user input shell script programming

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

read y or n to confirm deletion unix shell

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 ]

Chrome OS bash script if statement error

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

Resources