第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
下一篇 2017-01-04

相關推薦

  • 第三周練習

    列出當前系統上所有已經登錄的用戶的用戶名,注意:同一用戶登錄多次,只顯示一次。                     ~]# who | cut -d" " …

    Linux干貨 2016-11-09
  • shell變量的淺談

    變量本質上是存儲數據的一個或多個計算機內存地址,變量的命令規則包括: 1) 不能使用程序中的保留字,如if, for 2) 變量由字母、下劃線和數字組成,且不能以數字開頭 3) 要求風名知義 4) 統一命名規則:駝峰命名法 變量主要分為本地變量、環境變量、局部變量、位置變量和特殊變量 (1)本地變量:只對當前shell…

    2017-08-05
  • 非對稱密鑰加密解密

    兩臺計算機一臺用公鑰加密
    另一臺解密

    2018-01-08
  • LVS的四種模型

    相關術語: vs:Virtual Server,Director,Dispatcher,Balancer rs:Real Server,upstream server,backend server lvs集群的類型: lvs-nat:修改請求報文的目標IP lvs-dr:操作封裝新的MAC地址; lvs-tun:在原請求IP報文之外新加一個IP首部; lvs…

    Linux干貨 2016-10-30
  • 優云實踐:巧用Salt,實現CMDB配置自動發現

    隨著互聯網+新形勢的發展,越來越多的企業步入雙態(穩敏雙態)IT時代,信息化環境越來越復雜,既有IOE三層架構,也有VCE、Openstack等云虛擬化架構和互聯網化的分布式大數據架構。所以,企業急需建立一套合適的配置管理庫(CMDB),像人類“大腦”一樣統一存儲從基礎架構到業務應用各層面的配置信息,以便協調“身體”(運維系統)各部分完成復雜的運維工作。 C…

    系統運維 2016-07-26
  • linux基礎知識:文件管理,bash特性

    本文簡要介紹了文件類的管理命令,包括mv、cp、mkdir等等。還介紹了一些萬用字符的用法。

    2017-12-12

評論列表(1條)

  • luoweiro
    luoweiro 2017-02-23 07:54

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

欧美性久久久久