06-14-2009
Equivalence of test, /usr/bin/test, [ ], and /usr/bin/[
these are commands and you can only use one of them:-
examples:-
if test -z "$1"
then
echo "No command-line arguments."
else
echo "First command-line argument is $1."
fi
echo
if /usr/bin/test -z "$1" # Equivalent to "test" builtin.
# ^^^^^^^^^^^^^ # Specifying full pathname.
then
echo "No command-line arguments."
else
echo "First command-line argument is $1."
fi
echo
if [ -z "$1" ] # Functionally identical to above code blocks.
# if [ -z "$1" should work, but...
#+ Bash responds to a missing close-bracket with an error message.
then
echo "No command-line arguments."
else
echo "First command-line argument is $1."
fi
if /usr/bin/[ -z "$1" ] # Again, functionally identical to above.
# if /usr/bin/[ -z "$1" # Works, but gives an error message.
# # Note:
# This has been fixed in Bash, version 3.x.
then
echo "No command-line arguments."
else
echo "First command-line argument is $1."
fi
Finally:-
The [[ ]] construct is the more versatile Bash version of [ ]. This is the extended test command, adopted from
ksh88.
No filename expansion or word splitting takes place between [[ and ]], but there is parameter expansion
and command substitution.
file=/etc/passwd
if [[ -e $file ]]
then
echo "Password file exists."
fi
Using the [[ ... ]] test construct, rather than [ ... ] can prevent many logic errors in scripts. For example,
the &&, ||, <, and > operators work within a [[ ]] test, despite giving an error within a [ ] construct.
10 More Discussions You Might Find Interesting
1. UNIX for Dummies Questions & Answers
Hi,
No need to say I'm new to unix shell scripting.
I have a very simple script that goes this way:
for datos in `ls -rt $UNXLOG/26-Jan*`
do
export arch=`echo $datos |cut -d, -f1`
if
then
export linea1=`grep Debut ${arch}`
export horatot=`echo $linea1 |cut -d' ' -f5`
... (7 Replies)
Discussion started by: mvalonso
7 Replies
2. UNIX for Dummies Questions & Answers
Hi
I am doing a script like
if
then
echo "table name dosent exist"
exit
fi
the problem is if $table_name is null then i am getting the error
Please help me
Thanks in advance (2 Replies)
Discussion started by: ssuresh1999
2 Replies
3. Shell Programming and Scripting
Hi All,
I am new to shell scripting and right now I am just limited to using the pre-written scripts. I am in to Infrastructure management where we use different scripts to get the information passed on to the monitoring tools. I am trying to use this script to get the information about the... (2 Replies)
Discussion started by: theamrit
2 Replies
4. Shell Programming and Scripting
hi
i am trying to compare a value with value 50. but i am getting
"
I am using
if
then
echo "------------"
fi
please help
thanks in advance
Satya (2 Replies)
Discussion started by: Satyak
2 Replies
5. Shell Programming and Scripting
Greetings, using ksh on Solaris, I am trying to identify the current version of a package installed on multiple servers using if statement in a precursor to upgrading.
I have searched the forums and have found many hits, reviewed 3 pages and have tried the different variations noted there. Also... (3 Replies)
Discussion started by: 22blaze
3 Replies
6. UNIX for Dummies Questions & Answers
find . -name "*.*"|xargs grep WT:DBF_WL>> $F
Wfexist=`cat $F|grep $i`
echo $Wfexist
if ;
then
echo $Wfexist
echo "Workflow Exist"
else
touch $O
chmod 777 $O
echo $Wfexist
echo $WfExist >> $O
fi
I am getting the error that -ne: unary operator expected in the line with red... (2 Replies)
Discussion started by: ritu.s
2 Replies
7. UNIX for Dummies Questions & Answers
Why am I getting this error.... #!/bin/sh
# iOS-Ad-Remover
# Marshall Ford @ marshallbford@gmail.com
# This project is hosted @ http://ios-ad-
# remover.sourceforge.net
# Under the GNU GPL open source license
clear
echo
if ; then echo "You need to be root to run this script."; exit 0; #... (24 Replies)
Discussion started by: mbf123
24 Replies
8. UNIX for Dummies Questions & Answers
Hi
I have problem with my script.
I dont now why but i don't change anything and script stop working.
this is my code:
#!/bin/sh
for i in `ps -A | grep pocron.sh | grep -v grep | awk '{print $2}'`
do
COUNT=$((COUNT+1))
done
ostatnie_wykonanie=`cat porader.log`... (1 Reply)
Discussion started by: fotex
1 Replies
9. Shell Programming and Scripting
Hi All,
I'm assigning a numeric value to variable count=2, well its being assigned by code above the if condition.
I want to test for 2 conditions , when $count = 0 or $count <=2 and do something when the condition matches. here is my code, but i run into the infamous :
if ]
then
... (2 Replies)
Discussion started by: Irishboy24
2 Replies
10. Shell Programming and Scripting
In all my Googling, this usually happens when someone is comparing a variable, and that variable is unset. That doesn't appear to be the case for me...
#!/bin/bash -x
while read line
do
f=$(echo $line | tr -s ' ' | cut -d' ' -f 3)
echo $f
if
then
echo "This... (2 Replies)
Discussion started by: jnojr
2 Replies
test(1) General Commands Manual test(1)
NAME
test, [ - Evaluates conditional expressions
SYNOPSIS
test [expression]
[[expression]]
STANDARDS
Interfaces documented on this reference page conform to industry standards as follows:
test: XCU5.0
[: XCU5.0
Refer to the standards(5) reference page for more information about industry standards and associated tags.
OPTIONS
None
OPERANDS
The expression to be evaluated as true or false. This expression is constructed from the operators and elements explained in the DESCRIP-
TION section.
DESCRIPTION
The test command evaluates an expression constructed of functions and operators. If the value of expression is true, test returns an exit
value of zero(0); otherwise, it returns FALSE, a nonzero exit value. The test command also returns a nonzero exit value if there are no
arguments.
The alternate form of the command surrounds expression with brackets ([ ]). When you use this form, you must surround the brackets with
spaces.
The test Expressions
All of the listed functions and operators are separate arguments to test.
The following functions are used to construct expression: TRUE if file exists and has read permission. TRUE if file exists and has write
permission. TRUE if file exists and has execute permission. TRUE if file exists and is a regular file. TRUE if file exists and is a
directory. TRUE if file exists. TRUE if file exists and is a character-special file. TRUE if file exists and is a block-special file.
TRUE if file exists and is a named pipe (FIFO). [Tru64 UNIX] TRUE if file exists and is a soft link. Synonym for -L expression. TRUE if
file exists and is a soft link. Synonym for -h expression. TRUE if file exists and its set-user ID bit is set. TRUE if file exists and
its set-group ID bit is set. [Tru64 UNIX] TRUE if file exists and its sticky bit is set. TRUE if file exists and has a size greater than
zero(0). TRUE if the open file with file descriptor number file_descriptor (1 by default) is associated with a terminal device. TRUE if
the length of string1 is zero(0). TRUE if the length of string1 is nonzero. TRUE if string1 and string2 are identical. TRUE if string1
and string2 are not identical. TRUE if string1 is not the null string. TRUE if the integers number1 and number2 are algebraically equal.
Any of the comparisons -ne, -gt, -ge, -lt, and -le can be used in place of -eq.
The listed functions can be combined with the following operators: Unary negation operator. Binary AND operator. Binary OR operator (the
-a operator has higher precedence than the -o operator). [Tru64 UNIX] Parentheses for grouping.
EXIT STATUS
The following exit values are returned: The test command evaluated expression and its value is TRUE. The test command evaluated expression
and is value is FALSE, or there are no arguments. An error occurred.
EXAMPLES
To test whether a file exists and is not empty, enter: if test ! -s "$1" then echo $1 does not exist or is empty. fi
If the file specified by the first positional parameter to the shell procedure does not exist, this displays an error message. If
$1 exists, it displays nothing. There must be a space between -s expression and the file name.
The double quotes around $1 ensure that the test will work properly even if the value of $1 is the empty string. If the double
quotes are omitted and $1 is the empty string, test displays the error message test: parameter expected. To do a complex compari-
son, enter: if [ $# -lt 2 -o ! -s "$1" ] then exit fi
If the shell procedure was given fewer than two positional parameters or the file specified by $1 does not exist, then this exits
the shell procedure. The special shell variable $# represents the number of positional parameters entered on the command line that
started this shell procedure.
ENVIRONMENT VARIABLES
The following environment variables affect the execution of test: Provides a default value for the internationalization variables that are
unset or null. If LANG is unset or null, the corresponding value from the default locale is used. If any of the internationalization vari-
ables contain an invalid setting, the utility behaves as if none of the variables had been defined. If set to a non-empty string value,
overrides the values of all the other internationalization variables. Determines the locale for the interpretation of sequences of bytes
of text data as characters (for example, single-byte as opposed to multibyte characters in arguments). Determines the locale for the for-
mat and contents of diagnostic messages written to standard error. Determines the location of message catalogues for the processing of
LC_MESSAGES.
SEE ALSO
Commands: csh(1), find(1), ksh(1), Bourne shell sh(1b), POSIX shell sh(1p)
Standards: standards(5)
test(1)