shell exercises -26

Questions asked

Write a script, turn to the / etc / passwd each user to say hello, and say what each other's ID is like:

Hello, root,your UID is 0.

Answers

#!/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

Questions asked

There is a file test.xml linux system / home directory, as follows:

<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>

Please write the comment section to delete the contents of the shell script file, get the file contents of all artifactItem and progressive output using the following format:
artifactItem: groupId: artifactId: aaa

Answers

#!/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

Questions asked

Please write a shell function, a function named f_judge, the following functions

  1. When the / home / log directory exist under the / home directory at the beginning of all tmp file or directory to / home / log directory.

  2. When the / home / log directory does not exist, create it, and then exit.

Answers

#!/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

Questions asked

linux system, there is a directory file ip-pwd.ini / root / at, as follows:

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

The file format of each line are linux server ip, root user name, root password, use a shell batch process all tomcat kill off these servers.

Answers

#!/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

Questions asked

Write a script to find / data / log 'directory, create time is 3 days before the suffix is ​​* .log files after the package sent to the next / data / log service on 192.168.1.2, and delete the original file .log, only reserved file package.

Answers

#!/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

Questions asked

The following text, of which the first 5 rows content

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

After shell script processing, in the following output format:

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

Answers

#!/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

Guess you like

Origin blog.51cto.com/865516915/2436492