1. 計算「/etc」目錄中全部「*.conf」形式的配置文件所佔用的總空間大小。
[root@Carlton scripts]# vim mmm.sh
#!/bin/bash
#cd /etc
Number=$(ls -l $(find /etc -type f -name *.conf)| awk '{print $5}' )
Total=0
for i in $Number
do
Total=`expr $Total + $i`
done
echo "The Total size is $Total kb"
"mmm.sh" 10L, 184C written
[root@Carlton scripts]# ./mmm.sh
The Total size is 362579 kb
2.由用戶從鍵盤輸入一個大於1的整數(如50),並計算從1到該數之間各整數的和。
#/bin/bash
read -p "Please input one number(>1): " UP
sum =0
i=1
#while [ $UP -ge $i ]
#do
# sum=$[ $sum + $UP]
# UP=$[$UP-1]
#i=`expr $i + 1`
#done
while [ $i -le $UP ]
do
sum=`expr $sum + $i`
i=`expr $i + 1`
done
echo "The sum of 1- $UP is : $sum "
"lll.sh" 20L, 303C written
[root@Carlton scripts]# ./lll.sh
Please input one number(>1): 8
sum: =0: No such file or directory
The sum of 1- 8 is : 36
上面兩種方法均可以
3. 批量添加20個系統用戶帳號,用戶名稱依次爲「stu1」、「stu2」、「stu3」、……「stu20」,各用戶的初始密碼均設置爲「123456」。繼續編寫一個批量刪除用戶的腳本程序,將添加的20個用戶刪除。
[root@Carlton September]# ./addstu20.sh
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
[root@Carlton September]# grep "stu" /etc/passwd
stu1:x:504:504::/home/stu1:/bin/bash
stu2:x:505:505::/home/stu2:/bin/bash
stu3:x:506:506::/home/stu3:/bin/bash
stu4:x:507:507::/home/stu4:/bin/bash
stu5:x:508:508::/home/stu5:/bin/bash
stu6:x:509:509::/home/stu6:/bin/bash
stu7:x:510:510::/home/stu7:/bin/bash
stu8:x:511:511::/home/stu8:/bin/bash
stu9:x:512:512::/home/stu9:/bin/bash
stu10:x:513:513::/home/stu10:/bin/bash
stu11:x:514:514::/home/stu11:/bin/bash
stu12:x:515:515::/home/stu12:/bin/bash
stu13:x:516:516::/home/stu13:/bin/bash
stu14:x:517:517::/home/stu14:/bin/bash
stu15:x:518:518::/home/stu15:/bin/bash
stu16:x:519:519::/home/stu16:/bin/bash
stu17:x:520:520::/home/stu17:/bin/bash
stu18:x:521:521::/home/stu18:/bin/bash
stu19:x:522:522::/home/stu19:/bin/bash
stu20:x:523:523::/home/stu20:/bin/bash
[root@Carlton September]# ./deluser20
[root@Carlton September]# grep "stu" /etc/passwd
[root@Carlton September]# cat addstu20.sh
#!/bin/bash
i=1
while [ $i -le 20 ]
do
useradd stu$i
echo "123456" |passwd --stdin stu$i
i=`expr $i + 1 `
done
[root@Carlton September]# cat deluser20
#!/bin/bash
i=1
while [ $i -le 20 ]
do
userdel -r stu$i
i=`expr $i + 1`
done
cat /etc/passwd |grep stu
4.由用戶從鍵盤輸入一個字符,並判斷該字符是否爲字母、數字或者其餘字符,並輸出相應的提示信息。
[root@Carlton September]# cat information.sh
#!/bin/bash
read -p "Please input any words in this screen : " XX
case $XX in
[a-z]|[A-Z])
echo "this is english words"
;;
[0-9])
echo "NUmber"
;;
*)
echo "It is not number or words"
esac
[root@Carlton September]# ./information.sh
Please input any words in this screen : 1
NUmber
[root@Carlton September]# ./information.sh
Please input any words in this screen : e
this is english words
[root@Carlton September]# ./information.sh
Please input any words in this screen : ^[[17~
It is not number or words
5.編寫一個shell程序,計算多個整數值的和,須要計算的各個數值由用戶在執行腳本時做爲命令行參數給出。
[root@Carlton September]# vim kkk.sh
#!/bin/bash
Result=0
while [ $# -gt 0 ] $#傳參的個數,$1第一個傳參的個數
do
Result=`expr $Result + $1`
shift
done
echo "the sum is "$Result""
~
"kkk.sh" 9L, 118C written
[root@Carlton September]# ./kkk.sh 3 3
the sum is 6
[root@Carlton September]# sh -x kkk.sh 34 55
+ Result=0
+ '[' 2 -gt 0 ']'
++ expr 0 + 34
+ Result=34
+ shift
+ '[' 1 -gt 0 ']'
++ expr 34 + 55
+ Result=89
+ shift
+ '[' 0 -gt 0 ']'
+ echo 'the sum is 89'
the sum is 89
6. 計算任意整數之和
[root@Carlton September]# ./kkk.sh 23 44
the sum is 67
[root@Carlton September]# cat kkk.sh
#!/bin/bash
Result=0
while [ $# -gt 0 ]
do
Result=`expr $Result + $1`
shift
done
echo "the sum is "$Result""
7.在腳本中定義一個help函數,當用戶輸入的腳本參數不是「start」或「stop」時,加載該函數並給出關於命令用法的幫助信息,不然給出對應的提示信息
[root@Carlton September]# vim jjj.sh
#!/bin/bash
help() {
echo "Usage: "$0" start|stop"
}
case "$1" in
start)
echo "Starting ..."
;;
stop)
echo "stop..."
;;
*)
help
esac
"jjj.sh" 15L, 162C written
[root@Carlton September]# ./jjj.sh
Usage: ./jjj.sh start|stop
[root@Carlton September]# ./jjj.sh stop
stop...
[root@Carlton September]# ./jjj.sh sart
Usage: ./jjj.sh start|stop
[root@Carlton September]# ./jjj.sh start
Starting ...
8. 在腳本中定義一個加法函數,用於計算兩個數的和,並調用該函數分別計算12+3四、56+789的和。
[root@Carlton September]# vim uuu.sh
#!/bin/bash
adder() {
echo `expr $1 + $2`
}
adder 12 34
adder 56 789
"uuu.sh" [New] 7L, 76C written
[root@Carlton September]# chmod +x uuu.sh
[root@Carlton September]# ./uuu.sh
46
845
9. 刪除系統中的stu1~stu20各用戶帳號,但stu八、stu18除外。
#!/bin/bash
i=1
while [ $i -le 20 ]
do
if [ $i -eq 8 ] || [ $i -eq 18 ] ; then
let i++
continue
fi
userdel -r stu$i
let i++
done
"ggg.sh" [New] 12L, 160C written
[root@Carlton September]# chmod +x ggg.sh
[root@Carlton September]# ./ggg.sh
userdel: user 'stu1' does not exist
userdel: user 'stu2' does not exist
userdel: user 'stu3' does not exist
userdel: user 'stu4' does not exist
userdel: user 'stu5' does not exist
userdel: user 'stu6' does not exist
userdel: user 'stu7' does not exist
userdel: user 'stu9' does not exist
userdel: user 'stu10' does not exist
userdel: user 'stu11' does not exist
userdel: user 'stu12' does not exist
userdel: user 'stu13' does not exist
userdel: user 'stu14' does not exist
userdel: user 'stu15' does not exist
userdel: user 'stu16' does not exist
userdel: user 'stu17' does not exist
userdel: user 'stu19' does not exist
userdel: user 'stu20' does not exist
[root@Carlton September]# ./addstu20.sh
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
passwd read,is 123456
[root@Carlton September]# ./ggg.sh
[root@Carlton September]# grep "stu" /etc/passwd
stu8:x:511:511::/home/stu8:/bin/bash
stu18:x:521:521::/home/stu18:/bin/bash
[root@Carlton September]#
10.循環提示用戶輸入字符串,並將每次輸入的內容保存到臨時文件「/tmp/input.txt」中,當用戶輸入「END」字符串時退出循環體,並統計出input.txt文件中的行數、單詞數、字節數等信息,統計完後刪除臨時文件。
[root@Carlton September]# vim fff.sh
#!/bin/bash
while true
do
read -p "Input a string: " STR
echo $STR >> /tmp/input.txt
if [ "$STR" = "END" ] ; then
break
fi
done
wc /tmp/input.txt
rm -f /tmp/input.txt
"fff.sh" [New] 12L, 204C written
[root@Carlton September]# chmod +x fff.sh
[root@Carlton September]# ./fff.sh
Input a string: 123
Input a string: 213
Input a string: 3254
Input a string: 3465
Input a string: sadfse
Input a string: END
6 6 29 /tmp/input.txt