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

shell习题-26

2019-09-07 20:42 1286 查看

题目要求

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

Hello, root,your UID is 0.

参考答案

#!/bin/bash
#这个脚本用来问候用户
#作者:猿课-阿铭 www.apelearn.com
#日期:2018-12-11

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>
<!-- </artifactItem><groupId>some groupId</groupId>
<version>1.0.1.2.333.555</version> </artifactItem>-->
</artifactItems>
</configuration>

请写出shell脚本删除文件中的注释部分内容,获取文件中所有artifactItem的内容,并用如下格式逐行输出:
artifactItem:groupId:artifactId:aaa

参考答案

#!/bin/bash
#这个脚本用来格式化xml文件
#作者:猿课-阿铭 www.apelearn.com
#日期:2018-12-11

sed '/<!--.*-->/d' test.xml > test2.xml
egrep -n '<!--|\-\->' test2.xml |awk -F ':' '{print $1}' > /tmp/line_number1.txt
n=`wc -l /tmp/line_number1.txt|awk '{print $1}'`
n1=$[$n/2]
for i in `seq 1 $n1`
do
j=$[$i*2]
k=$[$j-1]
x=`sed -n "$k"p /tmp/line_number1.txt`
y=`sed -n "$j"p /tmp/line_number1.txt`
sed -i "$x,$y"d test2.xml
done

grep -n 'artifactItem>' test2.xml |awk '{print $1}' |sed 's/://' > /tmp/line_number2.txt
n2=`wc -l /tmp/line_number2.txt|awk '{print $1}'`

get_value(){
sed -n "$1,$2"p test2.xml|awk -F '<' '{print $2}'|awk -F '>' '{print $1,$2}' > /tmp/value.txt

cat /tmp/value.txt|while read line
do
x=`echo $line|awk '{print $1}'`
y=`echo $line|awk '{print $2}'`
echo artifactItem:$x:$y
done
}

n3=$[$n2/2]
for j in `seq 1 $n3`
do
m1=$[$j*2-1]
m2=$[$j*2]
nu1=`sed -n "$m1"p /tmp/line_number2.txt`
nu2=`sed -n "$m2"p /tmp/line_number2.txt`
nu3=$[$nu1+1]
nu4=$[$nu2-1]
get_value $nu3 $nu4
done

题目要求

请撰写一个shell函数,函数名为 f_judge,实现以下功能

  1. 当/home/log目录存在时将/home目录下所有tmp开头的文件或目录移到/home/log目录。

  2. 当/home/log目录不存在时,创建该目录,然后退出。

参考答案

#!/bin/bash
#这个脚本用来写一个小函数
#作者:猿课-阿铭 www.apelearn.com
#日期:2018-12-11

f_judge()
{
if [ -d /home/log ]
then
#find /home -name "tmp*" |xargs -i mv {} /home/log/
find /home -name "tmp*" -exec mv {} /home/log/ \;
else
mkdir /home/log
exit
fi
}

f_judge

题目要求

linux系统中,目录/root/下有一个文件ip-pwd.ini,内容如下:

10.111.11.1,root,xyxyxy
10.111.11.2,root,xzxzxz
10.111.11.3,root,123456
10.111.11.4,root,xxxxxx
……

文件中每一行的格式都为linux服务器的ip,root用户名,root密码,请用一个shell批量将这些服务器中的所有tomcat进程kill掉。

参考答案

#!/bin/bash
#这个脚本用来批量杀tomcat进程
#作者:猿课-阿铭 www.apelearn.com
#日期:2018-12-12

cat > kill_tomcat.expect <<EOF
#!/usr/bin/expect
set passwd [lindex \$argv 0]
set host [lindex \$argv 1]
spawn ssh root@\$host

expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "\$passwd\r" }
}

expect "]*"
send "killall java\r"
expect "]*"
send "exit\r"
EOF

chmod a+x kill_tomcat.expect

cat ip-pwd.ini|while read line
do
ip=`echo $line |awk -F ',' '{print $1}'`
pw=`echo $line |awk -F ',' '{print $3}'`
./kill_tomcat.expect $pw $ip
done

题目要求

写一个脚本查找/data/log目录下,创建时间是3天前,后缀是*.log的文件,打包后发送至192.168.1.2服务上的/data/log下,并删除原始.log文件,仅保留打包后的文件。

参考答案

#!/bin/bash
#这个脚本用来查找老日志打包
#作者:猿课-阿铭 www.apelearn.com
#日期:2018-12-12

cd /data/log
find . -type f -name "*.log" -mtime +3 > /tmp/old_log
d=`date +%F`

tar czf $d.tar.gz `cat /tmp/old_log|xargs`
rsync -a $d.tar.gz 192.168.1.2:/data/log/
cat /tmp/old_log|xargs rm

题目要求

有如下文本,其中前5行内容为

1111111:13443253456
2222222:13211222122
1111111:13643543544
3333333:12341243123
2222222:12123123123

用shell脚本处理后,按下面格式输出:

[1111111]
13443253456
13643543544
[2222222]
13211222122
12123123123
[3333333]
12341243123

参考答案

#!/bin/bash
#这个脚本用来处理文本
#作者:猿课-阿铭 www.apelearn.com
#日期:2018-12-12

for w in `awk -F ':' '{print $1}' 3.txt |sort |uniq`
do
echo "[$w]"
awk -v w2=$w -F ':' '$1==w2 {print $2}' 3.txt
done
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: