在寫shell指令碼時,常常會用到迴圈語句。這裡簡單總結下常用的3種迴圈語句,以便以後能快速地參考寫出,廢話不說,開始demo: 1、for 主要用於:在以知序列裡遍曆元素 [plain] view plaincopy #! /bin/sh - if [ $# -ge 1 ]; then path=$@ else path=*.sh fi for i in $path do ls -lh $i
使用方法將其複製,儲存為一個shell檔案,比如security.sh.將其上傳到linux伺服器上,執行sh security.sh,就可以使用該指令碼了! 代碼如下: #!/bin/sh # desc: setup linux system security # author:coralzd # powered by www.freebsdsystem.org # version 0.1.2 written by 2011.05.03 #account
1. 單個檔案的讀取 在shell指令碼下,可以多種方式實現按行讀取檔案,如下: for line in `cat ${input_filename}` do echo $line done while read line do echo $line done < ${input_filename} 其中第二種方式是將檔案重新導向到標準輸入中 2.
這篇文章主要介紹了Shell中的for和while迴圈詳細總結,本文講解了for迴圈的數欄位形式、詳細列出、對檔案進行迴圈,while迴圈的三種使用場合等內容,需要的朋友可以參考下 一、for迴圈 1.數欄位形式 代碼如下: for i in {1..10} do echo $i done 2.詳細列出(字元且項數不多) 代碼如下: for File in 1 2 3 4 5 do echo
這篇文章主要介紹了Shell中的for迴圈總結,本文講解了shell中for迴圈用法、shell下for迴圈的幾種方法等內容,需要的朋友可以參考下 關於shell中的for迴圈用法很多,一直想總結一下,今天網上看到上一篇關於for迴圈用法的總結,感覺很全面,所以就轉過來研究研究,嘿嘿... 代碼如下: for((i=1;i<=10;i++));do echo $(expr $i * 4);done 在shell中常用的是 for i in $(seq 10)