Linux基礎之shell腳本編程(四)

1、寫一個腳本

  (1) 能接受四個參數:start, stop, restart, status

   start: 輸出“starting 腳本名 finished.”

  (2) 其它任意參數,均報錯退出;

  1 #!/bin/bash
  2 #author:BaoZhang
  3 #
  4 flag=0
  5 if [ $# -eq 1 ];then
  6   case $1 in
  7     start)
  8       echo "start $0 finished"
  9       filg=1
 10       ;;
 11     stop)
 12       echo "stop $0 finished"
 13       flag=0
 14       ;;
 15     restart)
 16       echo "restart $0 finished"
 17       flag=1
 18       ;;
 19     status)
 20       if [ $flag -eq 1 ];then
 21         echo "$0 is running..."
 22       else
 23         echo "$0 stopped...."
 24       fi
 25       ;;
 26     *)
 27       echo "invalid argument,usage:$0 {start|stop|status|restart}"
 28       ;;
 29   esac
 30 else
 31   echo "usage : $0 {start|stop|status|restart}"
 32 fi

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

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

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

  1 #!/bin/bash
  2 #author:BaoZhang
  3 #
  4 read -p "please input username:" username
  5 until who | grep "\b$username\b" &>/dev/null ;do
  6   echo "$username not login "
  7   sleep 3
  8 done
  9 echo "$username login!!"
 10 exit 0
 11

3、寫一個腳本,顯示用戶選定要查看的信息;

   cpu) display cpu info

   mem) display memory info

   disk) display disk info

   quit) quit

   非此四項選擇,則提示錯誤,并要求用戶重新選擇,只到其給出正確的選擇為止;

  1 #!/bin/bash
  2 #author:BaoZhang
  3 #
  4 echo  "please input what you want to do:"
  5 echo "cpu: display cpu info"
  6 echo "mem: display mem info"
  7 echo "disk: diplay disk info"
  8 echo "quit: quit this script"
  9 read  input
 10 until [ $input == "cpu" -o $input == "mem" -o $input == "disk" -o $input == "quit" ] ; do
 11   read -p "error input,please input what you want to do:" input
 12 done
 13 case $input in
 14   cpu)
 15     lscpu
 16     ;;
 17   mem)
 18     cat /proc/meminfo
 19     ;;
 20   disk)
 21     fdisk -l
 22     ;;
 23   quit)
 24     echo "quit..."
 25     exit 0
 26     ;;
 27 esac
 28

4、寫一個腳本

  (1) 用函數實現返回一個用戶的UID和SHELL;用戶名通過參數傳遞而來;

  (2) 提示用戶輸入一個用戶名或輸入“quit”退出;

    當輸入的是用戶名,則調用函數顯示用戶信息;

    當用戶輸入quit,則退出腳本;進一步地:顯示鍵入的用戶相關信息后,再次提醒輸出用戶名或quit: 

  1 #!/bin/bash
  2 #author:BaoZhang
  3 #
  4 function findUser
  5 {
  6   if id $1 &>/dev/null;then
  7     userid=$(grep "^\b$1\b" /etc/passwd | cut -d":" -f3)
  8     usershell=$(grep "^\b$1\b" /etc/passwd | cut -d":" -f7)
  9     echo "user $1: uid=$userid, shell=$usershell "
 10   else
 11     echo "the user not exist"
 12   fi
 13 }
 14 
 15 echo "input a user name for the user info,input quit for quit:" 
 16 read input
 17 until [ $input == "quit" ];do
 18   findUser $input
 19   echo "please input again or quit" 
 20   read input
 21 done
 22

5、寫一個腳本,完成如下功能(使用函數)

   (1) 提示用戶輸入一個可執行命令的名字;獲取此命令依賴的所有庫文件;

   (2) 復制命令文件至/mnt/sysroot目錄下的對應的rootfs的路徑上,例如,如果復制的文件原路徑是/usr/bin/useradd,則復制到/mnt/sysroot/usr/bin/目錄中;

   (3) 復制此命令依賴的各庫文件至/mnt/sysroot目錄下的對應的rootfs的路徑上;規則同上面命令相關的要求;

  1 #!/bin/bash
  2 #author:BaoZhang
  3 #
  4 command_target_dir=/mnt/sysroot/
  5 lib_target_dir=/mnt/sysroot/rootfs/
  6 function copy
  7 {
  8   if $1 &>/dev/null;then
  9     command=$(which $1 | tail -1)
 10     cp $command $command_target_dir
 11     echo "$1 copy finished"
 12     for i in { $(ldd $command | cut -d" " -f3) };do
 13       cp $i $lib_target_dir &>/dev/null
 14     done
 15     echo "$1 lib file copy finished"
 16   else
 17     echo "error input .."
 18   fi
 19 }
 20 
 21 echo "input a command or quit for quit:"
 22 read input
 23 until [ $input == "quit" ];do
 24    if [ cd $command_target_dir &>/dev/null -a cd $lib_target_dir &>/dev/null ] ;then
 25      copy $input
 26    else
 27      mkdir -p $command_target_dir &>/dev/null
 28      mkdir -p $lib_target_dir &>/dev/null
 29      copy $input
 30    fi
 31   echo "you can input a command again or quit for quit:"
 32   read input
 33 done

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

(0)
zhangbaozhangbao
上一篇 2016-11-27 23:04
下一篇 2016-11-28 01:00

相關推薦

  • Linux系統管理常用命令

    系統管理工具 進程的分類: CPU-Bound:CPU密集型,非交互。特別消耗CPU的,加密解密,壓縮解壓 IO-Bound:IO密集型,交互。大量的硬盤讀寫,例如復制文件 Linux系統狀態的查看及管理工具:pstree, ps, pidof, pgrep, top, htop, glance, pmap, vmstat, dstat, kill, pki…

    Linux干貨 2017-12-18
  • CentOS6+ LAMP+ wordpress 搭建個人博客站

        每個IT工程師都期待擁有一個自己的博客站,本文講述在CentOS 6系統LAMP環境下使用WordPress框架構建個人博客站的詳細過程。     —-構建LAMP環境—- 1,安裝apache,mysql php yum install -y httpd mysql…

    2017-07-11
  • CentOS 6.5下編譯安裝httpd+mysql+php過程實錄

    一、安裝環境     Linux系統:CentOS 6.5     Apache版本:http-2.4.12     MySQL版本:MySQL 5.6.24     PHP版本:PHP-5.6.8 …

    Linux干貨 2015-05-18
  • CentOS7下重置root密碼

    CentOS7下重置root密碼          Linux系統、UNIX系統和其他類UNIX系統中,存在唯一的超級用戶root。普通用戶密碼忘掉可以用root用戶重置,但是一旦root密碼忘掉,事情就復雜起來了。本文主要介紹root密碼忘掉之后,重置密碼的過程。   &nbs…

    Linux干貨 2017-03-30
  • VIM入門及進階

    什么是VIM?     VIM類似于Vi編輯器, 它是一個功能強大、可高度定制的文本編輯器, 是一個純粹的自由軟件。注意:vi和vim不完全相同 為什么要使用VIM?     相信大多數人接觸Linux時使用的第一個文本編輯器都不是VIM,很多人看到VIM復雜的命令操作就望而祛步…

    系統運維 2016-03-04
  • http

    練習:分別使用CentOS 7和CentOS 6實現以下任務 一、配置三個基于名稱的虛擬主機          (a) discuzX          (b) wordpress  &nb…

    Linux干貨 2016-10-09
欧美性久久久久