SHELL中的for流程式控制制

來源:互聯網
上載者:User

標籤: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流程式控制制

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.