shell腳本學習(三)

test1:for循環

#!/bin/bash
for var in one two three four
do
        echo "The number is $var"
done

echo "Now show read values from file"
file="place"
for var in $(cat $file)
do
        echo "Visit beautiful $var"
done


cat place:
Tokyo
New York
HongKong
Landon


結果:
The number is one
The number is two
The number is three
The number is four
Now show read values from file
Visit beautiful Tokyo
Visit beautiful New
Visit beautiful York
Visit beautiful HongKong
Visit beautiful Landon

test2: shell腳本中,對於循環的輸出、可以使用管道或進行重定向。可以通過done命令之後添加一個處理命令來實現。

#!/bin/bash
#注意for後面的括號
for (( a = 1; a < 10; a++ ))
do
        echo "The number is $a"
done > test14test.txt
echo "Command is finished!"


cat test14test.txt
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9


結果:
Command is finished!

循環是對系統數據進行迭代最常用的方法,無論是目錄中的文件還是文件中的數據,下面是一些簡單的例子(example1、example2)

example1:查找可執行文件。使用命令行運行一個程序時,Linux系統會搜索一系列目錄來查找對應的文件,這些目錄被定義在PATH目錄中。如果想找出那些可執行文件是可用的,只需要掃描PATH環境變量中所有目錄就行了,這個腳本就是做這件事。

IFS分隔符,即內部字段分隔符,默認情況下會將 空格、製表符、換行符當做字段分隔符。如果bash shell在數據中看到了這些字符中的任意一個,就會假定這表明了列表中一個新數據字段的開始。要解決這個問題,可以在shell腳本中臨時更改IFS的值來限制分隔符的字符。例如:IFS=$'\n' (換行符)即:shell忽視空格和製表符,把換行當成新數據字段的開始。

如果冒號作爲分隔符,則:IFS=:

如果指定多個分隔符(換行、冒號、分號、雙引號),則:IFS=$'\n':;"

#!/bin/bash
#1. 第一個for循環,對環境變量PAHT目錄進行迭代.即:找到所有的目錄
IFS=:
for folder in $PATH
do
        echo "Now print all folder:$folder:"
        #2. 這樣所有的目錄都放在了folder中,第二個for用來遍歷目錄下的所有文件
        for file in $folder/*
        do #3. 最後檢查每個文件是否具有可執行權限
                if [ -x $file ]
                then
                        echo "Now print all can excult file:$file"
                fi
        done
done

結果:
Now print all folder:/usr/local/sbin/uds/bin:
Now print all can excult file:/usr/local/sbin/uds/bin/conf.ini
Now print all can excult file:/usr/local/sbin/uds/bin/Guide
Now print all can excult file:/usr/local/sbin/uds/bin/libcrypto.so.1.0.0
Now print all can excult file:/usr/local/sbin/uds/bin/libssl.so.1.0.0
Now print all can excult file:/usr/local/sbin/uds/bin/libuds.so
... ...
... ...

 

example2:創建多個用戶賬戶。擁有大量用戶時,爲每一個用戶創建賬戶,可以使用while循環。前提是需要把所有用戶放在文本文件中,然後使用腳本創建。這個文本文件的格式:userid,username。用逗號分隔。讀取文件數據時,將IFS設置成逗號,然後使用read命令讀取文件中的各行。如;while IFS=',' read -r userid name

read命令會自動讀取.csv文件的下一行內容,當read命令返回FALSE(讀取完整個文件時),while命令就退出了。

運行後,命令:tail /etc/passwd,發現用戶已經添加進去了。

#!/bin/bash
input="users.csv"
while IFS=',' read -r userid username
do
        echo "adding user:$userid"
        useradd -c "$name" -m "$userid"
done < "$input"


cat user.csv
xt,Xing
ab,AngleBaby
jj,Junjie


結果:
adding user:xt
useradd:用戶“xt”已存在
adding user:ab
useradd:用戶“ab”已存在
adding user:jj
useradd:用戶“jj”已存在

test3:演示引用庫文件(自定義的庫文件)。

#!/bin/bash
#myFunc庫與該腳本在同一個文件夾,因此可以使用下面的形式
. ./myFuncs

val1=10
val2=20
result1=$(addem $val1 $val2)
result2=$(multem $val1 $val2)

echo "The addem result is $result1"
echo "The multem result is $result2"


cat myFuncs
#!/bin/bash
#自定義庫文件
#注意,函數名和{之間一定要有空格,不然會說找不到這個函數
function addem {
        echo $[ $1 + $2 ]
}

function multem {
        echo $[ $1 * $2 ]
}



結果:
The addem result is 30
The multem result is 200

test4:正則表達式實戰 - 目錄文件計數。

#!/bin/bash
#統計PATH路徑的各個目錄下的文件數目
mypath=$(echo $PATH | sed 's/:/ /g')
count=0
for dir in $mypath
do
        check=$(ls $dir)
        for item in $check
        do
                count=$[ $count + 1 ]
        done
        echo "$dir - $count"
        count=0
done

結果:
/usr/local/sbin/uds/bin - 15
ls: 無法訪問'/usr/local/arc/arcanist/bin': No such file or directory
/usr/local/arc/arcanist/bin - 0
ls: 無法訪問'/usr/bin/php/bin': Not a directory
/usr/bin/php/bin - 0
ls: 無法訪問'/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.25-4.b18.fc21.x86_64/bin': No such file or directory
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.25-4.b18.fc21.x86_64/bin - 0
ls: 無法訪問'/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.25-4.b18.fc21.x86_64/jre/bin': No such file or directory
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.25-4.b18.fc21.x86_64/jre/bin - 0
/usr/lib64/qt-3.3/bin - 0
/usr/lib64/ccache - 9
/usr/local/bin - 2
/usr/bin - 2226
/usr/local/sbin - 2
/usr/sbin - 712
ls: 無法訪問'/home/[email protected]/.local/bin': No such file or directory
/home/[email protected]/.local/bin - 0
ls: 無法訪問'/home/[email protected]/bin': No such file or directory
/home/[email protected]/bin - 0

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章