shell編程if及find查找作業

寫一個腳本/root/bin/createuser.sh,實現如下功能:使用一個用戶名做為參數,如果指定參數的用戶存在,就顯示其存在,否則添加之;顯示添加的用戶的id號等信息

[root@www sh.log]# cat createuser.sh 
#!/bin/bash
#author
#使用一個用戶名作為參數,如果指定參數的用戶存在,就顯示其存在,否則添加,顯示添加用戶的id號等信息
read -p "username:" user
id $user &> /dev/null
if [ $? -eq 0 ];then
	echo  "$user already exist"
else
	useradd $user &> /dev/null
	echo "`id $user`"
fi
[root@www sh.log]# bash createuser.sh
username:wang
wang already exist
[root@www sh.log]# bash createuser.sh 
username:xiaodeng   
uid=1002(xiaodeng) gid=1002(xiaodeng) groups=1002(xiaodeng)

寫一個腳本/root/bin/yesorno.sh,提示用戶輸入yes或no,并判斷用戶輸入的是yes還是no,或是其它信息

[root@www sh.log]# cat yesorno.sh 
#!/bin/bash
#author:DYW
#提示用戶輸入yes或no,并判斷用戶輸入的是yes還是no,或是其他信息
read -p "input yes or no please:" biu
piu=`echo "$biu" | tr "[a-z]" "[A-Z]"`
case $piu in
	Y|YES)
	echo "you select yes"
	;;
	N|NO)
	echo "you select no"
	;;
	*)
	echo "you select others"
	;;
esac

[root@www sh.log]# bash yesorno.sh 
input yes or no please:yES
you select yes
[root@www sh.log]# bash yesorno.sh 
input yes or no please:YEs
you select yes
[root@www sh.log]# bash yesorno.sh
input yes or no please:No
you select no
[root@www sh.log]# bash yesorno.sh
input yes or no please:12312
you select others

寫一個腳本/root/bin/filetype.sh,判斷用戶輸入文件路徑,顯示其文件類型(普通,目錄,鏈接,其它文件類型)

[root@www sh.log]# bash filetype.sh 
input filename please:/sh.log/qiuhe.sh
The /sh.log/qiuhe.sh is ordinary file
[root@www sh.log]# cat filetype.sh 
#!/bin/bash
#suthor:DYW
#判斷用戶輸入文件路徑,顯示其文件類型(普通,目錄,鏈接,其他文件類型)
read -p "input filename please:" file
	if [ -z $file  ];then
		echo "Please enter a path"
	else 
		if [ -L $file ];then
			echo "The $file is Symbol file"
		elif [ -c $file ];then
			echo "The $file is char file"
		elif [ -d $file ];then
			echo "The $file is dir file"
		elif [ -b $file ];then
			echo "The $file is block file"
		elif [ -f $file ];then
			echo "The $file is ordinary file"
		elif [ -p $file ];then
			echo "The $file is pipe file"
		elif [ -S $file ];then
			echo "The $file is scoket file"
		else
			echo "Enter the correct file path"
		fi
	fi
[root@www sh.log]# bash filetype.sh 
input filename please:/wang/file1
The /wang/file1 is ordinary file
[root@www sh.log]# bash filetype.sh 
input filename please:/wang/jiaoben.sh
Enter the correct file path
[root@www sh.log]# bash filetype.sh 
input filename please:/wang/jiaoben1.sh
The /wang/jiaoben1.sh is ordinary file
[root@www sh.log]# bash filetype.sh 
input filename please:/usr/bin
The /usr/bin is dir file

寫一個腳本/root/bin/checkint.sh,判斷用戶輸入的參數,是否為正整數

[root@www sh.log]# cat cheskint.sh 
#!/bin/bash
#author:DYW
#判斷用戶輸入的參數,是否為正整數
read -p "input a number please:" number
	if [ -z $number ];then
		echo "You must input a number!"
		exit
		else
			if let var=$number &>/dev/null;then
				if [ $? -eq 0 ];then
					if [ $number -lt 0 ];then
						echo "$number is Negative integer"
					elif [ $number -gt 0 ];then
						echo "$number is Positive integer"
					else
						echo "$number is 0"
					fi
				fi
			else
				echo "$number is not integer"
			fi
	fi
[root@www sh.log]# bash cheskint.sh 
input a number please:10
10 is Positive integer
[root@www sh.log]# bash cheskint.sh 
input a number please:-112  
-112 is Negative integer
[root@www sh.log]# bash cheskint.sh 
input a number please:0
0 is not integer
[root@www sh.log]# bash cheskint.sh 
input a number please:
You must input a number!
[root@www sh.log]# bash cheskint.sh 
input a number please:asdhiuadqwl
asdhiuadqwl is not integer

查找/var目錄下屬主為root,且屬組為mail的所有文件

[root@www ~]# find /var -user root -group mail -ls
134321240    0 drwxrwxr-x   2 root     mail           46 Aug 12 18:30 /var/spool/mail

查找/var目錄下不屬于root、lp、gdm的所有文件

[root@www ~]# find /var/ -not \( -user root -o -user lp -o -user gdm \) -ls
134686482    0 drwx------   2 tss      tss             6 Nov 21  2015 /var/lib/tpm
443945    0 drwx------   2 postfix  root           24 Aug  9 06:23 /var/lib/postfix
   833    4 -rw-------   1 postfix  postfix        33 Aug  9 17:41 /var/lib/postfix/master.lock
134914400    0 -rw-rw----   1 wang     mail            0 Aug 11 13:02 /var/spool/mail/wang
134974157    0 -rw-rw----   1 laowang  mail            0 Aug 12 10:10 /var/spool/mail/laowang
134993654    0 -rw-rw----   1 xiaodeng mail            0 Aug 12 18:30 /var/spool/mail/xiaodeng
134924704    0 drwx------   2 postfix  root            6 Jun 10  2014 /var/spool/postfix/active
202230254    0 drwx------   2 postfix  root            6 Jun 10  2014 /var/spool/postfix/bounce
443946    0 drwx------   2 postfix  root            6 Jun 10  2014 /var/spool/postfix/corrupt
67719609    0 drwx------   2 postfix  root            6 Jun 10  2014 /var/spool/postfix/defer
134924705    0 drwx------   2 postfix  root            6 Jun 10  2014 /var/spool/postfix/deferred
202230255    0 drwx------   2 postfix  root            6 Jun 10  2014 /var/spool/postfix/flush
443947    0 drwx------   2 postfix  root            6 Jun 10  2014 /var/spool/postfix/hold
        6 Jun 10  2014 /var/spool/postfix/trace
   ...

查找/var目錄下最近一周內其內容修改過,同時屬主不為root,也不是postfix的文件

[root@www ~]# find /var/ \( -not \( -user root -o -user postfix \) -mtime -7 \) -ls
134914400    0 -rw-rw----   1 wang     mail            0 Aug 11 13:02 /var/spool/mail/wang
134974157    0 -rw-rw----   1 laowang  mail            0 Aug 12 10:10 /var/spool/mail/laowang
134993654    0 -rw-rw----   1 xiaodeng mail            0 Aug 12 18:30 /var/spool/mail/xiaodeng
134993684    0 -rw-rw----   1 gdm      mail            0 Aug 12 23:03 /var/spool/mail/gdm
[root@www ~]# stat /var/spool/mail/wang 
  File: ‘/var/spool/mail/wang’
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d	Inode: 134914400   Links: 1
Access: (0660/-rw-rw----)  Uid: ( 1000/    wang)   Gid: (   12/    mail)
Context: unconfined_u:object_r:mail_spool_t:s0
Access: 2016-08-11 13:02:38.126781165 +0800
Modify: 2016-08-11 13:02:38.126781165 +0800
Change: 2016-08-11 13:02:38.126781165 +0800
 Birth: -

查找當前系統上沒有屬主或屬組,且最近一個周內曾被訪問過的文件

[root@www ~]# find / \( -nouser -o -nogroup \) -atime -7

查找/etc目錄下大于1M且類型為普通文件的所有文件

[root@www ~]# find /etc/ \( -size +1M -o -type f \) -ls
134320258    4 -rw-r--r--   1 root     root          541 Aug  9 05:48 /etc/fstab
134320259    0 -rw-------   1 root     root            0 Aug  9 05:48 /etc/crypttab
134993652    4 -rw-r--r--   1 root     root           81 Aug 12 20:25 /etc/resolv.conf
   151    4 -rw-r--r--   1 root     root         1690 Dec  9  2015 /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
   152    4 -rw-r--r--   1 root     root         1004 Dec  9  2015 /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Debug-7
   153    4 -rw-r--r--   1 root     root         1690 Dec  9  2015 /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Testing-7
67355015    4 -rw-r--r--   1 root     root         2388 Jun 29  2015 /etc/pki/tls/certs/Makefile
67355016    4 -rwxr-xr-x   1 root     root          610 Jun 29  2015 /etc/pki/tls/certs/make-dummy-cert
67355017    4 -rwxr-xr-x   1 root     root          829 Jun 29  2015 /etc/pki/tls/certs/renew-dummy-cert
134484404    8 -rwxr-xr-x   1 root     root         5178 Jun 29  2015 /etc/pki/tls/misc/CA
134484405    4 -rwxr-xr-x   1 root     root          119 Jun 29  2015 /etc/pki/tls/misc/c_hash
134484406    4 -rwxr-xr-x   1 root     root          152 Jun 29  2015 /etc/pki/tls/misc/c_info
134484407    4 -rwxr-xr-x   1 root     root          112 Jun 29  2015 /etc/pki/tls/misc/c_issuer
.....
[root@www ~]# stat /etc/hostname
  File: ‘/etc/hostname’
  Size: 22        	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 134974153   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:hostname_etc_t:s0
Access: 2016-08-11 07:51:46.178807313 +0800
Modify: 2016-08-09 05:56:51.103010237 +0800
Change: 2016-08-09 05:56:51.103010237 +0800
 Birth: -

查找/etc目錄下所有用戶都沒有寫權限的文件

[root@www ~]# find /etc/ -not -perm /222 -ls 
67305775  196 -r--r--r--   1 root     root       198453 Aug  9 05:50 /etc/pki/ca-trust/extracted/java/cacerts
134417508  352 -r--r--r--   1 root     root       359773 Aug  9 05:50 /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt
201627950  264 -r--r--r--   1 root     root       266702 Aug  9 05:50 /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
201627951  216 -r--r--r--   1 root     root       217510 Aug  9 05:50 /etc/pki/ca-trust/extracted/pem/email-ca-bundle.pem
201627952  208 -r--r--r--   1 root     root       211626 Aug  9 05:50 /etc/pki/ca-trust/extracted/pem/objsign-ca-bundle.pem
...

查找/etc目錄下至少有一類用戶沒有執行權限的文件

[root@www ~]# find /etc/ -not -perm -111 -ls 
134751833    4 -rw-r--r--   1 root     root          936 Mar  6  2015 /etc/mke2fs.conf
134974150    4 -rw-r--r--   1 root     root           37 Aug  9 05:56 /etc/vconsole.conf
134974152    4 -rw-r--r--   1 root     root           19 Aug  9 05:56 /etc/locale.conf
134974153    4 -rw-r--r--   1 root     root           22 Aug  9 05:56 /etc/hostname
134679689    4 -rw-r--r--   1 root     root          163 Aug  9 05:49 /etc/.updated
134914398   12 -rw-r--r--   1 root     root        12288 Aug  9 06:23 /etc/aliases.db
...

查找/etc/init.d目錄下,所有用戶都有執行權限,且其它用戶有寫權限的文件

[root@www ~]# find /etc/init.d/ -perm -113 -ls
67830539    0 ---x--x-wx   1 root     root            0 Aug 13 02:40 /etc/init.d/file1
...

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

(1)
DYWDYW
上一篇 2016-08-16
下一篇 2016-08-16

相關推薦

  • Shell腳本編程

    Shell腳本編程

    2017-09-20
  • 正則表達式和變量寫腳本

    #!/bin/bash 檢查系統所有用戶的shell是否為bash? 注釋:用grep查找是否有此類用戶,如果有則為真,則$?必然為0;顯示的passwd結果對我們沒意義,所以重定向到空。  grep “\bbash\b$” /etc/passwd &> /dev/null  A=`echo $?` …

    Linux干貨 2017-04-16
  • Nginx的簡單配置和簡單實驗..

    yum install nginx #安裝Nginx,安裝包在epel源中 systemctl start nginx.service #啟動服務 #實驗:配置一個虛擬主機 #先給一個網頁文件 ~]#mkdir /data/nginx/vhost1 -pv ~]#vim /data/nginx/vhost1/index.html <h1>Ngin…

    Linux干貨 2017-06-19
  • 計算機基礎第一部分

    ** 第一課:計算機基礎知識** 第一章計算機的組成 計算機是由硬件(Hardwaer)和軟件(Software)來組成兩大類,他是一臺可以自由傳輸數據, 儲存數據,娛樂,工作等等的智能設備。 第一節計算機硬件 主板架構圖 內存====用來存儲臨時數據,弱點喜歡松動一般電腦黑屏都是他松動了,導致電腦不能機 基本上%80以上的電腦不能開機都是他出了問題,服務器…

    Linux干貨 2017-05-19
  • 小巧精悍——常見文本處理工具用法及技巧總結

    小巧精悍——常見文本處理工具用法及技巧總結 Linux最重要的哲學思想就是:一切皆文件??梢娢募拔募僮髟贚inux當中是多么的重要。在Linux系統中我們也會經常用到各種文本文件處理的操作,熟練使用這些小巧精悍的文本處理工具,在關鍵時刻往往展現非凡的神奇功效。常見文本處理小工具:cat、hexdump、nl、less、head、tail、cut、tr、r…

    Linux干貨 2016-11-05
  • openssl基礎應用

    參考http://www.www58058.com/2704 http://www.williamlong.info/archives/837.html 一、前言 什么是openssl?講openssl之前我們先了解下什么是ssl?ssl是secure socket layer的簡稱,其使用對稱加密解密,非對稱加密解密(公鑰加密解密),單向加密解密結合證書實…

    Linux干貨 2015-09-27
欧美性久久久久