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" ]
Related
read -p "Enter your name: " NAME
echo "Hello $NAME, nice to meet you".```
In terminal:
ks#USER Desktop % ./script.sh
./script.sh:read:18: -p: no coprocess
Hello , nice to meet you.
[picture of the problem ][1]
[1]: https://i.stack.imgur.com/ZpVri.png
In ZSH -p doesn't mean prompt as in Bash. See man zshbuiltins:
read [ -rszpqAclneE ] [ -t [ num ] ] [ -k [ num ] ] [ -d delim ]
(...)
-p Input is read from the coprocess.
To get a prompt with ZSH implementation of read:
$ read "?Enter your name: " NAME
Enter your name: myname
$ echo $NAME
myname
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 debug my bash script. What's wrong with my syntax here? I'm trying to evaluate a parameter entered by the user and based on that run one of my IF-THEN statements. However, I'm getting a command not found.
Here's my script thus far:
if [[ $# != 4 ]]; then
echo "Usage: ./test.sh <ABC|XYZ> <owner> <db> <TARGETHOST>" 2>&1
exit 1
fi
case $1 in
ABC|XYZ)
filename="get-$1.sql"
;;
*)echo "Must enter ABC or XYZ"
exit 1
;;
esac
export OWNER=$2
export DB=$3
export HOST_NM=$4
export PORT=5432
export LOG="test-$1.log"
PSQL=`which psql`
if [[$1=="ABC"]]; then
RUNCLI=$("$PSQL" -h $HOST_NM -p $PORT -U $OWNER $DB -F $'\t' --no-align -f get-$1.sql | tee >> $LOG)
exit 1
else
echo "Error running report ..."
fi
if [[$1=="XYZ"]]; then
RUNCLI2=$("$PSQL" -h $HOST_NM -p $PORT -U $OWNER $DB -a -f get-$1.sql | tee >> $LOG)
exit 1
else
echo "Error running report ..."
fi
Error:
./test.sh: line 41: [[XYZ==ABC]]: command not found
Error running report ...
./test.sh: line 51: [[XYZ==XYZ]]: command not found
Error running report ...
Although the question is already answered in the comment section I want to give an answer and share some knowledge which is not obvious (at least it was not for me).
The if in bash just checks the return code of the following command, which means that instead of if [ condition ]... or if [[ condition ]]... you could also write if ./configure && make....
[ and [[ are commands or shell-built-ins, respectively, as well and not part of the if syntax. An which [ for instance returns /bin/[.
At this point it is obvious that you need spaces between the brackets and the condition since it is just just a set of parameters passed to a command.
If you have this in mind, you will never forget the spaces again.
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
I wrote two scripts which try to do the same action in two different ways, but I get errors each time I run those. Kindly requesting your help to correct my scripts and to improve my knowledge as well. All I am trying to do the vps setup in a single script. Following two scripts are just a portion of it which get errors each time.
1) Script to set hostname through cpanel xml-api for a vps in openvz node
cat vpstest.sh
#/bin/bash
hostname_status=`curl -sku root:PASSWORDHERE "https://ip.x.x.x:2087/xml-api/sethostname?hostname=server.domain.com" | awk -F"[<>]" '/status/{print $3}' | head -n1`
if [ $hostname_status -eq 1 ]; then
echo "Hostname set"
else
echo "Failed setting hostname"
fi
Output:
# ./vpstest.sh
./vpstest.sh: line 3: [: -eq: unary operator expected
Failed setting hostname
2) Script to set hostname via command line in an openvz node
cat vpstest1.sh
#!/bin/bash
hostname_status=`vzctl set containerID --hostname server.domain.com --save`
if [ "$hostname_status" -eq 1 ] ; then
echo "Hostname set"
else
echo "Failed setting hostname"
fi
Output:
# ./vpstest1.sh
./vpstest1.sh: line 3: [: CT configuration saved to /etc/vz/conf/containerID.conf: integer expression expected
Failed setting hostname
Can someone help to clear these errors?
First, the output of vzctl set containerID --hostname server.domain.com --save seems not be an integer value whereas -eq is only provided to do comparisons between integers values.
This would explain the following error :
integer expression expected
Then, you should read this reminder about the necessity (or not) to protect your variables with double quotes.
This would explain the following error, you could use something like :
./vpstest.sh: line 3: [: -eq: unary operator expected
If you want to check the status of a command :
command >/dev/null 2>&1 && echo "success" || echo "fail"
# or
if command >/dev/null 2>&1; then
echo "success"
else
echo "fail"
fi
You could also check the variable $? which correspond to the status of the previous command (0 when that command success or another integer value which is 1 in most of cases, or more).