馬哥教育網絡班21期+第9周課程練習

1、寫一個腳本,判斷當前系統上所有用戶的shell是否為可登錄shell(即用戶的shell不是/sbin/nologin);分別這兩類用戶的個數;通過字符串比較來實現;

[root@localhost test]# ./exercise3.sh 
be eable to login users sum:5
can't login users sum:29
[root@localhost test]# cat exercise3.sh 
#!/bin/bash
declare -i logincount=0
declare -i nologincount=0
for i in $(cut -d: -f7 /etc/passwd);do
if [ "$i" == "/sbin/nologin" ];then
let nologincount++
continue
else
let logincount++
fi
done
echo "be eable to login users sum:$logincount"
echo "can't login users sum:$nologincount"

2、寫一個腳本

    (1) 獲取當前主機的主機名,保存于hostname變量中;

    (2) 判斷此變量的值是否為localhost,如果是,則將當前主機名修改為www.magedu.com;

    (3) 否則,則顯示當前主機名;

[root@localhost test]# hostname
localhost.localhostdomain
[root@localhost test]# ./exercise4.sh 
www.magedu.com
[root@localhost test]# hostname
www.magedu.com
[root@localhost test]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=www.magedu.com
[root@localhost test]# cat exercise4.sh 
#!/bin/bash
hostname=$HOSTNAME
if [ "${hostname%.*}" == "localhost" ];then
sed -i  '/^HOSTNAME=/d' /etc/sysconfig/network && 
echo "HOSTNAME=www.magedu.com" >> /etc/sysconfig/network && hostname www.magedu.com && hostname
else
echo "$HOSTNAME"
fi

3、寫一個腳本,完成如下功能

    (1) 傳遞一個磁盤設備文件路徑給腳本,判斷此設備是否存在;

    (2) 如果存在,則顯示此設備上的所有分區信息;

[root@localhost test]# ./exercise5.sh /dev/
Please input a illegal device path!
[root@localhost test]# ./exercise5.sh /etc/
Please input a illegal device path!
[root@localhost test]# ./exercise5.sh /dev/sda1
/dev/sda1
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       477M   29M  424M   7% /boot
[root@localhost test]# ./exercise5.sh /dev/sda
/dev/sda
Disk /dev/sda: 16.1 GB, 16106127360 bytes
255 heads, 63 sectors/track, 1958 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000a8052
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        1959    15215616   8e  Linux LVM
[root@localhost test]# cat exercise5.sh 
#!/bin/bash
[ $# -eq 0 ] && echo "must input a device path!" && exit 12
[ $# -gt 1 ] && echo "too many args,only for one!" && exit 13
[ ! -e $1  ] && echo "not a decive path or doesn't exisits!" && exit 14
if fdisk -l $1 &>/dev/null && echo "$1" | grep "^/dev/[hs]d[a-z]$"; then
fdisk -l $1
elif fdisk -l $1 &>/dev/null && echo "$1" | grep "^/dev/[sh]d[a-z][[:digit:]]$"; then
df  -h $1
else
echo "Please input a illegal device path!"
fi

4、寫一個腳本,完成如下功能

   腳本能夠接受一個參數;

   (1) 如果參數1為quit,則顯示退出腳本,并執行正常退出;

   (2) 如果參數1為yes,則顯示繼續執行腳本;

   (3) 否則,參數1為其它任意值,均執行非正常退出;

[root@www test]# ./exercise6.sh 
quit) exit script
yes) continue script
=======================
Enter a option:quit
see you around!
[root@www test]# echo $?
0
[root@www test]# ./exercise6.sh 
quit) exit script
yes) continue script
=======================
Enter a option:yes
testing!
[root@www test]# ./exercise6.sh 
quit) exit script
yes) continue script
=======================
Enter a option:kg
force exit!
[root@www test]# echo $?
12
[root@www test]# cat exercise6.sh 
#!/bin/bash
cat << EOF
quit) exit script
yes) continue script
=======================
EOF
read  -p "Enter a option:" option
function test {
echo "testing!"
}
case $option in 
     "quit")
  echo "see you around!"
  exit 0
  ;;
      "yes")
   test
   ;;
*)
echo "force exit!"
exit 12
;;
esac

5、寫一個腳本,完成如下功能

   傳遞一個參數給腳本,此參數為gzip、bzip2或者xz三者之一;

   (1) 如果參數1的值為gzip,則使用tar和gzip歸檔壓縮/etc目錄至/backups目錄中,并命名為/backups/etc-20160613.tar.gz;

   (2) 如果參數1的值為bzip2,則使用tar和bzip2歸檔壓縮/etc目錄至/backups目錄中,并命名為/backups/etc-20160613.tar.bz2;

   (3) 如果參數1的值為xz,則使用tar和xz歸檔壓縮/etc目錄至/backups目錄中,并命名為/backups/etc-20160613.tar.xz;

   (4) 其它任意值,則顯示錯誤壓縮工具,并執行非正常退出;

[root@www test]# ls -l  /backups/etc-20160804
-rw-r--r--. 1 root root 6196128 Aug  4 21:26 /backups/etc-20160804
[root@www test]# cat exercise8.sh 
#!/bin/bash
cat << EOF
 gzip) To compress and archive by gzip and tar;
bzip2) To compress and archive by bzip2 and tar;
   xz) To compress and archive by xz and tar;
=======================
EOF
read  -p "Enter a option:" option
case $option in 
     "gzip")
  [ -d /backups/ ] || mkdir /backups/
  tar -zcf /backups/etc-$(date +%Y%m%d) /etc/
  echo "gzip compressed"
  exit 0
  ;;
      "bzip2")
  [ -d /backups/ ] || mkdir /backups/
  tar -jcf /backups/etc-$(date +%Y%m%d) /etc/
  echo "bzip2 compressed"
  exit 0 
   ;;
      "xz")
  [ -d /backups/ ] || mkdir /backups/
  tar -Jcf /backups/etc-$(date +%Y%m%d)  /etc/
  echo "xz compressed"
  exit 0 
    ;;
*)
echo "force exit!"
exit 12
;;
esac

6、寫一個腳本,接受一個路徑參數:

   (1) 如果為普通文件,則說明其可被正常訪問;

   (2) 如果是目錄文件,則說明可對其使用cd命令;

   (3) 如果為符號鏈接文件,則說明是個訪問路徑;

   (4) 其它為無法判斷;

[root@www test]# ./exercise9.sh /etc/issue
/etc/issue is a common file!
[root@www test]# ./exercise9.sh /etc/rc.d/rc3.d/S55sshd 
/etc/rc.d/rc3.d/S55sshd is a symbol link file use ls -l for more info!
[root@www test]# ./exercise9.sh /dev/sda
/dev/sda doesn't exisits or maybe other type file!
[root@www test]# cat exercise9.sh 
#!/bin/bash
[ $# -eq 0  -o $# -gt 1 ] && echo "only for one arg or too many args!" && exit 12
if [ -f $1 -a ! -h $1 ];then
echo "$1 is a common file!"
elif [ -d $1 ];then
echo "$1 is a directory,you can use command cd to access!"
elif [ -h $1 ];then
echo "$1 is a symbol link file use ls -l for more info!"
else
echo "$1 doesn't exisits or maybe other type file!"
fi

7、寫一個腳本,取得當前主機的主機名,判斷

   (1) 如果主機名為空或為localhost,或為"(none)",則將其命名為mail.magedu.com;

   (2) 否則,顯示現有的主機名即可;

[root@www test]# bash -x exercise10.sh
+ '[' -z www.magedu.com -o www.magedu.com == localhost -o www.magedu.com == none ']'
+ hostname
www.magedu.com
[root@www test]# cat exercise10.sh 
#!/bin/bash
if [ -z $HOSTNAME -o "$HOSTNAME" == "localhost" -o "$HOSTNAME" == "none" ];then
 hostname mail.magedu.com && sed -i '/^HOSTNAME/d' /etc/sysconfig/network &&
 echo "HOSTNAME=mail.magedu.com" >> /etc/sysconfig/network
else
hostname
fi

8、寫一腳本,接受一個用戶名為參數;

   (1) 如果用戶的id號為0,則顯示其為管理員;

   (2) 如果用戶的id號大于0且小于500, 則顯示其為系統用戶;

   (3) 否則,則顯示其為普通用戶;

[root@www test]# ./exercise11.sh hehe
id: hehe: No such user
[root@www test]# ./exercise11.sh root
root is super user
[root@www test]# ./exercise11.sh ntp
ntp is System user
[root@www test]# ./exercise11.sh derulo
derulo is Common user
[root@www test]# cat exercise11.sh 
#!/bin/bash
[ $# -eq 0 -o $# -gt 1 ]&& echo "only for one user account or too many user account!"&& exit 12
userid=$(id $1 | cut -d " " -f1 | tr "()" " " | cut -d "=" -f2 | cut -d " " -f1)
echo "$userid $1" >/tmp/id.txt
id $1 &>/dev/null && awk '{if($1==0){print $2 " is super user";}else if($1>0 && $1<500)
{print $2 " is System user";}else{print $2 " is Common user";}}' /tmp/id.txt

10、寫一個腳本,傳遞一個用戶名參數給腳本;

   (1) 如果用戶的id號大于等于500,且其默認shell為以sh結尾的字符串,則顯示“a user can log system.”類的字符串;

   (2) 否則,則顯示無法登錄系統;


[root@www test]# ./exercise12.sh root
you cannot login!
[root@www test]# ./exercise12.sh derulo
a user can login!
[root@www test]# ./exercise12.sh hehe
user doesn't exisits!
[root@www test]# cat exercise12.sh 
#!/bin/bash
[ $# -eq 0 -o $# -gt 1 ]&& echo "only for one user account or too many user account!"&& exit 12
if id $1 &>/dev/null;then
userid=$(id $1 | cut -d " " -f1 | tr "()" " " | cut -d "=" -f2 | cut -d " " -f1)
usershell=$(finger $1 | grep "Shell" | cut -d ":" -f3 | cut -d " " -f2)
userend=${usershell:0-2}
echo "$userid $1 $usershell $userend" >/tmp/idtmp.txt
else
echo "user doesn't exisits!"
exit 13
fi
awk '{if($1>=500 && $4=="sh"){print "a user can login!";}else{print "you cannot login!";}}' /tmp/idtmp.txt

11、寫一個腳本,完成如下任務 :

   (1) 按順序分別復制/var/log目錄下的每個直接文件或子目錄至/tmp/test1-testn目錄中;

   (2) 復制目錄時,才使用cp -r命令;

   (3) 復制文件時使用cp命令;

   (4) 復制鏈接文件時使用cp -d命令;

   (5) 余下的所有類型,使用cp -a命令;

[root@www test]# ./exercise13.sh 
/var/log/anaconda.ifcfg.log copy to /tmp/tes1-testn/ finished!
/var/log/btmp copy to /tmp/tes1-testn/ finished!
/var/log/spooler-20160804 copy to /tmp/tes1-testn/ finished!
/var/log/tallylog copy to /tmp/tes1-testn/ finished!
.......
[root@www test]# cat exercise13.sh 
#!/bin/bash
sdir="/var/log"
ddir="/tmp/tes1-testn/"
[ ! -e $ddir ] && mkdir $ddir
for i in $(ls -l $sdir | grep "^-" | awk '{print $NF}');do
cp $sdir/$i  $ddir
echo "$sdir/$i copy to $ddir finished!"
done
for m in $(ls -l $sdir | grep "^d" | awk '{print $NF}');do
cp -r $sdir/$m  $ddir
echo  "$sdir/$m copy to $ddir finished!"
done
for n  in $(ls -l $sdir | grep "^l" | awk '{print $NF}');do
cp -d  $sdir/$i  $ddir
echo "$sdir/$n copy to $ddir finished!"
done
for k in $(ls -l $sdir | grep -v "^[-dl]" | awk '{print $NF}');do
cp -a $sdir/$i  $ddir
echo "$sdir/$k copy to $ddir finished!"
done
[root@www test]# cd /tmp/tes1-testn/
[root@www tes1-testn]# ls -a
.                     btmp-20160804  maillog-20160804   spooler
..                    ConsoleKit     messages           spooler-20160804
anaconda.ifcfg.log    cron           messages-20160804  sssd
anaconda.log          cron-20160804  ntpstats           tallylog
anaconda.program.log  cups           pm-powersave.log   wpa_supplicant.log
anaconda.storage.log  dmesg          ppp                wtmp
anaconda.syslog       dmesg.old      prelink            Xorg.0.log
anaconda.xlog         dracut.log     sa                 Xorg.0.log.old
anaconda.yum.log      gdm            samba              Xorg.1.log
audit                 httpd          secure             Xorg.9.log
boot.log              lastlog        secure-20160804    yum.log
btmp                  maillog        spice-vdagent.log

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

(0)
SnooSnoo
上一篇 2016-08-05 16:09
下一篇 2016-08-05 16:09

相關推薦

  • man的用法

    man的使用 man命令是Linux下的幫助指令,通過man指令可以查看Linux中的指令幫助、配置文件幫助和編程幫助等信息。 語法 man(選項)(參數) 選項 -a:在所有的man幫助手冊中搜索; -f:等價于whatis指令,顯示給定關鍵字的簡短描述信息; -P:指定內容時使用分頁程序; -M:指定man手冊搜索的路徑。 參數 數字:指定從哪本man手…

    Linux干貨 2018-03-04
  • 內核體系

    單內核體系設計、但充分借鑒了微內核設計體系的優點,為內核引入模塊化機制。 內核組成部分核心,一般為bzImage,通常在/boot目錄下,名稱為vmlinuz-VERSION-RELEASE;: kernel: 內核 kernel object: 內核對象,一般放置于/lib/modules/VERSION-RELEASE/ [ ]: N [M]: M [*…

    Linux干貨 2015-09-02
  • 筆記整理:權限管理1-基礎權限管理&默認權限

    權限管理: 權限的分配根據owner和group來進行分配的   對于文件,各個權限的意義: r:可以使用工具查看內容 w:往里寫 x:運行,提請內核發起一個進程 對于目錄,各個權限的意義: r:用ls 查看目錄列表   w:可以創建或刪除目錄中的文件   x:可以使用ls -l查看文件列表,也可cd進去   管理命令…

    Linux干貨 2016-08-05
  • 進程的基本動作機制

    進程 我們知道硬件到用戶使用分為:硬件,內核(系統),軟件。 硬件也就是我們常見到的計算機等等,就相當于我們的身體,內核就相當于是我們的大腦,軟件就相當于我們的動作。而進程就相當于我們怎么去實現這些動作。 進程是程序的一個具體實現,同一個程序可以執行多次,每次都可以在內存中開辟獨立的空間來裝載,從而產生多個進程。不同的進程還可以擁有各自獨立的IO接口。 進程…

    Linux干貨 2016-06-01
  • Linux基礎入門

    Linux基礎入門 1、Linux發行版及哲學思想 1.1 Linux發行版          Linux發行版(Linux Distribution,也被叫做GUN/Linux發行版),為一般用戶預先集成好的Linux操作系統及各種應用軟件。Linux發行版通常包含了包括桌面環…

    Linux干貨 2016-02-28
  • 第六周學習總結

    寫在前面 在互聯網+的時代,網絡顯得越發重要,如果現在你一頓不吃飯可能還沒事但是一個小時沒網絡,估計你都要瘋了。那么網絡到底是個啥呢?看不見又摸不著。拿著一臺電腦怎么和網絡建立連接呢?那么下面要講解的內容或許可以給你解決個大概。 閑聊網絡 如果讓你修一棟房子你會一層一層的往上修,并且規劃好一層用來開商鋪,二層用來開超市,上面一層用來干嘛等等,其實網絡也是一樣…

    2018-01-08

評論列表(1條)

  • 馬哥教育
    馬哥教育 2016-08-05 16:54

    寫的很好,排版也很棒,加油

欧美性久久久久