月份:2017年9月


shell习题-三行变一行


比如1.txt内容
1
2
3
4
5
6
7

处理后应该是
1 2 3
4 5 6
7

 

参考答案:

sed 'N;N;s/\n/ /g' 1.txt

shell习题-判断pid是否一致


先普及一小段知识,我们用ps aux可以查看到进程的PID,而每个PID都会在/proc内产生。如果查看到的pid而proc内是没有的,则是进程被人修改了,这就代表你的系统很有可能已经被入侵过了。

请大家用上面知识编写一个shell,定期检查下自己的系统是否被人入侵过。

 

参考答案:

#!/bin/bash

ps aux|awk '/[0-9]/ {print $2}'|while read pid
do
    result=`find /proc/ -maxdepth 1 -type d -name "$pid"`
    if [ -z $result ]; then
        echo "$pid abnormal!"
    fi
done

shell习题-更改后缀名


1 编写一个名为chname的程序,将当前目录下所有的.txt文件更名为.doc文件。

2 编写一个名为chuser的程序,执行中每隔5分钟检查指定的用户是否登录系统,用户名从命令行输入;如果指定的用户已经登录,则显示相关信息。

 

参考答案:

1
#!/bin/bash
find . -type f -name "*.txt" > /tmp/txt.list
for f in `cat /tmp/txt.list`
do
    n=`echo $f|sed -r 's/(.*)\.txt/\1/'`
    echo "mv $f $n.doc"
done

2 
#!/bin/bash
read -p "Please input the username: " user
while :
do
    if who | grep -qw $user
    then
        echo $user login.
    else
        echo $user not login.
    fi
    sleep 300
done

shell习题-判断用户登录


1 编写一个名为ifuser的程序,它执行时带用户名作为命令行参数,判断该用户是否已经在系统中登录,并给出相关信息。

2 编写一个名为menu的程序,实现简单的弹出式菜单功能,用户能根据显示的菜单项从键盘选择执行对应的命令。

 

参考答案:

1.
#!/bin/bash
read -p "Please input the username: " user
if who | grep -qw $user
then
    echo $user is online.
else
    echo $user not online.
fi

2. 
#!/bin/bash
function message()
{
    echo "0. w"
    echo "1. ls"
    echo "2.quit"
    read -p "Please input parameter: " Par
}
message
while [ $Par -ne '2' ] ; do
    case $Par in
    0)
        w
        ;;
    1)
        ls
        ;;
    2)
        exit
        ;;
    *)
        echo "Unkown command"
        ;;
  esac
  message
done

shell习题-判断文件存在


1 编写一个名为iffile程序,它执行时判断/bin目录下date文件是否存在?

2 编写一个名为greet的问候程序,它执行时能根据系统当前的时间向用户输出问候信息。设从半夜到中午为早晨,中午到下午六点为下午,下午六点到半夜为晚上。

 

参考答案:

1

#!/bin/bash
if [ -f /bin/date  ] 
then
    echo "/bin/date file exist."
else
    echo "/bin/date not exist."
fi

2 

#!/bin/bash
h=`date +%H`
if [ $h -ge 0 ] && [ $h -lt 12 ]
then
    echo "Good morning."
elif [ $h -ge 12 ] && [ $h -lt 18 ]
then
    echo "Good afternoon."
else
    echo "Good evening."
fi

shell习题-格式化输出


输入一串随机数字,然后按千分位输出。

比如输入数字串为“123456789”,输出为123,456,789

 

参考答案:

#!/bin/bash

read -p "输入一串数字:" num
v=`echo $num|sed 's/[0-9]//g'`
if [ -n "$v" ]
then
    echo "请输入纯数字."
    exit
fi
length=${#num}
len=0
sum=''
for i in $(seq 1 $length)
do
        len=$[$len+1]
        if [[ $len == 3 ]]
        then
                sum=','${num:$[0-$i]:1}$sum
                len=0
        else
                sum=${num:$[0-$i]:1}$sum
        fi
done

if [[ -n $(echo $sum | grep '^,' ) ]]
then
        echo ${sum:1}
else
        echo $sum
fi

上面这个答案比较复杂,下面再来一个sed的

#!/bin/bash
read -p "输入一串数字:" num
v=`echo $num|sed 's/[0-9]//g'`
if [ -n "$v" ]
then
    echo "请输入纯数字."
    exit
fi

echo $num|sed -r '{:number;s/([0-9]+)([0-9]{3})/\1,\2/;t number}'

shell习题-检查错误


写一个shell脚本,检查指定的shell脚本是否有语法错误,若有错误,首先显示错误信息,然后提示用户输入q或者Q退出脚本,输入其他内容则直接用vim打开该shell脚本。

提醒: 检查shell脚本有没有语法错误的命令是  sh -n   xxx.sh

 

参考答案:

#!/bin/bash
sh -n $1 2>/tmp/err
if [ $? -eq "0" ]
then
    echo "The script is OK."
else
    cat /tmp/err
    read -p "Please inpupt Q/q to exit, or others to edit it by vim. " n
    if [ -z $n ]
    then
        vim $1
        exit
    fi
    if [ $n == "q" -o $n == "Q" ]
    then
        exit
    else
        vim $1
        exit
    fi

fi