一、初级练习题
1、打印99乘法表
for m in $(seq 9)
do
for n in $(seq 9)
do
if [ ${m} -ge ${n} ]
then
echo " ${n} * ${m}\c"
fi
done
printf " \n"
done
2、读取score.txt文件的内容,然后计算平均分
cat score.txt | while read line
do
arr=(${line})
if [ ${arr[0]} != "姓名" ]
then
let avg=(${arr[1]}+${arr[2]}+${arr[3]})/3
echo "${arr[0]}的平均分为:${avg}"
fi
done
3、使用for循环在/clsn目录下批量创建10个html文件,其中每个文件需要包含10个随机小写字母加固定字符串clsn
path="/clsn"
rm -rf ${path}
test -d ${path} || mkdir ${path}
for((i=0;i<10;i++))
do
fileName=`cat /dev/urandom | head -n 10 | md5sum | head -c 10`
fileName="${fileName}-clsn.txt"
touch "${path}/${fileName}"
done
4、将练习题3中结果文件名中的clsn字符串全部改成znix(最好用for循环实现),并且将扩展名txt部改成大写
for file in `ls ${path}`
do
oldFileName=${file}
newFileName=${oldFileName/clsn/znix}
newFileName=${newFileName/txt/TXT}
mv "${path}/${oldFileName}" "${path}/${newFileName}"
done
echo -e "\n"
echo "5、实现一个1-99的抓阄程序"
function getRandom(){
min=$1
max=$2
r=${RANDOM}
#先得出max和min的差值
dis=$((${max}-${min}))
res=$((${r}%${dis}+${min}))
}
scores=($(seq 99))
for((i=1;i<100;i++))
do
echo "请输入您的名字进行抓阄"
read username
length=${#scores[@]}
getRandom 0 ${length}-1
#获取随机到的下标
index=${res}
score=${scores[index]}
#删除下标对应的数据
unset scores[${index}]
#将剩下的数据构造成一个新的数组,以放下下次抓阄
scores=(${scores[*]})
echo ${scores[*]}
echo -e "${username}的抓阄结果是:${score}\n"
done