1、编写函数,实现打印绿色oK和红色FAILED,判断是否有参数,存在为ok,不存在为FAILED
#!/bin/bash
colour() {
if [ $# -ne 0 ];then
echo -e "\033[31m OK \033[0m"
else
echo -e "\033[32m FAILED \033[0m"
fi
}
read -p "请输入要传递的参数:" c
colour $c
2、编写函数,实现判断是否无位置参数,如无参数,提示错误
#!/bin/bash
fun(){
if [ $# -eq 0 ];then
echo "error"
else
echo "haveing"
fi
}
read -p "please input : " i
fun $i
3、编写函数实现两个数字做为参数,返回最大值
#!/bin/bash
fun(){
if [ $num1 -gt $num2 ];then
echo "the num1 bigger"
elif [ $num1 -lt $num2 ];then
echo "the num2 bigger"
else
echo "equal"
fi
}
read -p "please input two nums:" num1 num2
fun $num1 $num2
4、编写函数,实现两个整数位参数,计算加减乘除。
#!/bin/bash
fun(){
echo a+b=$((a+b))
echo a-b=$((a-b))
echo a*b=$((a*b))
echo a/b=$((a/b))
}
read -p "please input int number:" a b
fun $a $b
5、将/etc/ shadow文件的每一行作为元数赋值给数组
#!/bin/bash
num=`wc -l < /etc/shadow`
for i in `seq $num`
do
arr[$i]=`head -$i /etc/shadow | tail -1`
done
for i in ${arr[*]}
do
echo $i
done
6、使用关联数组统计文件/etc/passwd中用户使用的不同类型shell的数量
#!/bin/bash
declare -A shells
while read line
do
type=`echo $line | cut -d : -f7`
let shells[$type]++
done </etc/passwd
for i in ${!shells[@]}
do
echo "$i : ${shells[$i]}"
done
7、使用关联数组按扩展名统计指定目录中文件的数量
read -p "请输入要查看的目录:" path
[ -z $path ]&& echo "输入错误:输入为空" && exit 1
cd $path
for i in `ls`
do
if [ -d $i ]; then
echo $i >>~/homework3/ceshi.txt
fi
done
declare -A arroy
a=0
while read line
do
let arroy[$a]++
done <~/homework/ceshi.txt
b=0
for b in ${!arroy[*]}
do
echo "目录文件共有: ${arroy[$b]}"
done