LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Blogs > Jukka's script corner
User Name
Password

Notices

Using posix-sh (sh, ksh, bash, ...)

My site (finnish)

My first language is finnish, then
sh, C, awk, K, ... english ...
Sorry about my finglish.
Rate this Entry

If - how to use

Posted 12-05-2008 at 02:27 AM by kshfi
Updated 12-07-2008 at 09:57 AM by kshfi
Tags bash, ksh

Programmers - shell if is command.

Code:
if command exit code is 0
then
    do something 1
    do something 2
fi
Ex.
Code:
if  cp fromfile tofile
then
   echo "Works fine"
else
   echo "Error ???"
fi
[ = test
You can write test or [

Code:
a=1
b=1
test "$a" = "$b"
echo $?
0

a=1
b=2
test "$a" = "$b"
echo $?
1
is same as

Code:
a=1
b=1
[ "$a" = "$b" ]
echo $?
0

a=1
b=2
[ "$a" = "$b" ]
echo $?
1
In some *nix systems test and [ are same binary. Bash, ksh, ... [ and test are built-in commands.

If you like to test command exit status, then it is possible with if.

Code:
a=1
b=1
if [ "$a" = "$b" ] ; then
   echo "equal"
fi
# or
a=1
b=1
if test "$a" = "$b"  ; then
   echo "equal"
fi
You can write it also:
Code:
a=1
b=1
[ "$a" = "$b" ] 
if [ $? = 0 ] ; then
   echo "equal"
fi
Why ; before then ?
if and then on same line = you need
command delimeter ;

Remember
if is command which has arguments and options. = space between every arguments.

Basic string testing
= equal
!= not equal

Basic numeric testing (options)
-eq equal
-ne not equal
-lt less than
-le less or equal
-gt greater than
-ge greater or equal

Basic file testing (options)
-f file exist
-d directory exist
-x execute priviledges
-r read ...
-w write ...

Negation !
Code:
if [ ! -f file ] ; then
   echo "file $file not exists"
fi
Code:
if ! cp x y ; then
   echo "error"
fi
And, or
-a and
-o or
Code:
if cp x y -a cp b z ; then
   echo "noth copy done"
else
   echo "co not worked
fi
if not like to see errors
Code:
if cp x y 2>/dev/null -a cp b z 2>/dev/null; then
   echo "both copy done"
else
   echo "cp not worked"
fi


&& ||

Code:
cp x y && cp a b || echo "error"
Is same as
Code:
if cp x y ; then
  if ! cp a b ; then
        echo "error"
  fi
fi

&& if prev. exit stat 0, then this
|| if prev. exit stat <> 0, then this
Using variables
when use variables in test commands, use "". Why ? If variable is empty - you get error.

Code:
if [ "$a" = "" ] ; then
   echo "empty"
   exit 
fi
If $a value is empty and you write
Code:
if [ $a = "" ] ; then
   echo "empty"
   exit 
fi
you get error, because arg-1 is nothing.
"$a" is string, length 0

[[ ]] (( ))
[[ is not same as [
(( is for only numeric testing (C-like)

Code:
a=1
b=2
((  a>b  )) && echo "$a > $b "
Views 1158 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 09:41 AM.

Main Menu
Advertisement

My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration