shell 流程控制

2021-10-01 23:58:09 字數 2015 閱讀 5235

數字迴圈1

#!/bin/bash

for((i=1;i<=3;i++));

doecho

$done

數字迴圈2

#!/bin/bash

for i in $(seq13

);do

echo

$done

數字迴圈3

#!/bin/bash

for i in ;

doecho

$done

數字迴圈4

#!/bin/bash

awk'

begin

'

字元迴圈1

#!/bin/bash

for s in

"hello";

doecho

$done

字元迴圈2

#!/bin/bash

for c in $*;

doecho $ is the input char

done

字元迴圈3

#!/bin/bash

for c in12

3;doecho

$done

字元迴圈4

#!/bin/bash

str1="

hello world

"for c in

$;do

echo

$done

路徑迴圈1

#!/bin/bash

forfile

in /proc/*

; do

echo $file is file path \! ;

done

路徑迴圈2

#!/bin/bash  

forfile

in $(ls *.sh

) do

echo $file is file path \!;

done

用於執行一系列命令和從輸入檔案中讀取資料

#!/bin/bash

x=1while(($<5

));do

echo

$ #let

"x++

"x=`expr $ + 1

`done

說明:let命令,用於執行乙個或多個表示式,變數計算中不需要加上$來表示變數

無限迴圈

#!/bin/bash

while

true

doecho

1done

or

#!/bin/bash

for((;;));

doecho

1done

用於執行一系列命令直至條件為true時停止,一般while優於until

#!/bin/bash

x=1until((x>3

));do

echo

$ x=`expr $ + 1

`done

#!/bin/bash

score="a"

case $ in"a

") echo

"a score";;

"b") echo

"b score";;

*) echo

"unknown score

"esac

比較運算子

-gt    大於

-lt     小於

-eq   等於

==    等於則條件為真

!=     不等於則條件為真

-z     字串長度為0則為真

-n     字串長度不為0則為真

邏輯運算子

-a    並且

-o    或者

Shell流程控制

case迴圈 if condition condition then statements if true 1 elif condition condition then statements if true 2 else statements if all else fails fi注 方括號中的...

Shell 流程控制

shell的流程控制不可為空,如果else分支沒有語句執行,就不要寫這個else。if 語句語法格式 if condition then command1 command2 commandn fi寫成一行 適用於終端命令提示符 if ps ef grep c ssh gt 1 then echo t...

Shell流程控制

shell 流程控制 if else if語法格式 if condition then command1 command2 fi 末尾的fi就是if倒過來拼寫 if else語法 if condition then command1 command2 else command1 fi if else...