第10周作業(下)

4、寫一個腳本
(1) 能接受四個參數:start, stop, restart, status
start: 輸出“starting 腳本名 finished.”
(2) 其它任意參數,均報錯退出。

#!/bin/bash
#
[ $# -ne 1 ] && echo "You should give one parameter." && exit 1

case  $1 in
start)
    echo "Starting $0 finished."
    ;;
stop)
    echo "Stopping $0 finished."
    ;;
restart)
    echo "Restarting $0 finished."
    ;;
status)
    echo "Status of $0 is normal."
    ;;
*)
    echo "You gave wrong parameter.Now,exiting..."
    exit 2
    ;;
esac

5、寫一個腳本,判斷給定的用戶是否登錄了當前系統。

  (1) 如果登錄了,則顯示用戶登錄,腳本終止。

  (2) 每3秒鐘,查看一次用戶是否登錄。

#!/bin/bash
#
[ $# -ne 1 ] && echo "You should give one parameter." && exit 1

while true;do
    if who | grep -o "^\($1\)";then
        echo "$1 is already logged on."
        exit 0
    fi    
    sleep 3
done

6、寫一個腳本,顯示用戶選定要查看的信息。
cpu) display cpu info
mem) display memory info
disk) display disk info
quit) quit
非此四項選擇,則提示錯誤,并要求用戶重新選擇,只到其給出正確的選擇為止。

#!/bin/bash
#
cat << EOF
cpu) Display CPU information.
mem) Display memory information.
disk) Display disk information.
quit) Quit.
=======================================
EOF

read -p "Please enter your choice:" option

while [ "$option" != "cpu" -a "$option" != "mem" -a "$option" != "disk" -a "$option" != "quit" ];do
    echo "You should enter cpu,mem,disk,or quit."
    read -p "Enter your choice again:" option
done

case $option in
cpu)
    lscpu
    ;;
mem)
    free -m
    ;;
disk)
    fdisk -l /dev/sd[a-z]
    df -h
    ;;
quit)
    echo "Exiting..."
    exit 0
    ;;
esac

7、寫一個腳本
(1) 用函數實現返回一個用戶的UID和SHELL;用戶名通過參數傳遞而來。
(2) 提示用戶輸入一個用戶名或輸入“quit”退出。
當輸入的是用戶名,則調用函數顯示用戶信息。
當用戶輸入quit,則退出腳本;進一步地:顯示鍵入的用戶相關信息后,再次提醒輸出用戶名或quit。

#!/bin/bash
#
cat <<EOF
You  give one username,then you'll get some information,e.g. user id and user shell.
=============================================================================
EOF

user_info() {
    while true;do
	    read -p "Please enter a username:" username   
	    if id -u $username &>/dev/null;then
	        echo "$username's id is $(id -u $username)"
	        echo "$username's shell is `grep "^\($username\)\>" /etc/passwd | cut -d: -f7`."
	    else
	        if [ $username == "quit" ];then
		        echo "Existing..."
		        exit 0
	        else
		        echo "Please enter a correct username."
		        continue
	        fi
	    fi
    done
}

user_info

8、寫一個腳本,完成如下功能(使用函數)
(1) 提示用戶輸入一個可執行命令的名字;獲取此命令依賴的所有庫文件。
(2) 復制命令文件至/mnt/sysroot目錄下的對應的rootfs的路徑上,例如,如果復制的文件原路徑是/usr/bin/useradd,則復制到/mnt/sysroot/usr/bin/目錄中。
(3) 復制此命令依賴的各庫文件至/mnt/sysroot目錄下的對應的rootfs的路徑上;規則同上面命令相關的要求。

#!/bin/bash
#
cat <<EOF
Enter your command ,then the program will copy the command file and lib files to /mnt/sysroot/lib64/ .
==================================================================================================
EOF

copycmd() {
    while true;do
        read -p "Enter yours command:" command
	    if which $command &>/dev/null;then
            sour=$(whereis -b $command | grep -o "/.*")
		    dest="/mnt/sysroot"$sour
		    cp $sour $dest
		        if [ $? -eq 0 ];then
			        echo "A copy of $command is finished."
		        else
			        echo "A copy of $command is failed."
			        continue
                fi
            sour1=$(ldd $sour | grep -o "/lib64/.*[[:space:]]")
	        dest2="/mnt/sysroot/lib64"
		    cp $sour1 $dest2
		        if [ $? -eq 0 ];then
		            echo "A copy of $command's lib files is finished." 
    		    else
			        echo "A copy of $command's lib files is failed."
			        continue
		        fi
	    else
		    if [ $command == "quit" ];then
		        echo "Existing..."
		        exit 0
		    else 
		        echo "Please enter a correct command."
		        continue
		    fi
	    fi
done
}
		    
copycmd

原創文章,作者:N24_lantian,如若轉載,請注明出處:http://www.www58058.com/65735

(0)
N24_lantianN24_lantian
上一篇 2017-01-03 22:19
下一篇 2017-01-04 00:05

相關推薦

  • Vim 末行模式 & crontab & scripts 練習

    1、復制/etc/rc.d/rc.sysinit文件至/tmp目錄,將/tmp/rc.sysinit文件中的以至少一個空白字符開頭的行的行首加#;   ~]# cp /etc/rc.d/rc.sysinit /tmp     %s@^[[:space:]]\+\*@#&@g     (使用元字符 有幾…

    Linux干貨 2016-10-31
  • 關于大型網站技術演進的思考(二):存儲的瓶頸(2)

    原文出處: 夏天的森林   上篇里我講到某些網站在高并發下會報出503錯誤,503錯誤的含義是指網站服務端暫時無法提供服務的含義,503還表達了網站服務端現在有問題但是以后可能會提供正常的服務,對http協議熟悉的人都知道,5開頭的響應碼表達了服務端出現了問題,在我們開發測試時候最為常見的是500錯誤,500代表的含義是服務端程…

    2015-03-11
  • CentOS7系統用戶空間管理進程systemd詳解

    概述:     系統啟動過程中,當內核啟動完成,后加載根文件系統,后就緒的一些用戶空間的服務的管理工作,就交由init進行啟動和管理,在CentOS6之前的init的管理方式都類似,相關的內容我們在之前的文章中也做出過介紹。在CentOS7上,init變成了systemd,其管理方式也發生了重大的變化,本章就跟大家歐一…

    Linux干貨 2016-09-21
  • Linux 第八天: 練習和作業

    Linux 第八天: (08月09日) 練習和作業       1刪除/etc/grub2.cfg所有以空白開頭的行行首空白字符 sed 's#^[[:space:]]\+##' /etc/grub2.cfgsed -r 's@^[[:space:]]+@@' /etc/grub2.cfg…

    Linux干貨 2016-08-11
  • nginx負載均衡實驗

    實驗一、 實驗環境:     1、一臺director主機。并部署nginx服務。        內網iP:192.168.1.1  外網iP:10.1.64.1     2、一臺后端服務器。并部署h…

    Linux干貨 2016-10-30
  • 如何修復Ubuntu 12.04上時間不正確的問題

    大家好: 今天跟大家分享下如何修復ubuntu 12.04上時間不正常的問題。 1–首先打date查看系統的時間是否正常 martell@cnux10:~/sistes/sha-mmb-o2o2o$ date  Thu Dec  1 09:15:47 UTC 2016 martell@cnux10:~/sistes/sha-m…

    Linux干貨 2016-12-04

評論列表(1條)

  • luoweiro
    luoweiro 2017-02-23 07:54

    腳本寫的很不錯,對于腳本傳參有了深刻的總結。

欧美性久久久久