Linux高级运维工程师面试题收集--带答案(更新)

Linux安全运维 (7.1万) 2016-12-22 15:09:47

25、写一个脚本,实现批量添加20个用户,用户名为user01-20,密码为user后面跟5个随机字符

#!/bin/bash
#description: useradd
for i in `seq -f"%02g" 1 20`;do
useradd user$i
echo "user$i-`echo $RANDOM|md5sum|cut -c 1-5`"|passwd –stdinuser$i >/dev/null 2>&1
done

26、写一个脚本,实现判断192.168.1.0/24网络里,当前在线的IP有哪些,能ping通则认为在线

#!/bin/bash
for ip in `seq 1 255`
  do
    {
     ping -c 1 192.168.1.$ip > /dev/null 2>&1
     if [ $? -eq 0 ]; then
          echo 192.168.1.$ip UP
     else
          echo 192.168.1.$ip DOWN
     fi
   }&
done
wait

26、写一个脚本,判断一个指定的脚本是否是语法错误;如果有错误,则提醒用户键入Q或者q无视错误并退出其它任何键可以通过vim打开这个指定的脚本

[root@localhost  tmp]# cat checksh.sh
 #!/bin/bash
 read -p "please input check script-> " file
 if [ -f $file ]; then
    sh -n $file > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        read -p "You input $file syntax error,[Type q to exit or Type vim to  edit]" answer
        case $answer in
        q | Q)
           exit 0
           ;;
        vim )
           vim $file
           ;;
        *)
         exit 0
         ;;
        esac
   fi
 else
    echo "$file not exist"
    exit 1
 fi

27、写一个脚本:(27包括3个小题)

1、创建一个函数,能接受两个参数:

1)第一个参数为URL,即可下载的文件;第二个参数为目录,即下载后保存的位置;

2)如果用户给的目录不存在,则提示用户是否创建;如果创建就继续执行,否则,函数返回一个51的错误值给调用脚本;

3)如果给的目录存在,则下载文件;下载命令执行结束后测试文件下载成功与否;如果成功,则返回0给调用脚本,否则,返回52给调用脚本;

[root@localhost tmp]# cat downfile.sh
#!/bin/bash
url=$1
dir=$2
download()
  {
    cd $dir >> /dev/null 2>&1
    if [ $? -ne 0 ];then
        read -p "$dir No such file or directory,create?(y/n)" answer
        if [ "$answer" == "y" ];then
            mkdir -p $dir
            cd $dir
            wget $url 1> /dev/null 2>&1
        else
            return "51"
        fi
    fi
    if [ $? -ne 0 ]; then
        return "52"
    fi
}
download $url $dir
echo $?

THE END

Leave a Reply