Confusion about bash file exists test operator - bash

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?

Related

Bash script conditional do while loop [duplicate]

Usually, I use square brackets in the if statement:
if [ "$name" = 'Bob' ]; then ...
But, when I check if grep succeeded I don't use the square brackets:
if grep -q "$text" $file ; then ...
When are the square brackets necessary in the if statement?
The square brackets are a synonym for the test command. An if statement checks the exit status of a command in order to decide which branch to take. grep -q "$text" is a command, but "$name" = 'Bob' is not--it's just an expression. test is a command, which takes an expression and evaluates it:
if test "$name" = 'Bob'; then ...
Since square brackets are a synonym for the test command, you can then rewrite it as your original statement:
if [ "$name" = 'Bob' ]; then ...
[ is actually a command, equivalent (almost, see below) to the test command. It's not part of the shell syntax. (Both [ and test, depending on the shell, are often built-in commands as well, but that doesn't affect their behavior, except perhaps for performance.)
An if statement executes a command and executes the then part if the command succeeds, or the else part (if any) if it fails. (A command succeeds if it exits with a status ($?) of 0, fails if it exits with a non-zero status.)
In
if [ "$name" = 'Bob' ]; then ...
the command is
[ "$name" = 'Bob' ]
(You could execute that same command directly, without the if.)
In
if grep -q "$text" $file ; then ...
the command is
grep -q "$text" $file
man [ or man test for more information.
FOOTNOTE: Well, the [ command is almost equivalent to the test command. The difference is that [ requires ] as its last argument, and test does not -- and in fact doesn't allow it (more precisely, test doesn't treat a ] argument specially; for example it could be a valid file name). (It didn't have to be implemented that way, but a [ without a matching ] would have made a lot of people very very nervous.)
The best way to think of the [ ... ] syntax, is to consider [ to be a program - which it is!
Check this out:
~ $ ls /usr/bin/\[
/usr/bin/[
on the other hand, you're probably not using that version of it since bash also provides [ as a shell built-in.
Anyway, to answer your question: What if does is run the command you give it and see it the return value is 0 or not. You use [ to do other, more interesting comparisons such as string comparisons. See man [ and man bash.

Bash ping command in if statement with square brackets does not work [duplicate]

Usually, I use square brackets in the if statement:
if [ "$name" = 'Bob' ]; then ...
But, when I check if grep succeeded I don't use the square brackets:
if grep -q "$text" $file ; then ...
When are the square brackets necessary in the if statement?
The square brackets are a synonym for the test command. An if statement checks the exit status of a command in order to decide which branch to take. grep -q "$text" is a command, but "$name" = 'Bob' is not--it's just an expression. test is a command, which takes an expression and evaluates it:
if test "$name" = 'Bob'; then ...
Since square brackets are a synonym for the test command, you can then rewrite it as your original statement:
if [ "$name" = 'Bob' ]; then ...
[ is actually a command, equivalent (almost, see below) to the test command. It's not part of the shell syntax. (Both [ and test, depending on the shell, are often built-in commands as well, but that doesn't affect their behavior, except perhaps for performance.)
An if statement executes a command and executes the then part if the command succeeds, or the else part (if any) if it fails. (A command succeeds if it exits with a status ($?) of 0, fails if it exits with a non-zero status.)
In
if [ "$name" = 'Bob' ]; then ...
the command is
[ "$name" = 'Bob' ]
(You could execute that same command directly, without the if.)
In
if grep -q "$text" $file ; then ...
the command is
grep -q "$text" $file
man [ or man test for more information.
FOOTNOTE: Well, the [ command is almost equivalent to the test command. The difference is that [ requires ] as its last argument, and test does not -- and in fact doesn't allow it (more precisely, test doesn't treat a ] argument specially; for example it could be a valid file name). (It didn't have to be implemented that way, but a [ without a matching ] would have made a lot of people very very nervous.)
The best way to think of the [ ... ] syntax, is to consider [ to be a program - which it is!
Check this out:
~ $ ls /usr/bin/\[
/usr/bin/[
on the other hand, you're probably not using that version of it since bash also provides [ as a shell built-in.
Anyway, to answer your question: What if does is run the command you give it and see it the return value is 0 or not. You use [ to do other, more interesting comparisons such as string comparisons. See man [ and man 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.

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 ]

When are square brackets required in a Bash if statement?

Usually, I use square brackets in the if statement:
if [ "$name" = 'Bob' ]; then ...
But, when I check if grep succeeded I don't use the square brackets:
if grep -q "$text" $file ; then ...
When are the square brackets necessary in the if statement?
The square brackets are a synonym for the test command. An if statement checks the exit status of a command in order to decide which branch to take. grep -q "$text" is a command, but "$name" = 'Bob' is not--it's just an expression. test is a command, which takes an expression and evaluates it:
if test "$name" = 'Bob'; then ...
Since square brackets are a synonym for the test command, you can then rewrite it as your original statement:
if [ "$name" = 'Bob' ]; then ...
[ is actually a command, equivalent (almost, see below) to the test command. It's not part of the shell syntax. (Both [ and test, depending on the shell, are often built-in commands as well, but that doesn't affect their behavior, except perhaps for performance.)
An if statement executes a command and executes the then part if the command succeeds, or the else part (if any) if it fails. (A command succeeds if it exits with a status ($?) of 0, fails if it exits with a non-zero status.)
In
if [ "$name" = 'Bob' ]; then ...
the command is
[ "$name" = 'Bob' ]
(You could execute that same command directly, without the if.)
In
if grep -q "$text" $file ; then ...
the command is
grep -q "$text" $file
man [ or man test for more information.
FOOTNOTE: Well, the [ command is almost equivalent to the test command. The difference is that [ requires ] as its last argument, and test does not -- and in fact doesn't allow it (more precisely, test doesn't treat a ] argument specially; for example it could be a valid file name). (It didn't have to be implemented that way, but a [ without a matching ] would have made a lot of people very very nervous.)
The best way to think of the [ ... ] syntax, is to consider [ to be a program - which it is!
Check this out:
~ $ ls /usr/bin/\[
/usr/bin/[
on the other hand, you're probably not using that version of it since bash also provides [ as a shell built-in.
Anyway, to answer your question: What if does is run the command you give it and see it the return value is 0 or not. You use [ to do other, more interesting comparisons such as string comparisons. See man [ and man bash.

Resources