99热99这里只有精品6国产,亚洲中文字幕在线天天更新,在线观看亚洲精品国产福利片 ,久久久久综合网

歡迎加入QQ討論群258996829
麥子學(xué)院 頭像
蘋果6袋
6
麥子學(xué)院

Linux SHELL中的循環(huán)詳解

發(fā)布時間:2016-12-15 17:23  回復(fù):0  查看:2335   最后回復(fù):2016-12-15 17:23  

本文和大家分享的主要是linux中的SHELL循環(huán)相關(guān)用法,希望通過本文的分享,對大家學(xué)習(xí)linux 有所幫助,一起來看看吧。

  1、寫一個腳本,判斷當(dāng)前系統(tǒng)用戶shell是否都為可登陸shell(即非/sbin/nologin),分別計算兩類用戶的個數(shù)(通過比較字符串實現(xiàn))

  #!/bin/bash# check the user could login by default or not,and how many it is # 20161211declare loginideclare nologinjfor ((i=1;i<=$(cat /etc/passwd|wc -l);i++))do

  defaultshell=$(head -$i /etc/passwd|sed '$!d'|cut -d':' -f7)

  if [ $defaultshell == /sbin/nologin ];then

  logini=$(($logini+1))

  else

  nologinj=$(($nologinj+1))

  fidone

  echo "the default login_user has $logini,the others has $nologinj"

  或者

  #!/bin/bash##

  declare nologinideclare othersj

  for ((i=1;i<=$(cat /etc/passwd|wc -l);i++))do

  if [ $(head -$i /etc/passwd|sed '$!d'|sed 's/^.*://') == /sbin/nologin ]

  then

  nologini=$(($nologini+1))else

  othersj=$(($othersj+1))fi

  doneecho "the default login users is $nologini,the others is $othersj"

  2、寫一個腳本:1)獲取當(dāng)前主機(jī)的主機(jī)名,保存在變量hostname中;2)判斷此變量的值是否是localhost,如果是,將當(dāng)前主機(jī)名更改為www.mageedu.com3)如果不是,則顯示主機(jī)名

  #!/bin/bash# check the $HOSTNAME if it's localhost,then change it to www.mageedu.com ;else display it# 20161212

  hostname=$(hostname)if [ $hostname == localhost ] ||[ $hostname == localhost.localdomain ]then

  hostname "www.mageedu.com"

  echo -e 'I change HOSTNAME is $hostname\n'else

  echo -e $hostnamefi

  3、寫一個腳本實現(xiàn)如下功能:1)傳遞一個磁盤設(shè)備文件路徑給腳本,判斷此設(shè)備是否存在;2)如果存在,則顯示此設(shè)備上所有分區(qū)信息

  #!/bin/bash# please input a device ,check if it's a block device ,so display the partition#

  read -p "Please input a block device,like:sdasdb" device

  if [ -b /dev/$device ]then

  echo "the $device is a block device:/dev/$device;and its partition like:"

  lsblk /dev/$deviceelse

  echo "$device is not exist!"fi

  4、寫一個腳本實現(xiàn)以下功能:腳本能接受一個參數(shù)1)如果參數(shù)1quit,則顯示退出腳本,并執(zhí)行正常退出;2)如果參數(shù)1yes,則顯示繼續(xù)執(zhí)行腳本;3)否則,參數(shù)1為其他任意值,均執(zhí)行非正常退出

  #!/bin/bash# while you input 'quit',the shell exit 0;input 'yes' the shell continue;input other command the shell exit 1#

  read -p "please input yes,quit,*" l

  case $l in

  yes)

  echo "the shell end with no error"

  exit 0

  ;;

  quit)

  echo "the shell continue"

  continue

  ;;

  *)

  echo "the shell out within error"

  exit 1

  ;;

  esac

  5、寫一個腳本,實現(xiàn)以下功能,傳遞一個參數(shù)給腳本,此參數(shù)為gzipbzip2或者xz三者之一:1)如果參數(shù)1的值為gzip,則使用targzip歸檔壓縮/etc目錄至/backups目錄中,并命名為/backups/etc-20160613.tar.gz;2)如果參數(shù)1的值為bzip2,則使用tarbzip2歸檔壓縮/etc目錄至/backups目錄中,并命名為/backups/etc-20160613.tar.bz2;3)如果參數(shù)1xz,則使用tarxz歸檔壓縮/etc目錄至/backups目錄中,并命名為/backups/etc-20160613.tar.xz;4)如果是其他值,則顯示錯誤的壓縮工具,并執(zhí)行非正常退出

  #!/bin/bash# tar /etc/* with bzip2,gzip,xz#

  if [ $# == 0 ]

  then

  echo " you must choose a option(like bzip2,gzip,xz)!"

  else

  case $1 in

  gzip)

  tar -czvf /backups/etc-20161212.tar.gz /etc/*

  ;;

  bzip2)

  tar -P -cjvf /backups/etc-20161212.tar.bz2 /etc/*

  ;;

  xz)

  tar -Jcvf /backups/etc-20161212.tar.xz /etc/*

  ;;

  esac

  ls -al /backupsfi

  6、寫一個腳本接受一個路徑參數(shù):1)如果是普通文件,則說明其可被正常訪問;2)如果是目錄文件,則說明可對其使用cd命令;3)如果是符號鏈接文件,則說明是個訪問路徑;4)其他為無法判斷

  #!/bin/bash# check the file or directory 's type# 20161213

  if [ $# -eq 0 ];then

  echo "you must insert a file or directory!"elif [ -f $1 ];then

  echo "$1 is a file and you can read/write it"elif [ -d $1 ];then

  echo "$1 is a directory,you can access it with COMMAND 'cd'"elif [ -l $1 ];then

  echo "$1 is a link file"else

  echo "i can't fond what type it is"fi

  7、寫一個腳本,取得當(dāng)前主機(jī)的主機(jī)名,判斷1)如果主機(jī)名為空或者為localhost,或者為none,則將其命名為mailmagedu.com;2)負(fù)責(zé),顯示現(xiàn)有的主機(jī)名即可

  同題2

  8、寫一個腳本,接受一個用戶名為參數(shù):1)如果用戶的id號為0,則顯示為管理員;2)如果用戶的id號大于0且小于500,則顯示其為系統(tǒng)用戶;3)否則,顯示其為普通用戶

  #!/bin/bash# check the user's type,it's operater or ordinery user# 20161213

  if [ $# -eq 0 ];then

  echo "Please input a username!just one"elif `id -u $1>>/dev/null 2>&1`;[ $? != 0 ];then

  echo "the user $1 isn't fond" && exit 1

  elif [ `id -u $1` -eq 0 ];then

  echo "$1 is root"

  elif [ `id -u $1` -lt 500 ];then

  echo "$1 is a operator"

  else

  echo "$1 is a user"

  fi

  9、寫一個腳本,傳遞一個用戶名參數(shù)給腳本;如果用戶的id號大于等于500,且其默認(rèn)shell為以sh結(jié)尾的字符串,則顯示“a user can log system"類的字符串

  #!/bin/bash# check user's ID greater than 500 and the last two character of the default SHELL is sh# 20161213

  if [ $# -eq 0 ];then

  echo 'Please insert a username!'elif `id -u $1 >>/dev/null 2>&1`;[ $? != 0 ];then

  echo "The user isn't fond" elif [ $(id -u $1) -ge 500 ] && [ $(grep $1 /etc/passwd|sed 's/.*://'|grep -o "sh$") == sh ];then

  echo "The user $1 can login system"else

  echo "The user'ID is $(id -u $1),the user cannt login"fi

 

來源:運(yùn)維部落

您還未登錄,請先登錄

熱門帖子

最新帖子

?