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.178linux.com/36055

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

相关推荐

  • MySQL高可用架构之MHA

    MySQL高可用架构之MHA 1、关于MHA MHA(Master HA)是一款开源的MySQL的高可用程序,它为MySQL主从复制架构提供了automating master failover功能。MHA在监控到master节点故障时,会提升其中拥有的最新数据的slave节点成为新的master节点,在此期间,MHA会通过其它从节点获取额外信息来避免一致性…

    Linux干货 2017-03-30
  • 用户和组管理类命令的使用

    1.列出当前系统所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示以此即可; who | cut -d' ' -f1 | sort | uniq 2.取出最后登录到当前系统的用户的相关信息; who | head&nb…

    Linux干货 2016-11-21
  • bash的特性总结

    什么是bash:       shell作为用户与计算机内核交互的接口,是用户与计算机沟通的桥梁,而bash(borne again shell)是众多shell里面最为流行一种,bash作为众多shell里面的一种有着众多的特性,掌握bash的众多特性将会是我们今后学习linux必经之路。&nbs…

    Linux干货 2015-10-27
  • Linux高级文件系统管理之磁盘配额、RAID和LVM的使用

    磁盘配额Quota 磁盘配额(Quota)的作用:   在Linux系统中,由于是多用户、多任务的环境,所以会有多用户共同使用一个硬盘空间的情况发生,如果其中有少数几个用户大量占掉了硬盘空间的话,那肯定影响其他用户的使用权限。因此管理员应该适当限制硬盘的空间给用户,以妥善分配系统资源。 磁盘配额的一般用途    比较常使用的几种…

    Linux干货 2016-09-02
  • linux 加密和证书

    安全目标:机密性:明文传输的ftp, http,telnet 不安全数据完整性:身份验证:可用性:安全技术:认证,授权,安全通信,审计密码算法和协议:对称加密,公钥加密,单向加密,认证协议 1、对称加密:加密,解密使用同一个秘钥,效率高 DES:Data Encrption Standard, 56bit3DES:AES:AdvancedBlowfish缺点…

    2017-09-11
  • 网络管理命令

    linux网络属性     ifconfig命令家族:ifconfig,route,netstat ifconfig命令:接口及地址查看和管理     ifconfig [interface]:     ifconfig -a:显示所有接口…

    Linux干货 2016-09-13