Shell考題初級篇

將當前目錄下大於10K的文件轉移到/tmp目錄下web

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

 

編寫一個shell,判斷用戶輸入的文件是不是一個字符設備文件。若是是,請將其拷貝至/dev目錄下shell

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

 

請解釋該腳本中註釋行的默認含義與基礎含義apache

#!/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開頭bash

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

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

 

批量建立10個系統賬號oldboy01-oldboy10並設置密碼(密碼爲隨機8位字符串)this

#!/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有哪些(方法有不少)spa

#!/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出現的次數rest

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

 

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
相關文章
相關標籤/搜索