bash腳本編程實例
-
1.寫一個腳本
- 接受一個以上文件路徑作為參數
- 顯示每個文件擁有的行數
-
總結說明本次共為幾個文件統計了其行數
#!/bin/bash # read -p "please input some paths:" paths if [ -z $paths ];then echo "There are not any paths inputting." exit 1 fi declare -i files=0 for i in $paths;do if [ ! -e $i ];then echo "$i is not existing." continue elif [ ! -f $i ];then echo "$i is not a file" continue else count=`wc -l $i|cut -d' ' -f2` echo "$i has $count lines." files=$[$files+1] fi done echo "counting lines for $files files."
-
2.寫一個腳本
- 傳遞兩個以上字符串當做用戶名
- 創建這些用戶,且密碼同為用戶名
-
總結說明共創建了幾個用戶
#!/bin/bash # read -p "please input some usernames:" usernames if [ -z $usernames ];then echo "There are not any usernames inputting." exit 1 fi declare -i users=0 for i in $usernames;do if ! id $i &> /dev/null;then useradd $i echo $i|passwd --stdin $i users=$[$users+1] else echo "$i has existed." continue fi done echo "createing users for $users users."
-
3.寫一個腳本,新建20個用戶,visitor1-visitor20,計算他們的ID之和
#!/bin/bash # declare -i ids=0 for i in {1..20};do if ! id visitor$i &> /dev/null;then useradd visitor$i echo "create a new user named visitor$i." else echo "this user named visitor$i has existed." fi id=`id -u visitor$i` ids=$[$ids+$id] done echo "id count for these users are $ids."
原創文章,作者:N27_xiaoni,如若轉載,請注明出處:http://www.www58058.com/84388
這幾個腳本還是用到蠻多知識點的,很不錯,再接再勵。