could someone help me figure out why this doesn't work? - bash

i=0
if [$i -eq 0]
then
echo "i is equal to 0"
else
echo "NOT EQUAL <><><><><><><><><><><><><><><><><><><>"
fi
it is part of a bash script and it always takes the else branch. I'm completely new to bash so its probably something silly

you need [ $i instead of [$i.
This is because the [ is a builtin command and $i should be it's first parameter. If you miss the space between command and parameter, then the shell will look for [$i command and after evaluation will tell you that there is no [0 command to be executed.

You need spaces after '[' and before ']'. '[' is a command.

Related

Confusion about bash file exists test operator

The following returns nothing:
which asdf
So why does the if statement get triggered here?
x=$(which asdf)
if [ -f $x ]; then echo "exists"; fi
You didn't quote $x, so your test becomes [ -f ], which is true because -f is a non-empty string.
if [ -f "$x" ]; then
Though Chepner has given good solution, in case you want to look for an alternate approach then try following once.
which asdf 2>&1 >/dev/null && echo "exists"
Looks like you are trying to check if a command exists. It is better to use the command builtin instead of which in that context, like this:
if command -v asdf; then
echo "exists"
fi
To learn more about command, try help command.
Related:
How to check if a program exists from a Bash script?
Is double square brackets [[ ]] preferable over single square brackets [ ] in Bash?

Command online argument in shell script

Hi I have written small shell script, I am not able to understand the behavior of that script. can any one help me to understand that script.
Script:
#!/bin/bash
if [ -z $1 ]
then
echo "fail"
else
echo "success"
fi
While executing the script .
./test.sh one
It exuting the else statement instead of main statement , even though its passing the argument.
can any one explain me this behavior to understand
The -z test in bash is checking if a string is an empty (zero length) value.
Since you're passing an argument to the script $1 is not empty and therefore -z $1 evaluates to false, executing the else portion of your script.
Side note: Since you're working with strings I recommend you to quote variables as follows:
if [ -z "$1" ]; then
echo "String is empty / No argument given"
else
echo "String is not empty / Argument given"
fi
Edit:
As pointed out by user1934428 it's probably better to use [[ instead of [. This, among others, eliminates the need for quoting. See more differences here.
if [[ -z $1 ]]; then
...
However, be aware that this is a bash extension and won't work in sh scripts.

bash script unexpected else in for loop

this script tries to ping through a hosts string array.
for i in "${arr[#]}"
do
echo "check if $i is online"
ping -c1 $i &>/dev/null
if[ $? -eq 0 ] then
echo "$i is online"
else
echo "$i is not online"
fi
done
I am new to bash scripting so if some one could tell me why I get an
syntax error near unexpected token `else'
and also if this is a good approach. Thanks in advance
[ is not part of bash's syntax; it is a regular character like a or 8. As such, the parser does not recognize if[ as two words if and [; it just sees the name if[, which it assumes might be a command name followed by a series of arguments terminated by the end of the line. The following line is also a valid command. The next line, though, begins with else, which is a recognized keyword and thus cannot occur in command position, but only as part of an on-going if statement, triggering the error.
All of which is to say, you must separate if and [ with a space.
if [ $? -eq 0 ]; then
(You also need a semi-colon before then if it appears on the same line, which would be the next problem you encountered after fixing the space issue.)
The line number 6, should have been with a semi-colon ; and need a space after the if in if[ $? of your line.
if [ $? -eq 0 ]; then
More recommended way is you can directly use ping's exit-code in if-statement as
if ping -c 1 "$i" &> /dev/null
then
echo "$i is online"
else
echo "$i is not online"
fi
See the below excerpt from the man ping page on why I used the -c flag.
-c count
Stop after sending count ECHO_REQUEST packets. With deadline option, ping waits for count ECHO_REPLY packets, until the timeout expires.
Use http://www.shellcheck.net/ , to debug such trivial syntax errors.
if [ $? -eq 0 ]; then You need to have a space between the if and the [ and also you missed a ;

Test bash command line arguments

I don't know bash well but this seems pretty basic, yet I'm stuck on it. I'm using the bash installed on Mac OS X. I'm simply trying to test 1 command line argument and this is what I have and it doesn't work.
if [$1 -eq 'clean']
then
echo "Your argument is 'clean'!"
fi
Every time I've tried it, bash gives me a command not found error.
I'm obviously doing something wrong, what is it?
Couple of issues here:
Spaces around [ and ] are required in shell
-eq is used for comparing integers not for strings
Try this instead:
if [[ "$1" == "clean" ]]; then
echo "Your argument is 'clean'!"
fi
If you are using bash then [[ and ]] are more efficient than [ and ]

Trying to check if information written in a text file is equal to a variable within shell script

I'm still a newbie to Linux scripting. My goal is to determine if the information written in a .txt file is equal to a variable within the script. This is the code that I have written so far:
#!/bin/sh
a=-5
if [$(cat < top.txt) -eq $a ]; then
echo "YES!"
else
echo "NNOOO!"
fi
Please let me know what I should be doing. To reiterate, I want top.txt to equal -5. I currently have -5 written in the text but can't seem to get it to work.
Use More Quotes™:
[ "$(cat top.txt)" = "$a" ]
However, beware that this doesn't handle the case where top.txt ends with a newline - $() will chop that right off. So you might be better served with this:
[ "$(cat top.txt; printf x)" = "$a"x ]
Another issue is that you can't check files containing the NUL character this way; variables can not contain NUL characters.
At this point I'd be tempted to ask what you are trying to achieve.
You can use:
[ "$(<top.txt)" -eq "$a" ] && echo "YES!" || echo "NOOOS!"

Resources