散仙学shellとプログラミングする(二)


詳細
散仙本編は主にshellの中のプロセス制御について話して、プロセス制御はすべてのプログラミング言語の中で不可欠な一部で、プロセス制御を通じて、私たちのプログラムをもっと柔軟にすることができます.
次に、shellの中でif elseフロー制御文を使用する方法を見てみましょう.shellの中のフロー制御文は比較的特殊な他のプログラミング言語で、例えばJAVAは、booleanの値を通じて、あるフローを通過するかどうかを判断します.shellの中で、shellがコマンドを実行するステータスコードによって識別され、0のステータスコードを返し、trueを表します.
[root@h1 test]# cat test.sh 


if date 
then

 echo "success!"

fi
[root@h1 test]# sh test.sh 
2014  08  09      04:13:47 CST
success!
[root@h1 test]# 

if thenは改行せずに書くこともできますが、次のようにセミコロンを付ける必要があります.
[root@h1 test]# cat test.sh 


if date ;then

 echo "success!"

fi
[root@h1 test]# sh test.sh 
2014  08  09      04:15:42 CST
success!
[root@h1 test]# 

if-then-else文の使用を見てみましょう.
[root@h1 test]# cat test.sh 


if date ;then

 echo "success!"

fi
[root@h1 test]# sh test.sh 
2014  08  09      04:15:42 CST
success!
[root@h1 test]# 

多重if-elseの使用を次に示します.
[root@h1 test]# cat c.sh 

if (( 3 < 2  )) ; then

echo "3<2"
elif (( 4 < 3  )); then
echo "4<3"

elif (( 90 < 40 )); then
echo "90<100"

else 
echo "all is error!"

fi


[root@h1 test]# sh c.sh 
all is error!
[root@h1 test]# 

以下にtestコマンドを説明すると、testコマンドはif-then文で異なる条件をテストする方法を提供し、test中の条件が成立すればtestコマンドの脱退ステータスコードは0であり、成立しなければステータスコード1に戻る.
コマンド形式:
if test condition
then
    commands
fi
これはbash shellの中の書き方と同じです.
if [ condition ]
then
    commands
fi
角カッコはtestコマンドで使用する条件を定義します.カッコの両側にスペースを追加する必要があります.そうしないと、エラーが発生します.
testコマンドは、3種類の条件を判断できます.
数値比較;
文字列の比較;
ファイルの比較;
数値比較:
n 1−eq n 2は、n 1がn 2と等しいか否かを判断する
n 1-ge n 2は、n 2以上であるか否かを判断する
n 1-gt n 2 n 1がn 2より大きいかどうかを検査する
n 1-le n 2 n 1がn 2以下であるか否かを検査する
n 1-ltn 2 n 1がn 2より小さいかどうかを検査する
n 1-ne n 2 n 1がn 2に等しくないかどうかを検査する
[root@h1 test]# cat d.sh 


if [ 10 -eq 12   ] ; then
echo "10==12"
else
echo "10!=12"
fi



if [  3 -ge 1   ] ; then
echo "3 > 1"
else
echo "3 < 1"
fi


[root@h1 test]# sh d.sh 
10!=12
3 > 1
[root@h1 test]# 

次に、文字列の比較例を示します.
[root@h1 test]# cat e.sh 


if [  "abc" = "abc"   ];then
echo "abc=abc"
else
echo "abc!=abc"
fi
echo "======================="

if [  'abc' = "abc"  ]; then
echo "abc=abc"
fi



#         
if  [ 6 = 7 ] ;then
echo "6=7"
else 
echo "6!=7"
fi



[root@h1 test]# sh e.sh 
abc=abc
=======================
abc=abc
6!=7
[root@h1 test]# 

注意数値の等しい比較は=で、括弧の左右にスペースが必要です
文字列比較のいくつかの使用方法:
(四角カッコ内にあることに注意>,)
str 1=str 2文字列等しい比較
str1 != str 2文字列の等しくない比較
str 1str 1>str 2 str 1がstr 2より大きいかどうかを確認します(辞書順)
-n str文字列strの長さが0でないかどうかをチェック
-z str文字列strの長さが0であるかどうかを確認します.
[root@h1 test]# cat aa.sh 


str="china"

str2=""


if [  -n $str ] ; then

echo "str is not empty"
else
echo "str is empty"
fi


if [  -z $str2  ] ; then
echo "str2 is empty"
else
echo "str2 is not empty"
fi



time=`date`

echo "    : $time"



[root@h1 test]# sh aa.sh 
str is not empty
str2 is empty
    : 2014  08  09      04:58:43 CST
[root@h1 test]# 

最後にファイル比較を見て
-d file fileファイルが存在し、ディレクトリであるかどうかを確認します.
-e file file fileが存在するかどうかを確認
-f file fileファイルが存在し、ファイルであるかどうかを確認します.
-r file fileファイルが存在し、読み取り可能かどうかを確認
-s file fileファイルが存在し、空でないかどうかを確認します.
-w file fileファイルが存在し、書き込み可能かどうかを確認
-x file file fileが存在するかどうかを確認し、実行可能
-O fileファイルが存在し、現在のユーザーに属しているかどうかを確認します.
-G fileファイルが存在するかどうかを確認し、デフォルトグループは現在のユーザーと同じです.
file 1-nt file 2 file 1がfile 2より新しいかどうかをチェック
file 1-ot file 2 file 1がfile 2より古いかどうかをチェック
[root@h1 test]# cat bb.sh 



#       
if [  -d $HOME  ] ; then
echo "it is a dir"
cd $HOME
ls -la
else 
echo "it is not a dir"

fi

echo "=========================="

#       

if [ -f /etc/profile   ] ; then

echo "it is a file"
else
echo "it is not a file"

fi










[root@h1 test]# sh bb.sh 
it is a dir
    329652
dr-xr-x---. 13 root   root      4096 8    9 05:07 .
dr-xr-xr-x. 22 root   root      4096 8    8 19:34 ..
-rw-r--r--   1 root   root 143775368 7   28 19:30 abc1.txt
-rw-------.  1 root   root      1087 6   13 19:06 anaconda-ks.cfg
-rw-r--r--   1 root   root        65 8    8 04:11 a.sh
-rw-------.  1 root   root     10267 8    9 04:22 .bash_history
-rw-r--r--.  1 root   root        18 5   20 2009 .bash_logout
-rw-r--r--.  1 root   root       176 5   20 2009 .bash_profile
-rw-r--r--.  1 root   root       119 6   16 21:12 .bashrc
-rw-r--r--   1 root   root        75 8    8 05:07 bc.sh
-rw-r--r--   1 root   root        90 8    8 04:14 b.sh
-rw-r--r--   1 root   root       141 8    8 05:12 cc.sh
-rw-r--r--   1 root   root        52 7   31 21:29 count2.txt
-rw-r--r--   1 root   root        52 7   31 19:46 count.txt
-rw-r--r--   1 root   root       127 8    8 04:20 c.sh
-rw-r--r--.  1 root   root       100 9   23 2004 .cshrc
-rw-r--r--.  1 root   root  96183833 6    9 17:27 hadoop-2.2.0.tar.gz
-rw-r--r--   1 root   root         1 7   31 21:25 hh.txt
drwxr-xr-x   3 root   root      4096 7   29 04:47 hivesrc
-rw-r--r--.  1 root   root      2111 6   16 13:10 initserver.sh
-rw-r--r--.  1 root   root      7995 6   13 19:06 install.log
-rw-r--r--.  1 root   root      3384 6   13 19:06 install.log.syslog
drwxr-xr-x   2 root   root      4096 7   31 21:19 intest
lrwxrwxrwx   1 root   root        12 7   31 21:45 jdk -> jdk1.7.0_25/
drwxr-xr-x.  8 search  143      4096 6    6 2013 jdk1.7.0_25
-rwx------.  1 root   root  96316511 11  20 2013 jdk-7u25-linux-x64.gz
drwxr-xr-x   3 root   root      4096 7   31 21:33 li
drwxr-xr-x   3 root   root      4096 7    9 04:08 lo
-rw-r--r--   1 root   root        25 8    8 04:20 log.140808
drwxr-xr-x   3 root   root      4096 7    9 04:08 login
drwxr-xr-x   3 root   root      4096 7   29 04:11 .m2
-rw-------   1 root   root       727 7   29 01:44 .mysql_history
-rw-r--r--   1 root   root      1048 6   19 03:31 setlimit.sh
drwx------.  2 root   root      4096 6   16 21:00 .ssh
-rw-r--r--.  1 root   root       129 12   4 2004 .tcshrc
drwxr-xr-x   2 root   root      4096 8    9 05:07 test
drwxr-sr-x   2 root   abc       4096 8    6 01:53 testidr
-rwxr--r--   1 root   root        46 8    8 03:43 test.sh
drwxr-xr-x   3 root   root      4096 6   20 02:51 tsethadoop
-rw-r--r--   1 root   root        87 8    8 05:29 t.sh
-rw-------   1 root   root      5191 8    9 05:07 .viminfo
-rw-r--r--   1 root   root   1110849 4    7 17:51   .mp3
==========================
it is a file
[root@h1 test]# 

新しい古いファイルを比較するコマンド:
[root@h1 test]# ll
    36
-rw-r--r-- 1 root root 228 8    9 04:58 aa.sh
-rw-r--r-- 1 root root  23 8    5 01:44 a.sh
-rw-r--r-- 1 root root 275 8    9 05:07 bb.sh
-rw-r--r-- 1 root root  54 8    9 04:19 b.sh
-rw-r--r-- 1 root root  90 8    9 05:11 cc.sh
-rw-r--r-- 1 root root 145 8    9 04:25 c.sh
-rw-r--r-- 1 root root 128 8    9 04:37 d.sh
-rw-r--r-- 1 root root 236 8    9 04:43 e.sh
-rw-r--r-- 1 root root  38 8    9 04:15 test.sh
[root@h1 test]# cat cc.sh 


if [  ./a.sh -nt ./b.sh    ] ; then
echo "a.sh is new "
else

echo "b.sh is new"

fi;


[root@h1 test]# sh cc.sh 
b.sh is new
[root@h1 test]# 

複数の条件の組合せテスト:
&&二つの条件を同時に満たす
その中の一つを満足すればよい
[root@h1 test]# cat dd.sh 



if [ 3 \> 2 ] && [ 4 \> 5 ] ; then
echo "3>2 and 4>5"
else
echo "condition is not fit"

fi


echo "=============================================="


if [ 3 \> 2 ] && [ 9 \> 5 ] ; then

echo "3>2 and 9>5"
else
echo "condition is not fit"

fi


[root@h1 test]# sh dd.sh 
condition is not fit
==============================================
3>2 and 9>5
[root@h1 test]# 

上の式は煩雑で、shellには数学演算の(exprssion))二重カッコのコマンドと文字列の両方のカッコのコマンドがわざわざ提供されています.
[root@h1 test]# cat z.sh 
if ((   4 > 6    )) ; then
echo "4 > 6"
else

echo " 4 > 6"

fi


[root@h1 test]# sh z.sh 
 4 > 6
[root@h1 test]# 

両方の括弧は、より高度な文字列操作コマンドを提供し、正則的な使い方を利用しています.
[root@h1 test]# cat h.sh 



if [[   "sanxian" == s*  ]] ; then

echo "begin with s"
else

echo "begin is not s"

fi
[root@h1 test]# sh h.sh 
begin with s
[root@h1 test]# 

注意==記号の右側の正規式は二重引用符で囲むことはできません
最後に、多重if-elseの代替コマンドcaseコマンドの使い方を見てみましょう.
構文の形式:
case var in
  pattern1 | pattern2 ) commands1::
  pattern3) commands2::
  *) default commands::
esac
例は次のとおりです.
[root@h1 test]# cat case.sh 



case "1" in
 
 "1" | "11")  echo "execute this 1" ;;
 "2")   echo "execute this 2" ;;
 *)  echo "this is all default"  ;;

esac
 
[root@h1 test]# sh case.sh 
execute this 1
[root@h1 test]# 

最後の終端記号は、2つの隣接するセミコロンで構成されていることに注意してください.上記の例を通じて、shellの中のcase文は非常に強く、柔軟で、できるだけcase文を使用して、私たちの流れをより明確にすることができます.