Shell考题初级篇

将当前目录下大于10K的文件转移到/tmp目录下

find . -type f -size +10k -exec mv {} /tmp \;

 

编写一个shell,判断用户输入的文件是否是一个字符设备文件。如果是,请将其拷贝至/dev目录下

#!/bin/bash
read -t 30 -p 'Please output the file you specified:' str1
# 读取用户输入内容

if [ -n ${str1} ] && [ -e ${str1} ];
# 判断文件的真伪
  then
    str2=$(ls -l ${str1})
    str3=${str2:0:1}
    if [ $str3 == "c" ];
    # 判断文件是否是块设备
      then
    mv $str1 /dev/
    fi
  else
    echo "Input is wrong."
fi

 

请解释该脚本中注释行的默认含义与基础含义

#!/bin/sh
# chkconfig: 2345 20 80
# /etc/rc.d/rc.httpd
# Start/stop/restart the Apache web server.
# To make Apache start automatically at boot, make this
# file executable: chmod 755 /etc/rc.d/rc.httpd
case "$1" in
'start')
/usr/sbin/apachectl start ;;
'stop')
/usr/sbin/apachectl stop ;;
'restart')
/usr/sbin/apachectl restart ;;
*)
echo "usage $0 start|stop|restart" ;;
esac
请解释该脚本中注释行的默认含义与基础含义
第一行:指定脚本文件的解释器
第二行:指定脚本文件在chkconfig程序中的运行级别,2345代表具体用户模式启动(可用'-'代替),20表示启动的优先级,80代表停止的优先级。优先级数字越小表示越先被执行
第三行:告诉使用者脚本文件应存放路劲
第四行:告诉用户启动方式以及启动的用途
第五行:对于脚本服务的简单描述
第六行:文件的扩展可执行操作

 

写一个简单的shell添加10个用户,用户名以user开头

#!/bin/bash
for i in `seq 1 10`;
  do
    useradd user${i}
  done

 

写一个简单的shell删除10个用户,用户名以user开头

#!/bin/bash
for i in `seq 1 10`;
  do
    userdel -r user${i}
  done

 

写一个shell,在备份并压缩/etc目录的所有内容,存放在/tmp/目录里,且文件名如下形式yymmdd_etc.tar.gz

#!/bin/bash
NAME=$(date +%y%m%d)_etc.tar.gz
tar -zcf /tmp/${NAME} /etc

 

批量创建10个系统帐号oldboy01-oldboy10并设置密码(密码为随机8位字符串)

#!/bin/bash
for i in `seq 1 10`;
  do
    useradd oldboy${i}
    echo $RANDOM | md5sum | cut -c 1-8 | passwd --stdin oldboy${i}
  done

  

写一个脚本,实现判断192.168.10.0/24网络里,当前在线用户的IP有哪些(方法有很多)

#!/bin/bash
red="\e[31m"
shutdown="\e[0m"
green="\e[32m"
for((i=2;i<=254;i++))
do
  ping -c 1 -W1 -w 0.1 192.168.10.${i} &> /dev/null 
  if [ $? -eq 0 ]
   then
    echo -e "${green}"192.168.10.${i}${shutdown}" is running."
   else
    echo -e "${red}"192.168.10.${i}${shutdown}" is stop."
  fi
done
View Code

 

取出/etc/passwd文件中shell出现的次数

注:shell是指后面的/bin/bash,/sbin/nologin等

 

awk -F: '{print $7}' /etc/passwd | sort | uniq -c

 

 

文档合并,并输出指定样式内容

100 Jason Smith   
200 John Doe   
300 Sanjay Gupta   
400 Ashok Sharma
employee.txt
100 $5,000   
200 $500   
300 $3,000   
400 $1,250
bonus.txt
400 ashok sharma $1,250  
100 jason smith  $5,000  
200 john doe  $500  
300 sanjay gupta  $3,000
处理结果
paste employee.txt bonus.txt | awk '{print $1,$2,$3,$5}' | tr '[A-Z]' '[a-z]' | sort -k 2
答案

 

请按照这样的日期格式(xxxx-xx-xx)每日生成一个文件,例如今天生成的文件为2017-07-05.log, 并且把磁盘的使用情况写到到这个文件中

df -h > $(date '+%Y-%m-%d').log

 

posted @ 2019-09-18 10:40  寻真丶  阅读(627)  评论(0)    收藏  举报