Using posix-sh (sh, ksh, bash, ...)
My site (finnish)
My first language is finnish, then
sh, C, awk, K, ... english ...
Sorry about my finglish.
My site (finnish)
My first language is finnish, then
sh, C, awk, K, ... english ...
Sorry about my finglish.
If - how to use
Programmers - shell if is command.
Ex.
[ = test
You can write test or [
is same as
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.
You can write it also:
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 !
And, or
-a and
-o or
if not like to see errors
&& ||
Is same as
Using variables
when use variables in test commands, use "". Why ? If variable is empty - you get error.
If $a value is empty and you write
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:
if command exit code is 0 then do something 1 do something 2 fi
Code:
if cp fromfile tofile then echo "Works fine" else echo "Error ???" fi
You can write test or [
Code:
a=1 b=1 test "$a" = "$b" echo $? 0 a=1 b=2 test "$a" = "$b" echo $? 1
Code:
a=1 b=1 [ "$a" = "$b" ] echo $? 0 a=1 b=2 [ "$a" = "$b" ] echo $? 1
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
Code:
a=1 b=1 [ "$a" = "$b" ] if [ $? = 0 ] ; then echo "equal" fi
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
-a and
-o or
Code:
if cp x y -a cp b z ; then echo "noth copy done" else echo "co not worked fi
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"
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
when use variables in test commands, use "". Why ? If variable is empty - you get error.
Code:
if [ "$a" = "" ] ; then echo "empty" exit fi
Code:
if [ $a = "" ] ; then echo "empty" exit fi
"$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 "
Total Comments 0