0% found this document useful (0 votes)
43 views

Test - Shell Scripting Tutorial

This document discusses the test command in shell scripting. It can be used to evaluate conditions and expressions in if/then statements. Some examples shown include checking file properties like size and timestamps, string equality, numeric comparisons, and checking for empty/null values. Proper syntax for using test in if/then/else conditional blocks is also demonstrated.

Uploaded by

Amitava Sarder
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Test - Shell Scripting Tutorial

This document discusses the test command in shell scripting. It can be used to evaluate conditions and expressions in if/then statements. Some examples shown include checking file properties like size and timestamps, string equality, numeric comparisons, and checking for empty/null values. Proper syntax for using test in if/then/else conditional blocks is also demonstrated.

Uploaded by

Amitava Sarder
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Test - Shell Scripting Tutorial https://www.shellscript.sh/test.

html

1 of 9 26-Jan-19, 7:01 PM
Test - Shell Scripting Tutorial https://www.shellscript.sh/test.html

Share

test test
[ [ test

[ test

$ type [
[ is a shell builtin
$ which [
/usr/bin/[
$ ls -l /usr/bin/[
lrwxrwxrwx 1 root root 4 Mar 27 2000 /usr/bin/[ -> test
$ ls -l /usr/bin/test
-rwxr-xr-x 1 root root 35368 Mar 27 2000 /usr/bin/test

[ ls

2 of 9 26-Jan-19, 7:01 PM
Test - Shell Scripting Tutorial https://www.shellscript.sh/test.html

if [$foo = "bar" ]

if test$foo = "bar" ] ]
[

if SPACE [ SPACE "$foo" SPACE = SPACE "bar" SPACE ]

==
= -eq

man
test

if while

test

if...then...else...

if [ ... ]
then
# if-code
else
# else-code
fi

fi if
esac
if [ ... ] then
;

if [ ... ]; then
# do something
fi

elif

if [ something ]; then
echo "Something"
elif [ something_else ]; then
echo "Something else"
else
echo "None of the above"
fi

echo "Something" [ something ]


[ something_else ] echo "Something
else" echo "None of the
above"

3 of 9 26-Jan-19, 7:01 PM
Test - Shell Scripting Tutorial https://www.shellscript.sh/test.html

$ X=5
$ export X
$ ./test.sh
... output of test.sh ...
$ X=hello
$ ./test.sh
... output of test.sh ...
$ X=test.sh
$ ./test.sh
... output of test.sh ...

$X
/etc/hosts

#!/bin/sh
if [ "$X" -lt "0" ]
then
echo "X is less than zero"
fi
if [ "$X" -gt "0" ]; then
echo "X is more than zero"
fi
[ "$X" -le "0" ] && \
echo "X is less than or equal to zero"
[ "$X" -ge "0" ] && \
echo "X is more than or equal to zero"
[ "$X" = "0" ] && \
echo "X is the string or number \"0\""
[ "$X" = "hello" ] && \
echo "X matches the string \"hello\""
[ "$X" != "hello" ] && \
echo "X is not the string \"hello\""
[ -n "$X" ] && \
echo "X is of nonzero length"
[ -f "$X" ] && \
echo "X is the path of a real file" || \
echo "No such file: $X"
[ -x "$X" ] && \
echo "X is the path of an executable file"
[ "$X" -nt "/etc/passwd" ] && \
echo "X is a file which is newer than /etc/passwd"

;
if
\

4 of 9 26-Jan-19, 7:01 PM
Test - Shell Scripting Tutorial https://www.shellscript.sh/test.html

\ ;

; if then

if [ "$X" -nt "/etc/passwd" ]; then


echo "X is a file which is newer than /etc/passwd"
fi

[ "$X" -nt "/etc/passwd" ] && \


echo "X is a file which is newer than /etc/passwd"

test

-a -e -S
-nt -ot -ef
-O

if && ||

#!/bin/sh
[ $X -ne 0 ] && echo "X isn't zero" || echo "X is zero"
[ -f $X ] && echo "X is a file" || echo "X is not a file"
[ -n $X ] && echo "X is of non-zero length" || \
echo "X is of zero length"

[
test
if...then...else...
[...]

test.sh: [: integer expression expected before -lt


test.sh: [: integer expression expected before -gt
test.sh: [: integer expression expected before -le
test.sh: [: integer expression expected before -ge

!=

5 of 9 26-Jan-19, 7:01 PM
Test - Shell Scripting Tutorial https://www.shellscript.sh/test.html

echo -en "Please guess the magic number: "


read X
echo $X | grep "[^0-9]" > /dev/null 2>&1
if [ "$?" -eq "0" ]; then
# If the grep found something other than 0-9
# then it's not an integer.
echo "Sorry, wanted a number"
else
# The grep found only 0-9, so it's an integer.
# We can safely do a test on it.
if [ "$X" -eq "7" ]; then
echo "You entered the magic number!"
fi
fi

echo
$?
grep grep [0-9]
^
grep [^0-9]

>/dev/null 2>&1

grep -v [0-9]

#!/bin/sh
X=0
while [ -n "$X" ]
do
echo "Enter some text (RETURN to quit)"
read X
echo "You said: $X"
done

while [ -n "$X" ]

6 of 9 26-Jan-19, 7:01 PM
Test - Shell Scripting Tutorial https://www.shellscript.sh/test.html

$ ./test2.sh
Enter some text (RETURN to quit)
fred
You said: fred
Enter some text (RETURN to quit)
wilma
You said: wilma
Enter some text (RETURN to quit)

You said:
$

#!/bin/sh
X=0
while [ -n "$X" ]
do
echo "Enter some text (RETURN to quit)"
read X
if [ -n "$X" ]; then
echo "You said: $X"
fi
done

if

if [ "$X" -lt "0" ]


then
echo "X is less than zero"
fi

.......... and ........

if [ ! -n "$X" ]; then
echo "You said: $X"
fi

if then

if then

if [ ! -n "$X" ]
echo "You said: $X"

then fi

Share

7 of 9 26-Jan-19, 7:01 PM
Test - Shell Scripting Tutorial https://www.shellscript.sh/test.html

Purchase flow
1. 1. Product preview

2. 2. Payment form
Optimize for conversion (?)
Require shipping information
More information: Required

3. 3. Receipt

Style

Pick a theme:

Locked

Unlock total customization and more with our


paid plan. Upgrade account

Use theme defaults

Customize:

Pull style from Twitter

Video thumbnail processing...

We support image and video uploads.

No file selected.

8 of 9 26-Jan-19, 7:01 PM
Test - Shell Scripting Tutorial https://www.shellscript.sh/test.html

9 of 9 26-Jan-19, 7:01 PM

You might also like