您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關linux for循環怎么理解,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
for循環是Linux shell 中最常用的結構。for 循環有三種結構:一種結構是列表for循環;第二種結構是不帶列表for循環;第三種結構是類C風格的for循環。
for var in list
do
commands
done
#!/bin/bash
for test in alabama alaska arizona arkansas california colorado
do
echo the next state is $test
done
>
the next state is alabama
the next state is alaska
the next state is arizona
the next state is arkansas
the next state is california
the next state is colorado
#!/bin/bash
for test in alabama alaska arizona arkansas california colorado
do
echo "the next state is $test"
done
echo "The las state we visited was $test"
test=connecticut
echo "wait, now we're visiting $test"
>
the next state is alabama
the next state is alaska
the next state is arizona
the next state is arkansas
the next state is california
the next state is colorado
The las state we visited was colorado
wait, now we're visiting connecticut
#!/bin/bash
for test in I don't know if this'll work
do
echo "word:$test"
done
>
word:I
word:dont know if thisll
word:work
#!/bin/bash
for test in nevada "New Hampshire" "New Mexico" "New York"
do
echo "Now going to $test"
done
>
Now going to nevada
Now going to New Hampshire
Now going to New Mexico
Now going to New York
#!/bin/bash
list="Alabama ALaska Arizona Arkansas Colorado"
list=$list" Connecticut"
for state in $list
do
echo "Have you ever visited $state?"
done
>
Have you ever visited Alabama?
Have you ever visited ALaska?
Have you ever visited Arizona?
Have you ever visited Arkansas?
Have you ever visited Colorado?
Have you ever visited Connecticut?
#!/bin/bash
file="states"
for state in `cat $file`
do
echo "Visit beautiful $state"
done
#cat states
>
Alabama
Alaska
dddfac
dfsase
cvcvss
dfqzcvc
ddddwq
bnnn
qqojfaso
>
Visit beautiful Alabama
Visit beautiful Alaska
Visit beautiful dddfac
Visit beautiful dfsase
Visit beautiful cvcvss
Visit beautiful dfqzcvc
Visit beautiful ddddwq
Visit beautiful bnnn
Visit beautiful qqojfaso
#!/bin/bash
IFS=$'\n'
file="states"
for state in `cat $file`
do
echo "visit beautiful $state"
done
>
#cat states
>
A labama
Alaska
dddfac
dfsase
cvcvss
dfqzcvc
ddddwq
bnnn
qqojfaso
>
visit beautiful A labama
visit beautiful Alaska
visit beautiful dddfac
visit beautiful dfsase
visit beautiful cvcvss
visit beautiful dfqzcvc
visit beautiful ddddwq
visit beautiful bnnn
visit beautiful qqojfaso
在處理長腳本時,可能在一個地方需要修改IFS的值,然后忘掉它并在腳本中其他地方以為還是默認的值。一個可參考的簡單實踐:
IFS.OLD=$IFS
IFS=$'\n'
t exist"
fi
done
類C語言風格
#!/bin/bash
for (( i=1; ido
echo "The next nuber is $i"
done
>
The next nuber is 1
The next nuber is 2
The next nuber is 3
The next nuber is 4
The next nuber is 5
The next nuber is 6
The next nuber is 7
The next nuber is 8
The next nuber is 9
The next nuber is 10
#!/bin/bash
for (( a=1, b=10; a do
echo "$a - $b"
done
>
1 - 10
2 - 9
3 - 8
4 - 7
5 - 6
6 - 5
7 - 4
8 - 3
9 - 2
10 - 1
11 - 0
12 - -1
上述就是小編為大家分享的linux for循環怎么理解了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。