1、写一个 bash脚本以输出数字 0 到 100 中 7 的倍数(0 7 14 21...)的命令。
vim /shell/homework1.sh
#!/bin/bash
for num in {1..100}
do
if [[ num%7 -eq o ]];then
echo $num
fi
done
执行输出脚本查看输出结果
输出结果:
2、写一个 bash脚本以统计一个文本文件 nowcoder.txt中字母数小于8的单词。
假设 nowcoder.txt 内容如下:
how they are implemented and applied in computer
首先创建一个nowcoder.txt文件,用作测试
vim /test/nowcoder.txt
how they are implemented and applied in computer
?然后配置脚本
vim /shell/homework2.sh
#!/bin/bash
for n in $(cat /test/nowcoder.txt)
do
if [ `expr length $n` -lt 8 ]
then
echo $n
fi
done
执行脚本查看输出结果
输出结果:
3、写一个 bash脚本以实现一个需求,去掉输入中含有this的语句,把不含this的语句输出
示例:
假设输入如下:
that is your bag
is this your bag?
to the degree or extent indicated.
there was a court case resulting from this incident
welcome to nowcoder
首先创建一个测试文件,用来储存输入的内容
vim /test/test1
编写脚本文件
#!/bin/bash
while true
do
read -p "input(if you want to stop inputting,please enter stop)" mark
if [ "$mark" = "stop" ];then
break
fi
echo $mark >> /test/test1
done
echo "Output results"
grep -w this -v /test/test1
echo > /test/test1
执行脚本,查看输出结果