標籤:shell中的for流程式控制制
for迴圈:
在shell中用於迴圈。類似於其他程式設計語言中的for,但又有些不同。for迴圈是Bash中最常用的文法結構。
常用格式格式一
for 變數
do
語句
done
格式二
for 變數 in 列表
do
語句
done
格式三
for ((變數=初始值; 條件判斷; 變數變化))
do
語句
done
如果do和for想在同一行,需要這樣
for arg in [list] ;do
commands...
done
arg將順次取得list中的變數
***************************************************************************************************
1、 for((i=1;i<=10;i++));do echo $(expr $i \* 4);done
2、在shell中常用的是 for i in $(seq 10)
3、for i in `ls`
4、for i in ${arr[@]}
5、for i in $* ; do
6、for File in /proc/sys/net/ipv4/conf/*/accept_redirects; do
7、for i in f1 f2 f3 ;do
8、for i in *.txt
9、for i in $(ls *.txt)
for in語句與` `和$( )合用,利用` `或$( )的將多行合為一行的缺陷,實際是合為一個字串數組
10、LIST="rootfs usr data data2"
for d in $LIST; do
用for in語句自動對字串按空格遍曆的特性,對多個目錄遍曆
11、for i in {1..10}
12、for i in stringchar {1..10}
13、awk ‘BEGIN{for(i=1; i<=10; i++) print i}‘
注意:AWK中的for迴圈寫法和C語言一樣的
***************************************************************************************************
SHELL中的for流程式控制制