1、寫一個腳本,判斷當前系統上所有用戶的shell是否為可登錄shell(即用戶的shell不是/sbin/nologin);分別這兩類用戶的個數;通過字符串比較來實現;
#!/bin/bash
#
declare -i count1=0
declare -i count2=0
for i in $(awk -F : '{print $7}' /etc/passwd); do
if [[ $i == "/sbin/nologin" ]]; then
let count1++
else
let count2++
fi
done
echo "total nologin:$count1"
echo "total longin: $count2"
腳本執行效果:
[root@localhost ~]# bash file1.sh
total nologin:19
total longin: 5
2、寫一個腳本
(1) 獲取當前主機的主機名,保存于hostname變量中;
(2) 判斷此變量的值是否為localhost,如果是,則將當前主機名修改為www.magedu.com;
(3) 否則,則顯示當前主機名;
#!/bin/bash
#
hostname=$(hostname)
if [[ $hostname == "localhost" ]]; then
hostname www.magedu.com
else
echo $hostname
fi
腳本執行效果:
[root@localhost ~]# bash file1.sh
[root@localhost ~]# hostname
3、寫一個腳本,完成如下功能
(1) 傳遞一個磁盤設備文件路徑給腳本,判斷此設備是否存在;
(2) 如果存在,則顯示此設備上的所有分區信息;
#!/bin/bash
#
[ $# -lt 1 ] && echo "At least a argument" && exit 2
if [ -b $1 ]; then
fdisk -l $1
else
echo "no block device or no exist"
fi
腳本執行效果:
[root@localhost ~]# bash file1.sh /dev/sda
Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000864ff
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 196607 97280 83 Linux
/dev/sda2 196608 273661951 136732672 8e Linux LVM
4、寫一個腳本,完成如下功能
腳本能夠接受一個參數;
(1) 如果參數1為quit,則顯示退出腳本,并執行正常退出;
(2) 如果參數1為yes,則顯示繼續執行腳本;
(3) 否則,參數1為其它任意值,均執行非正常退出;
#!/bin/bash
#
[ $# -lt 1 ] && echo "At least a argument" && exit 1
if [[ $1 == "quit" ]]; then
echo "quit script" && exit 0
elif [[ $1 == "yes" ]]; then
echo "contine run script"
else
echo "except quit"
fi
腳本執行效果:
[root@localhost ~]# bash file1.sh quit
quit script
[root@localhost ~]# bash file1.sh yes
contine run script
[root@localhost ~]# bash file1.sh no
except quit
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) 其它任意值,則顯示錯誤壓縮工具,并執行非正常退出;
#!/bin/bash
#
[ $# -lt 1 ] && echo "At least a argument" && exit 2
if [[ $1 == "gzip" ]]; then
tar -zcf /backups/etc-$(date +"%Y%m%d").tar.gz /etc
elif [[ $1 == "bzip2" ]]; then
tar -jcf /backups/etc-$(date +"%Y%m%d").tar.bz2 /etc
elif [[ $1 == "xz" ]]; then
tar -Jcf /backups/etc-$(date +"%Y%m%d").tar.xz /etc
else
echo "error compress utils" && exit 2
fi
腳本執行效果:
[root@localhost ~]# bash file1.sh gzip
tar: Removing leading `/' from member names
[root@localhost ~]# bash file1.sh bzip2
tar: Removing leading `/' from member names
[root@localhost ~]# bash file1.sh xz
tar: Removing leading `/' from member names
[root@localhost ~]# bash file1.sh cat
error compress utils
6、寫一個腳本,接受一個路徑參數:
(1) 如果為普通文件,則說明其可被正常訪問;
(2) 如果是目錄文件,則說明可對其使用cd命令;
(3) 如果為符號鏈接文件,則說明是個訪問路徑;
(4) 其它為無法判斷;
#!/bin/bash
#
[ $# -lt 1 ] && echo "At least a argument" && exit 2
if [ -L $1 ]; then
ls -l $1
elif [ -d $1 ]; then
cd $1;pwd
elif [ -f $1 ]; then
cat $1
else
echo "unknow"
fi
腳本運行效果:
[root@localhost ~]# bash file1.sh /etc/rc.d/rc3.d/K50netconsole
lrwxrwxrwx. 1 root root 20 Aug 6 06:14 /etc/rc.d/rc3.d/K50netconsole -> ../init.d/netconsole
[root@localhost ~]# bash file1.sh /etc/issue
\S
Kernel \r on an \m
[root@localhost ~]# bash file1.sh /etc/rc.d
/etc/rc.d
7、寫一個腳本,取得當前主機的主機名,判斷
(1) 如果主機名為空或為localhost,或為"(none)",則將其命名為mail.magedu.com;
(2) 否則,顯示現有的主機名即可;
#!/bin/bash
#
hostname=$(hostname)
if [ $hostname == "" -o $hostname == "localhost" -o $hostname == "(none)" ]; then
hostname mail.magedu.com
else
echo $hostname
fi
腳本運行效果:
[root@localhost ~]# bash file2.sh
[root@localhost ~]# hostname
mail.magedu.com
8、寫一腳本,接受一個用戶名為參數;
(1) 如果用戶的id號為0,則顯示其為管理員;
(2) 如果用戶的id號大于0且小于500, 則顯示其為系統用戶;
(3) 否則,則顯示其為普通用戶;
#!/bin/bash
#
[ $# -lt 1 ] && echo "At least a argument" && exit 2
! id $1 &>/dev/null && echo "user no exist" && exit 3
if [ $(id -u $1) -eq 0 ]; then
echo "Is root"
elif [ $(id -u $1) -gt 0 -a $(id -u $1) -lt 500 ]; then
echo "Is system"
else
echo "Is user"
fi
腳本執行結果:
[root@localhost ~]# bash file1.sh root
Is root
[root@localhost ~]# bash file1.sh bin
Is system
[root@localhost ~]# bash file1.sh user10
user exist
[root@localhost ~]# bash file1.sh join
Is user
10、寫一個腳本,傳遞一個用戶名參數給腳本;
(1) 如果用戶的id號大于等于500,且其默認shell為以sh結尾的字符串,則顯示“a user can log system.”類的字符串;
(2) 否則,則顯示無法登錄系統;
#!/bin/bash
#
[ $# -lt 1 ] && echo "At least a argument" && exit 2
! id $1 &>/dev/null && echo "user no exist" && exit 3
if [[ $(id -u $1) -ge 500 && $(grep $1 /etc/passwd |cut -d : -f 7) =~ sh$ ]]; then
echo "a user can log system."
else
echo "login faild"
fi
腳本運行效果:
[root@localhost ~]# bash file1.sh join
a user can log system.
[root@localhost ~]# bash file1.sh root
login faild
11、寫一個腳本,完成如下任務 :
(1) 按順序分別復制/var/log目錄下的每個直接文件或子目錄至/tmp/test1-testn目錄中;
(2) 復制目錄時,才使用cp -r命令;
(3) 復制文件時使用cp命令;
(4) 復制鏈接文件時使用cp -d命令;
(5) 余下的所有類型,使用cp -a命令;
#!/bin/bash
#
for i in /var/log/*; do
if [ -d $i ]; then
cp -r $i /tmp/test1/
elif [ -f $i ]; then
cp $i /tmp/test2/
elif [ -L $i ]; then
cp -d $i /tmp/test3/
else
cp -a $i /tmp/test4/
fi
done
腳本執行結果:
[root@localhost ~]# ll /tmp/test1
total 4
drwxr-xr-x. 2 root root 4096 Oct 15 00:35 anaconda
drwxr-x—. 2 root root 22 Oct 15 00:35 audit
drwx——. 2 root root 6 Oct 15 00:35 httpd
drwx——. 2 root root 6 Oct 15 00:35 ppp
[root@localhost ~]# ll /tmp/test2
total 3040
-rw-r–r–. 1 root root 8406 Oct 15 00:35 boot.log
-rw——-. 1 root root 0 Oct 15 00:35 btmp
-rw——-. 1 root root 384 Oct 15 00:35 btmp-20161005
-rw——-. 1 root root 34326 Oct 15 00:35 cron
[root@localhost ~]# ll /tmp/test3
total 0
[root@localhost ~]# ll /tmp/test4
total 0
原創文章,作者:heianyangguo,如若轉載,請注明出處:http://www.www58058.com/50695
每一個腳本都可以看到實現的效果,很好,下次能將排版美化一下