0% found this document useful (0 votes)
17 views9 pages

Shell Scripting Test Examples

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views9 pages

Shell Scripting Test Examples

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Test - Shell Scripting Tutorial [Link]

html

1 of 9 26-Jan-19, 7:01 PM
Test - Shell Scripting Tutorial [Link]

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 [Link]

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 [Link]

$ X=5
$ export X
$ ./[Link]
... output of [Link] ...
$ X=hello
$ ./[Link]
... output of [Link] ...
$ X=[Link]
$ ./[Link]
... output of [Link] ...

$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 [Link]

\ ;

; 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...
[...]

[Link]: [: integer expression expected before -lt


[Link]: [: integer expression expected before -gt
[Link]: [: integer expression expected before -le
[Link]: [: integer expression expected before -ge

!=

5 of 9 26-Jan-19, 7:01 PM
Test - Shell Scripting Tutorial [Link]

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 [Link]

$ ./[Link]
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 [Link]

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 [Link]

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

You might also like