シェルスタディ-10日-シェルフロー制御ステートメント

1.コントロール・フロー計算書について

1)ステートメント形式の場合

変数のパラメータリストに
ない
    コマンドが
行われ

または

パラメータリストの変数の場合;
    コマンドを
実行します

注:1つのループリストのデータのみを取得するたびに、次のコードブロックにそれを渡してください。

2)forステートメントの例

、直接値

[root @ test〜] #vi for-1.sh 
#!/ bin / bash 
for i in a b c d e 
do 
        echo test is $ i 
done 
[root @ test〜] #sh for-1.sh  
test is a 
test is b 
test is c 
test is d 
test is e 
[root @ test〜]#

B.変数の値

[root @ test〜] #vi for-2.sh 
#!/ bin / bash 
list = "a b c d e" 
for i in $ list 
do  
        echo test is $ i 
done 
[root @ test〜] #sh for-2.sh  
testは
テストですb
テストはc
テストはd
テストはe 
[root @ test〜]#

C 、コマンドから値を取得します

[root @ youxi1〜] #vim d.sh 
#!/ bin / bash 
for i in `head -5 / etc / passwd` 
do 
 echo $ i 
done 
[root @ test〜] #sh for-3.sh  
root:x :0:0:root:/ root:/ bin / bash 
bin:x:1:1:bin:/ bin:/ sbin / nologin
デーモン:x:2:2:daemon:/ sbin:/ sbin / nologin 
adm: x:3:4:adm:/ var / adm:/ sbin / nologin 
lp:x:4:7:lp:/ var / pool / lpd:/ sbin / nologin 
[root @ test〜]#

3)カスタムシェルセパレーター

デフォルトでは、ベースシェルはスペース、タブ、およびニューラインを区切り文字として使用します。IFSを介してセパレータとしてカスタマイズされます。

区切り文字として1文字を指定します。

IFS =:#使用:区切り文字としてコロン

IFSは、単一の文字を区切り文字として指定できます。IFS= :(コロンを区切り文字として使用します); IFSは、複数の文字を区切り文字として指定することもできます、IFS = \ n:;(バックスラッシュ、n、コロン、セミコロンを使用してセパレーター)。注: IFSでは、$ '\ n'と$ '\ t'は改行とタブです。

A.1

[root @ test〜] #vim a.sh 
#!/ bin / bash 
list = "a1 a2 b1 b2 \" hello world \ "" 
for i in $ list 
do 
 
 echo $ i 
done 
[root @ test〜] #sh a .SH 
A1 
、A2 
、B1 
、B2 
、 "ハロー
ワールド"

B.は、指定\ n個としてキャリッジリターンセパレーター用のための文

[root @ test〜] #vi for-4.sh 
#!/ bin / bash 
IFS = $ '\ 
n'for i in `head -2 / etc / passwd` 
do 
echo" $ i " 
done 
[root @ test〜] #sh for-4.sh  
root:x:0:0:root:/ root:/ bin / bash 
bin:x:1:1:bin:/ bin:/ sbin / nologin 
[root @ test〜]#

4)C言語でのステートメントの場合

A.文法形式

(; I <10;(私は0を= I ++))のために
ない
コマンドが
行われ

B.単一変数ループ

[root @ test〜] #vi for-5.sh 
#!/ bin / bash 
for((i = 1; i <= 3; i ++))
do 
echo num is $ i 
done 
[root @ test〜] #sh  for -5.sh 
num is 1 
num is 2 
num is 3 
[root @ test〜  ]#

C のためのネスティング(多変量)

[root @ test〜] #vi for-6.sh 
#!/ bin / bash 
for((a = 1、b = 5; a <6; a ++、
b- ))
do echo num is $ a- $ b
行わ
[試験@ルート〜]#SH for-6.sh  
9 - numが1である
NUM 2 - 8 
numが3 - 7 
NUMは4 - 6 
5 - numが5 
[テスト@ルート〜]#

D 、三角形を印刷

[root @ test〜] #vi for-7.sh 
#!/ bin / bash 
for((i = 1; i <5; i ++)); do 
  for((j = 1; j <= i; j ++)) ; do 
     echo -n '*' 
  done 
  echo -e "\ n" 
done 
[root @ test〜] #sh for-7.sh 
* 
 
** 
 
*** 
 
**** 
 
[root @ test〜  ]#

-eエスケープ文字を解釈し 
ます\ tタブ
\ 
nEnterキーを押します
\ b前の文字を削除します\ a
アラームを鳴らします\ 033 [32m $ var \ 033 [0m32 ---緑31 ---赤
-n改行なし


個人公開番号:

image.png

おすすめ

転載: blog.51cto.com/13440764/2575382