Test command

From Linux Shell Scripting Tutorial - A Beginner's handbook
Revision as of 17:44, 1 September 2009 by Admin (talk | contribs) (Created page with 'The test command is used to check file types and compare values. Test is used in conditional execution. test syntax is as follows: <source lang="bash">test condition</source> OR …')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The test command is used to check file types and compare values. Test is used in conditional execution. test syntax is as follows:

test condition

OR

test condition && true-command

OR

test condition || false-command

OR

test condition && true-command || false-command

Type the following command at a shell prompt (is 5 greater than 2? ):

test 5 > 2 && echo "Yes"

Sample Output:

Yes

You need to use the test command while make decision. Try the following examples and note down its output:

test 5 == 5 && echo "Yes" || echo "No"
test 5 == 15 && echo "Yes" || echo "No"
test 5 != 10 && echo "Yes" || echo "No"
test -f /etc/resolv.conf && echo "File /etc/resolv.conf found." || echo "File /etc/resolv.conf not found."
test -f /etc/resolv1.conf && echo "File /etc/resolv.conf found." || echo "File /etc/resolv.conf not found."