您的位置:首页 > 运维架构 > Shell

shell实战训练营Day26

2019-01-22 20:32 691 查看

判断所给目录内哪些二级目录下有没有text.txt文件。
有text.txt文件的二级目录,计算出该test.txt文件里面所给出单词的次数。
假如脚本名字为1.sh, 运行脚本的格式为 ./1.sh 123 root,其中123为目录名字,而root为要计算数量的单词

#!/bin/bash
if [ $# -ne 2 ]
then
echo "请提供两个参数,第一个参数是目录名字,第二个参数是单词"
exit
fi

cd $1
for f in

ls .

do
if [ -d $f ]
then
if [ -f $f/test.txt ]
then
n=
grep -cw "$2" $f/test.txt

echo "$1/$f目录下面有test.txt, 该test.txt里面的有$n个$2."
fi
fi
done

交互式脚本,根据提示,需要用户输入一个数字作为参数,最终打印出一个正方形。在这里我提供一个linux下面的特殊字符■,可以直接打印出来。

示例: 如果用户输入数字为5,则最终显示的效果为

■ ■ ■ ■ ■
■ ■ ■ ■ ■
■ ■ ■ ■ ■
■ ■ ■ ■ ■
■ ■ ■ ■ ■
#!/bin/bash
while :
do
read -p "Please input a nuber: " n
n1=

echo $n|sed 's/[0-9]//g'

if [ -n "$n1" ]
then
echo "$n is not a nuber."
continue
else
break
fi
done

for i in

seq 1 $n

do
for j in
seq 1 $n

do
echo -n "■ "
done
echo
done

写一个脚本,依次向/etc/passwd中的每个用户问好,并且说出对方的ID是什么,如:

Hello, root,your UID is 0.

#!/bin/bash
cat /etc/passwd |while read line
do
username=

echo $line|awk -F ':' '{print $1}'

uid=
echo $line|awk -F ':' '{print $3}'

echo "Hello, $username, your uid is $uid."
done

linux系统 /home目录下有一个文件test.xml,内容如下:

<configuration>
<artifactItems>
<artifactItem>
<groupId>zzz</groupId>
<artifactId>aaa</artifactId>
</artifactItem>
<artifactItem>
<groupId>xxx</groupId>
<artifactId>yyy</artifactId>
</artifactItem>

请写出shell脚本删除文件中的注释部分内容,获取文件中所有artifactItem的内容,并用如下格式逐行输出: artifactItem:groupId:artifactId:aaa #!/bin/bash sed '//d' test.xml > test2.xml egrep -n '
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息