1、編寫函數,實現打印綠色OK和紅色FAILED,判斷是否有參數,存在為Ok,不存在為FAILED
[root@shell ~]# vim ok.sh +
#!/bin/bash
read -p "請輸入一個參數:" i ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
function ok_failed() { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? if [ -z "$i" ]; then ? ? ? ?#-z為檢查字符串長度是否為空,也可以用[ $# -eq 0 ]
? ? ? ? echo -e "\033[31mFAILED\033[0m" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
? ? else ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? echo -e "\033[32mOK\033[0m" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
? ? fi ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
} ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
ok_failed ? ? ? ? ? ? ? ? ? ? ? #若用[ $# -eq 0 ]這里調用函數需要加參數$i?
[root@shell ~]# sh ok.sh 1
請輸入一個參數:1
OK
[root@shell ~]# sh ok.sh 1
請輸入一個參數:
FAILED
2、編寫函數,實現判斷是否無位置參數,如無參數,提示錯誤
[root@shell ~]# vim test_weizhi.sh +
#!/bin/bash
read -p "請輸入參數,若不要參數直接回車:" i
function test_weizhi() {
? ? ? ? if [ $# -eq 0 ];then
? ? ? ? ? ? ? ? echo "Error無參數"
? ? ? ? else
? ? ? ? ? ? ? ? echo "輸入的了$#個參數"
? ? ? ? fi
}
test_weizhi $i
[root@shell ~]# sh test_weizhi.sh?
請輸入參數,若不要參數直接回車:1 2 3 4?
輸入的了4個參數
[root@shell ~]# sh test_weizhi.sh?
請輸入參數,若不要參數直接回車:
Error無參數
3、編寫函數實現兩個數字做為參數,返回最大值
方法一:
[root@shell ~]# vim max_value.sh +
#!/bin/bash
read -p "請輸入兩個數字:" num1 num2
function max_value() {
? ? ? ? if [ $num1 -gt $num2 ];then
? ? ? ? ? ? ? ? echo "最大值為$num1"
? ? ? ? elif [ $num1 -eq $num2 ];then
? ? ? ? ? ? ? ? echo "兩個參數相等"
? ? ? ? else
? ? ? ? ? ? ? ? echo "最大值為$num2"
? ? ? ? fi
}
max_value $num1 $num2
[root@shell ~]# sh max_value.sh?
請輸入兩個數字:1 1
兩個參數相等
[root@shell ~]# sh max_value.sh?
請輸入兩個數字:1 2
最大值為2
[root@shell ~]# sh max_value.sh?
請輸入兩個數字:3 2
最大值為3
方法二:
方法一過于簡單而且bug太多,咱還是要謹慎點,上點難度
[root@shell ~]# vim max_value2.sh +
#!/bin/bash
read -p "請輸入兩個參數:" -a num ? ? ? ?#-a為指定變量類型為數組
function max_value2() {
? ? ? ? if ! [ ${#num[@]} -eq 2 ];then
? ? ? ? ? ? ? ? echo "請輸入兩個參數"
? ? ? ? ? ? ? ? exit 2? ? ? ? ? ? ? ? ? ? ? ? ? ?????????#如輸入的參數不等于2,則退出腳本
? ? ? ? fi
for i in ${num[@]} ? ? ? ? ? ? ? ? ? ? ?????????#使用for循環遍歷num數組?
do
? ? ? ? if [[ ! $i =~ ^[0-9]+$ ]];then ?????????#變量i匹配使用正則表達式匹配是否為數字
? ? ? ? ? ? ? ? echo "請輸入純數字"
? ? ? ? ? ? ? ? exit 2
? ? ? ? fi
done
? ? ? ? if [ ${num[0]} -eq ${num[1]} ];then
? ? ? ? ? ? ? ? echo "請輸入兩個不同的數字"
? ? ? ? ? ? ? ? exit 2
? ? ? ? fi
max=${num[0]} ? ? ? ? ? ? ? ? ? ? ? ? ? #定義最大值的初始值,一般都定義為第一個元素,以第一個元素為基準依次和其它元素比較找到最大值
for i in ${num[@]}
do
? ? ? ? if [ $i -gt $max ];then
? ? ? ? ? ? ? ? max=$i
? ? ? ? fi
done
echo "最大值為$max"
}
max_value2
[root@shell ~]# sh max_value2.sh
請輸入兩個參數:1 2 3
請輸入兩個參數
[root@shell ~]# sh max_value2.sh
請輸入兩個參數:a 2
請輸入純數字
[root@shell ~]# sh max_value2.sh
請輸入兩個參數:1 1
請輸入兩個不同的數字
[root@shell ~]# sh max_value2.sh
請輸入兩個參數:1 2
最大值為2
[root@shell ~]# sh max_value2.sh
請輸入兩個參數:3 2
最大值為3
4、編寫函數,實現兩個整數位參數,計算加減乘除。
方法一:
[root@shell ~]# vim ?calculate.sh +
#!/bin/bash
read -p "請輸入兩個參數:" i v
function calculate() {
? ? ? ? echo "$i+$v=$((i+v))"
? ? ? ? echo "$i-$v=$((i-v))"
? ? ? ? echo "$i*$v=$((i*v))"
? ? ? ? echo "$i÷$v=$((i/v))"
}
calculate
[root@shell ~]# sh calculate.sh
請輸入兩個參數:4 2?
4+2=6
4-2=2
4*2=8
4÷2=2
方法二:
方法一為超級簡化版,咱還是上點難度吧,用if條件判斷吧!(也可以用case這里就懶得寫了都差不多)
[root@shell ~]# vim ?calculate2.sh +
#!/bin/bash
read -p "請輸入運算符:" a
read -p "請輸入第一個整數:" i
read -p "請輸入第二個整數:" v
?
function calculate2() {
? ? ? ? if [ "$a" = "+" ];then
? ? ? ? ? ? ? ? echo "$i+$v=$((i+v))"
? ? ? ? elif [ "$a" = "-" ];then
? ? ? ? ? ? ? ? echo "$i-$v=$((i-v))"
? ? ? ? elif [ "$a" = "*" ];then
? ? ? ? ? ? ? ? echo "$i*$v=$((i*v))"
? ? ? ? elif [ "$a" = "/" ];then
? ? ? ? ? ? ? ? echo "$i÷$v=$((i/v))"
? ? ? ? else
? ? ? ? ? ? ? ? echo "請輸入合法與算法符"
? ? ? ? fi
}
calculate2
[root@shell ~]# sh calculate2.sh +
請輸入運算符:+
請輸入第一個整數:4
請輸入第二個整數:7
4+7=11
[root@shell ~]# sh calculate2.sh +
請輸入運算符:-
請輸入第一個整數:6
請輸入第二個整數:3
6-3=3
[root@shell ~]# sh calculate2.sh +
請輸入運算符:*
請輸入第一個整數:4
請輸入第二個整數:3
4*3=12
[root@shell ~]# sh calculate2.sh +
請輸入運算符:/
請輸入第一個整數:6
請輸入第二個整數:2
6÷2=3
[root@shell ~]# sh calculate2.sh +
請輸入運算符:&
請輸入第一個整數:3
請輸入第二個整數:3
請輸入合法與算法符
5、將/etc/shadow文件的每一行作為元數賦值給數組
[root@shell ~]# vim?shadow.sh +
#!/bin/bash
function shadow() { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
? ? ? ? declare -A arry_shadow ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? while read line
? ? ? ? do ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? arry_shadow+=([$line]=$line) ? ?#將當前行的內容作為關聯數組arry_shadow的鍵和值
? ? ? ? done < /etc/shadow ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
? ? ? ? for key in "${!arry_shadow[@]}" ? ? ? ? #!為返回對應的鍵值
? ? ? ? do ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? echo "${arry_shadow[$key]}" ? ? ? ? ? ??
? ? ? ? done ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
} ? ? ? ?line=1 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
shadow?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??[root@shell ~]# sh shadow.sh?
6、使用關聯數組統計文件/etc/passwd中用戶使用的不同類型shell的數量
[root@shell ~]# awk -F: '{count[$NF]++} END{for (shell in count) print count[shell], shell}' /etc/passwd | sort -n
1 /bin/sync
1 /sbin/halt
1 /sbin/shutdown
2 /usr/sbin/nologin
3 /bin/bash
32 /sbin/nologin這里count[$NF]++定義一個關聯數字,元素出現次數進行累加,最后使用for循環遍歷count數組,打印元素出現的次數和其元素?