RHCE作业3 shell编程

1、写一个 bash脚本以输出数字 0 到 100 中 7 的倍数(0 7 14 21...)的命令。

利用seq生成一百个数 使用if语句判断是否能整除7 如果能 就是7的倍数:

#!/bin/bash
#########################
#File name:seven.sh
#Created time:2023-12-22 21:25:58
#Description:
#########################
for i in `seq 100`
do
	if [ $((i%7)) -eq 0 ];then
	echo $i
	fi
done

方法2: 利用while循环 直到 7倍的i大于 100 停止循环

#!/bin/bash
#########################
#File name:seven2.sh
#Created time:2023-12-22 21:35:11
#Description:
#########################
i=1
while [ $[ 7*i ] -lt 100  ]
do

	echo $[ 7*i ] 
	let i++
done


2、写一个 bash脚本以统计一个文本文件 nowcoder.txt中字母数小于8的单词。
示例:
假设 nowcoder.txt 内容如下:
how they are implemented and applied in computer

采用双层循环 外层while循环读取文件 内层for 循环 将内容以列表形式读出

#!/bin/bash
#########################
#File name:read.sh
#Created time:2023-12-22 21:40:01
#Description:
#########################
while read line
do
	for i in $line
	do
		let sum++
	done

done < nowcoder.txt
echo "the words number is $sum"


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
你的脚本获取以上输入应当输出:
that is your bag
to the degree or extent indicated.
welcome to nowcoder

将输入读入到文件中 然后对文件grep过滤 得到结果

#!/bin/bash
#########################
#File name:nothis.sh
#Created time:2023-12-22 21:53:53
#Description:
#########################
read -p "if you want to stop you can input yes" input
while [[ "$input"x != "yes"x ]]
do	
	read  input
	echo $input >> nothis.txt 
done
grep -wv "this" nothis.txt
echo > nothis.txt

posted @ 2023-12-22 22:37  f0r9  阅读(2)  评论(0编辑  收藏  举报  来源