linux基础正则表达式shell基础文件查找和压缩

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux基础正则表达式shell基础文件查找和压缩相关的知识,希望对你有一定的参考价值。

linux基础正则表达式、shell基础、文件查找和压缩

1.shell编程显示电脑的基本信息,初级基础脚本。只适合6.7版本的。

COLOR="\033[1;36m"
COLOREND="\033[0m"
echo -e "CPU type is $COLOR `lscpu |grep ‘Model name‘|tr -s ‘ ‘|cut -d: -f2`$COLOREND"
echo -e "Disk space is $COLOR `lsblk |grep ‘disk‘ |tr -s ‘ ‘|cut -d‘ ‘ -f4|head -n 1` $COLOREND"
echo -e "Memory size is $COLOR `free -h |grep ‘Mem‘|tr -s ‘ ‘|cut -d‘ ‘ -f2` $COLOREND"
echo -e "OS Verion is $COLOR `cat /etc/redhat-release` $COLOREND"
echo -e "kernel version is $COLOR `uname -r` $COLOREND"
echo -e "IPaddr is $COLOR ` ifconfig ens33 |grep ‘netmask‘|tr -s ‘ ‘|cut -d‘ ‘ -f3`$COLOREND"        

2.找出ifconfig “网卡名” 命令结果中本机的IPv4地址

[root@centos7 scripts38]#ifconfig ens33 |grep netmask|tr -s ‘ ‘|cut -d‘ ‘ -f3
192.168.11.131
[root@centos7 ~]#ifconfig ens33 |grep ask | grep -o "\([0-9]\1,3\\.\)\3\[0-9]\1,3\"|head -1
192.168.6.136

3.查出分区空间使用率的最大百分比值

[root@centos7 scripts38]#df|grep ‘/dev/sd‘|tr -s ‘ ‘|cut -d‘ ‘ -f5|tr -d %|sort -nr|head -n 1
17

4.查出用户UID最大值的用户名、UID及shell类型

[root@centos7 scripts38]#cat /etc/passwd |cut -d: -f1,3,7 |sort -t: -k2 -nr|head -n 1
nfsnobody:65534:/sbin/nologin

5.查出/tmp的权限,以数字方式显示

[root@centos7 ~]#stat /tmp |grep ‘drwx‘|cut -d‘ ‘ -f2|tr -dc [0-9]
1777

6.统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序

netstat -tun|grep ESTAB|tr -s " " : |cut -d: -f6|sort -nr|uniq -c
      1 192.168.6.1

7.显示/proc/meminfo文件中以大小s开头的行(要求:使用两种方法)

[root@centos7 ~]#grep ^[sS]    /proc/meminfo 
SwapCached:         2288 kB
SwapTotal:       2097148 kB
SwapFree:        2058044 kB
Shmem:             16796 kB
Slab:              88744 kB
SReclaimable:      39980 kB
SUnreclaim:        48764 kB
[root@centos7 ~]#grep ‘^s\|^S‘    /proc/meminfo 
SwapCached:         2288 kB
SwapTotal:       2097148 kB
SwapFree:        2058044 kB
Shmem:             16796 kB
Slab:              88744 kB
SReclaimable:      39980 kB
SUnreclaim:        48764 kB
[root@centos7 ~]#grep ^[s\|S]    /proc/meminfo 
SwapCached:         2288 kB
SwapTotal:       2097148 kB
SwapFree:        2058044 kB
Shmem:             16796 kB
Slab:              88744 kB
SReclaimable:      39980 kB
SUnreclaim:        48764 kB
[root@centos7 ~]#grep -w \<‘s\|S.*‘ /proc/meminfo 
SwapCached:         2288 kB
SwapTotal:       2097148 kB
SwapFree:        2058044 kB
Shmem:             16796 kB
Slab:              88716 kB
SReclaimable:      39980 kB
SUnreclaim:        48736 kB

8.显示/etc/passwd文件中不以/bin/bash结尾的行

[root@centos7 ~]#cat /etc/passwd |grep -v  ‘/bin/bash$‘
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

9.显示用户rpc默认的shell程序

[root@centos7 ~]#cat /etc/passwd |grep ‘\<rpc\>‘|cut -d: -f7
/sbin/nologin

10.找出/etc/passwd中的两位或三位数

[root@centos7 ~]#cat /etc/passwd |grep   "\<[0-9]\2,3\\>"
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
libstoragemgmt:x:998:995:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin

11.显示CentOS7的/etc/grub2.cfg文件中,至少以一个空白字符开头的且后面有非
空白字符的行

[root@centos7 ~]#cat /etc/grub2.cfg |grep ^[[:space:]].*
  load_env
   set default="$next_entry"
   set next_entry=
   save_env next_entry
   set boot_once=true
   set default="$saved_entry"
  menuentry_id_option="--id"
  menuentry_id_option=""
  set saved_entry="$prev_saved_entry"
  save_env saved_entry
  set prev_saved_entry=
  save_env prev_saved_entry
  set boot_once=true
  if [ -z "$boot_once" ]; then
    saved_entry="$chosen"
    save_env saved_entry
  fi
  if [ x$feature_all_video_module = xy ]; then
    insmod all_video
  else
    insmod efi_gop
    insmod efi_uga
    insmod ieee1275_fb
    insmod vbe
    insmod vga
    insmod video_bochs
    insmod video_cirrus
  fi
  set timeout_style=menu
  set timeout=5
  set timeout=5
  source $prefix/user.cfg
  if [ -n "$GRUB2_PASSWORD" ]; then
    set superusers="root"
    export superusers
    password_pbkdf2 root $GRUB2_PASSWORD
  fi
    load_video
    set gfxpayload=keep
    insmod gzio
    insmod part_msdos
    insmod xfs
    set root=‘hd0,msdos1‘
    if [ x$feature_platform_search_hint = xy ]; then
      search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint=‘hd0,msdos1‘  b6888a3b-3755-49af-b1db-5b412eda8657
    else
      search --no-floppy --fs-uuid --set=root b6888a3b-3755-49af-b1db-5b412eda8657
    fi
    linux16 /vmlinuz-3.10.0-957.el7.x86_64 root=UUID=05bdb265-31b6-4a0f-942b-f69d4b103e8d ro crashkernel=auto rhgb quiet LANG=en_US.UTF-8
    initrd16 /initramfs-3.10.0-957.el7.x86_64.img
    load_video
    insmod gzio
    insmod part_msdos
    insmod xfs
    set root=‘hd0,msdos1‘
    if [ x$feature_platform_search_hint = xy ]; then
      search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint=‘hd0,msdos1‘  b6888a3b-3755-49af-b1db-5b412eda8657
    else
      search --no-floppy --fs-uuid --set=root b6888a3b-3755-49af-b1db-5b412eda8657
    fi
    linux16 /vmlinuz-0-rescue-55bf685d782642d9bcbb1eaf123db175 root=UUID=05bdb265-31b6-4a0f-942b-f69d4b103e8d ro crashkernel=auto rhgb quiet
    initrd16 /initramfs-0-rescue-55bf685d782642d9bcbb1eaf123db175.img
  source $config_directory/custom.cfg
  source $prefix/custom.cfg;

12.找出“netstat -tan”命令结果中以LISTEN后跟任意多个空白字符结尾的行

[root@centos7 ~]#netstat -tan|grep ‘LISTEN[[:space:]]\+‘
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:6000            0.0.0.0:*               LISTEN     
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp6       0      0 :::111                  :::*                    LISTEN     
tcp6       0      0 :::6000                 :::*                    LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:631                 :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN  

13.显示CentOS7上所有UID小于1000以内的用户名和UID

[root@centos7 ~]#cat /etc/passwd |cut -d‘:‘ -f1,3 |grep -v ‘\<[0-9]\4\\>‘
root:0
bin:1
daemon:2
adm:3
lp:4
sync:5
shutdown:6
halt:7
mail:8
operator:11
games:12
ftp:14
nobody:99
systemd-network:192
dbus:81
polkitd:999
libstoragemgmt:998
colord:997
rpc:32
gluster:996
saslauth:995
abrt:173
rtkit:172
pulse:171
radvd:75
rpcuser:29
nfsnobody:65534
unbound:994
chrony:993
qemu:107
tss:59
usbmuxd:113
geoclue:992
ntp:38
sssd:991
setroubleshoot:990
saned:989
gdm:42
gnome-initial-setup:988
sshd:74
avahi:70
postfix:89
tcpdump:72
[root@centos7 ~]#cat /etc/passwd |cut -d‘:‘ -f1,3 |grep ‘\<[0-9]\1,3\\>‘
root:0
bin:1
daemon:2
adm:3
lp:4
sync:5
shutdown:6
halt:7
mail:8
operator:11
games:12
ftp:14
nobody:99
systemd-network:192
dbus:81
polkitd:999
libstoragemgmt:998
colord:997
rpc:32
gluster:996
saslauth:995
abrt:173
rtkit:172
pulse:171
radvd:75
rpcuser:29
unbound:994
chrony:993
qemu:107
tss:59
usbmuxd:113
geoclue:992
ntp:38
sssd:991
setroubleshoot:990
saned:989
gdm:42
gnome-initial-setup:988
sshd:74
avahi:70
postfix:89
tcpdump:72

14.添加用户bash、testbash、basher、sh、nologin(其shell为/sbin/nologin),找 出/etc/passwd用户名和shell同名的行

[root@centos7 ~]#grep ‘\(.*\+\):.*\<\1$‘  /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
nologin:x:1026:1032::/home/nologin:/sbin/nologin
[root@centos7 ~]#egrep "(.*+):.*\<\1$" /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
nologin:x:1026:1032::/home/nologin:/sbin/nologin

15.利用df和grep,取出磁盘各分区利用率,并从大到小排序

[root@centos7 ~]#df |grep ‘/dev/sd‘|tr -s ‘ ‘|cut -d‘ ‘ -f5 |sort -nr
17%
5%
1%
[root@centos7 ~]#df |grep ‘/dev/sd‘|grep -o ‘[0-9]\0,3\%‘ |sort -nr 
17%
5%
1%

16.查看Linux大版本。

[root@centos7 data]#cat /etc/redhat-release |cut -d. -f1|grep -o ‘[[:digit:]]‘
7

17.显示三个用户root、mage、wang的UID和默认shell

[root@centos7 scripts38]#cat /etc/passwd |grep  -w ‘^root\|^wang\|^mage‘|cut -d: -f1,3,7
root:0:/bin/bash
wang:1027:/bin/bash
mage:1028:/bin/bash

18.找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行

[root@centos7 data]#cat /etc/rc.d/init.d/functions |grep  ‘^[[:alpha:]].*(.*).*‘
systemctl_redirect () 
checkpid() 
daemon() 
killproc() 
pidfileofproc() 
pidofproc() 
status() 
echo_success() 
echo_failure() 
echo_passed() 
echo_warning() 
update_boot_stage() 
success() 
failure() 
passed() 
warning() 
action() 
strstr() 
is_ignored_file() 
convert2sec() 
is_true() 
is_false() 
apply_sysctl() 
strstr "$(cat /proc/cmdline)" "rc.debug" && set -x

19.使用egrep取出/etc/rc.d/init.d/functions中其基名和?录名

目录名
[root@centos7 data]#echo /etc/rc.d/init.d/functions |egrep -o "^[/].*/"
/etc/rc.d/init.d/

20.统计last命令中以root登录的每个主机IP地址登录次数

[root@centos7 data]#last |grep ‘root‘   | egrep -o \([0-9]\1,3\\.\)\3\[0-9]\1,3\|sort|uniq -c|sort -nr
     70 192.168.6.1
      1 172.18.37.37

21.利用扩展正则表达式分别表示0-9、10-99、100-199、200-249、250-255

[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]

22.显示ifconfig命令结果中所有IPv4地址

[root@centos7 data]#ifconfig |egrep -o "(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.)3([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"
192.168.6.136
255.255.255.0
192.168.6.255
172.18.7.7
255.255.0.0
172.18.255.255
127.0.0.1
255.0.0.0
192.168.122.1
255.255.255.0
192.168.122.255

23.将此字符串:welcome to magedu linux 中的每个字符去重并排序,重复次数多的排到前面

[root@centos7 data]#echo welcome to magedu linux |grep -o "."|sort|uniq -c|sort -nr
      3 e
      3  
      2 u
      2 o
      2 m
      2 l
      1 x
      1 w
      1 t
      1 n
      1 i
      1 g
      1 d
      1 c
      1 a

24.编写脚本 backup.sh,可实现每日将/etc/目录备份到/backup/etcYYYYmm-dd中

COLOR="\033[1;36m"
COLOREND="\033[0m"
echo -e "$COLORStarting backup ...$COLOREND"
cp -av /etc/   /data/etc_backup-`date +%F`
echo -e "$COLORBackup is finished$COLOREND"

25.编写脚本 disk.sh,显示当前硬盘分区中空间利用率最大的值

COLOR="\033[1;36m"
COLOREND="\033[0m"
echo -e "$COLORStarting Hard Disk Inspection ...$COLOREND"                              
echo -e "$COLOR`df|grep "/dev/sd" |tr -s " "|cut -d ‘ ‘ -f5|sort -nr|head -1`$COLOREND"
echo -e "$COLORHard Disk Inspection is finished$COLOREND"

26.编写脚本 links.sh,显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数从大到小排序

COLOR="\033[1;36m"
COLOREND="\033[0m"
echo -e "$COLORLinsk ...$COLOREND"                              
echo -e "$COLOR` netstat -tun|grep ESTAB|tr -s " " : |cut -d: -f6|sort -nr|uniq -c`$COLOREND"
echo -e "$COLORLinsk$COLOREND"

27.通用版信息存放地点

[root@centos7 data]#cat /proc/partitions
硬盘大小存放位置
[root@centos7 data]#cat /proc/meminfo
内存大小存放位置
[root@centos7 data]#cat /proc/cpuinfo
CPU等存放位置

28.远程执行脚本。

先开启服务,6上面自带服务
[root@centos6 ~]#service httpd start
Starting httpd: httpd: apr_sockaddr_info_get() failed for centos6.10.localdomain
httpd: Could not reliably determine the server‘s fully qualified domain name, using 127.0.0.1 for ServerName
                                                           [  OK  ]
把脚本拷贝到6的主机上
[root@centos7 scripts38]#scp 2systeminfo.sh 192.168.6.129:/var/www/html/
The authenticity of host ‘192.168.6.129 (192.168.6.129)‘ can‘t be established.
RSA key fingerprint is SHA256:7SNWeID7Jr5cvOTZkLQWElsa/MbwymmEUZRhTd8qxJ4.
RSA key fingerprint is MD5:3e:fa:86:dd:c6:b8:41:34:a0:50:f3:f6:b3:17:82:9a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.6.129‘ (RSA) to the list of known hosts.
root@192.168.6.129‘s password: 
2systeminfo.sh                            100%  964   581.7KB/s   00:00    
查看是否拷贝到了6的主机上
[root@centos6 ~]#ls /var/www/html/
2systeminfo.sh
在其他主机上执行就可以实现了
[root@centos7 scripts38]#curl 192.168.6.129/2systeminfo.sh |bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--    100   964  100   964    0     0   335k      0 --:--:-- --:--:-- --:--:--  470k

29.找出“ldd /usr/bin/cat”命令的结果中的?件路径

[root@centos7 data]#ldd /usr/bin/cat | grep  -o ‘/[^[:space:]]\+‘
/lib64/libc.so.6
/lib64/ld-linux-x86-64.so.2

30.复制/etc/profile?/tmp/?录,?查找替换命令删除/tmp/profile?件中的??的空?字符

[root@centos7 data]#cp /etc/profile /tmp/
[root@centos7 data]#vim /tmp/profile 
:%s/^[[:space:]]\+//g

31.复制/etc/rc.d/init.d/functions?件?/tmp?录,?查找替换命令为/tmp/functions的每?开头为空?字符的?的??添加?个#号

[root@centos7 data]#cp /etc/rc.d/init.d/functions /tmp/
[root@centos7 data]#vim /tmp/functions 
:%s/^[[:space:]]/#/g

31.编写脚本 sumid.sh,计算/etc/passwd文件中的第10个用户和第20用户的UID之和

NUM1=` cat /etc/passwd |head -1 |cut -d: -f3`
NUM10=`cat /etc/passwd |head -10 |tail -1|cut -d: -f3`
SUM=$[NUM1+NUM10]
echo "num1 user UUID + num10 user UUID = $SUM"

32.编写脚本 sumspace.sh,传递两个文件路径作为参数给脚本,计算这两个文件中所有空白行之和

NUM1=` grep ‘^$‘ $1 |wc -l`
NUM2=` grep ‘^$‘ $2 |wc -l`
SUM=$[NUM1+NUM2]
echo "The sum of blank lines =$SUM"

33.编写脚本 sumfile.sh,统计/etc, /var, /usr 目录中共有多少个一级子目录和文件

ETC=`ls -l /etc/ | wc -l`
VAR=`ls -l /var/ | wc -l`
USR=`ls -l /usr/ | wc -l`
SUM=$[ETC+VAR+USR]
echo "sum=$SUM"

34.编写脚本 argsnum.sh,接受一个文件路径作为参数;如果参数个数小于1,
则提示用户“至少应该给一个参数”,并立即退出;如果参数个数不小于1,则显示第一个参数所指向的文件中的空白行数

[ $# -lt 1 ]  &&  echo "At least one parameter should be given"; exit 10;  
NUM1=` grep ‘^$‘ $1 |wc -l`
echo "The  blank lines =$NUM1"

35.编写脚本 hostping.sh,接受一个主机的IPv4地址做为参数,测试是否可连
通。如果能ping通,则提示用户“该IP地址可访问”;如果不可ping通,则提示用户“该IP地址不可访问”

. /data/scripts38/color.sh
read -p "Please enter a correct IP:" IP
# [[ $# -lt 1 ]]  &&  echo "Please enter a IP"; exit 10; 
[[ "$IP" =~  ^(([1-9]?[0-9]|1[0-9]2|2[0-4][0-9]|25[0-5])\.)3([1-9]?[0-9]|1[0-9]2|2[0-4][0-9]|25[0-5])$ ]] ||  echo -e "$COLOR Please enter a IP$COLOREND" ; exit 20;
ping -c1 -w2 $IP  &> /dev/null && echo -e "$COLOR该IP地址可访问$COLOREND"  ||echo -e "$COLOR该IP地址不可访问$COLOREND"

36.编写脚本 checkdisk.sh,检查磁盘分区空间和inode使用率,如果超过80%,就发广播警告空间将满

USE=` df|grep "/dev/sd" |tr -s " " %|cut -d "%" -f5|sort -nr|head -1` 
INODE=`df -i|grep "/dev/sd" |tr -s " " %|cut -d "%" -f5|sort -nr|head -1`
test $USE -gt 80 && echo " disk will be full " &&echo disk will be full | mail -s warning root || echo "disk is normal"
[[ "$INODE" -gt 80 ]] && echo " inode will be full " &&echo inode will be full | mail -s warning root || echo "inode is normal"
unset USE INODE

37.编写脚本 per.sh,判断当前用户对指定参数文件,是否不可读并且不可写

. /data/scripts38/color.sh
#read -p "Please enter a file:" FILE
[[ $# -lt 1 ]] &&  echo -e "$COLORNo input file$COLOREND" ; exit 10;
[[ ! -e $@ ]] &&  echo -e "$COLORfile does not exist$COLOREND" ; exit 20;
[ ! -r $@ -a ! -w $@ ] &&  echo -e "$COLORThe file is unreadable and not writable$COLOREND" ;
[  -r $@ -a ! -w $@ ] &&  echo -e "$COLORThe file is readable and not writable$COLOREND" ;
[  -r $@ -a  -w $@ ] &&  echo -e "$COLORThe file is readable and  writable$COLOREND" ;

38.编写脚本 excute.sh ,判断参数文件是否为sh后缀的普通文件,如果是,添加所有人可执行权限,否则提示用户非脚本文件

. /data/scripts38/color.sh
[[ $# -lt 1 ]] &&  echo -e "$COLORNo input file$COLOREND" ; exit 10;
[[ ! -e $@ ]] &&  echo -e "$COLORfile does not exist$COLOREND" ; exit 20;
[ ! -f $@ ] &&  echo -e "$COLORNot an ordinary document$COLOREND" ; exit 30;
[[ $@ =~ .sh$ ]] && (chmod a+x $@ ; echo -e "$COLOR$@是一个脚本文件$COLOREND" ) ||  echo -e "$COLORUser Non-script Files$COLOREND" ; exit 40;

39.编写脚本 nologin.sh和 login.sh,实现禁止和允许普通用户登录系统

. /data/scripts38/color.sh
[ -f /etc/nologin ] && echo -e "$COLORNormal users are prohibited from logging in$COLOREND"|| (touch /etc/nologin ; echo -e "$COLORNormal users are prohibited from logging in$COLOREND")
. /data/scripts38/color.sh
[ -f /etc/nologin ] && (rm -f /etc/nologin ; echo -e "$COLOROrdinary users can log in$COLOREND")||echo -e "$COLOROrdinary users can log in$COLOREND"

40.年龄

read -p "Input your age: " AGE
if [[ ! "$AGE" =~ ^[0-9]+$ ]]; then
    echo "Please input a digit age"
elif [ $AGE -lt 18 ];then
    echo "GOOD good study"
elif [ $AGE -lt 80 ];then
    echo "goot goot work"
elif [ $AGE -le 120 ];then
    echo "enjoy life"
else
    echo "Welcome to the earth"
fi

41.鸡兔同笼,头37,脚94

read -p "please input the head number:" HEAD
read -p "please input the foot number:" FOOT
Rabbit=$[FOOT/2-HEAD]
Chook=$[HEAD-Rabbit]
echo "Number of rabbits:" $Rabbit
echo "Number of chook:" $Chook

把ens33改为eth0

[root@centos7 ~]#sed -ri ‘/^[[:space:]]+linux16/s#(.*)#\1 net.ifnames=0#‘ /boot/grub2/grub.cfg
[root@centos7 ~]#sed -ir ‘/GRUB_CMDLINE_LINUX/s@"$@ net.ifnames=0"@‘ /etc/default/grub 

关闭selinux

[root@centos7 data]#sed -i  ‘s/SELINUX=enforcing/SELINUX=disabled/   ‘ /etc/selinux/config

42.

cat <<EOF
1.不知道想干啥
2.干什么都不容易
3.干点什么不好
4.吃饭饭
5.睡觉觉
6.打豆豆
EOF
read -p "抽奖啦啦,输入一个数字(1-6)):" NUM
case $NUM in 
1)
    echo "干什么都不如睡觉"
    ;;
2)
    echo "梦里啥都有,要什么自行车"
    ;;
3) 
    echo "吃饭,睡觉,打豆豆,一天又一天"
    ;;
4) 
    echo "少吃点,该减肥了"
    ;;
5)
    echo "一睡就是一整天"
    ;;
6)
    echo "打什么豆豆"
    ;;
*)
    echo "欢迎来地球"
esac

43.编写脚本createuser.sh,实现如下功能:使??个?户名做为参数,如果指定参数的?户存在,就显?其存在,否则添加之;显?添加的?户的id号等信息

if [ -z "$1" ];then
    read -p "please input user:" USER
    if
        [ -z $USER ] ;then
        echo "Password cannot be empty"
        exit 10;
    else
        NAME=$USER
    fi
else 
    NAME=$USER
fi

if      id $NAME &> /dev/null ; then
    echo "$NAME 账户已经存在"
    exit 20
else
    useradd $NAME && echo "密码创建成功"
    echo magedu | passwd --stdin $NAME  &> /dev/null && echo "初始密码为:magedu"
    passwd -e $NAME &> /dev/null
    echo "userinfo:`id $NAME`"
fi

44.编写脚本/root/bin/yesorno.sh,提??户输?yes或no,并判断?户输?的是yes还是no,或是其它信息

read -p "Do you agree? (yes or no): " ANSWER
case $ANSWER in
[Yy]|[Yy][Ee][sS])
    echo YES
    ;;
[Nn]|[Nn][Oo])
    echo NO
    ;;
*)
    echo input false
esac

45.编写脚本/root/bin/filetype.sh,判断?户输??件路径,显?其?件类型(普通,?录,链接,其它?件类型)

read -p "输入一个文件:" A
if [ -z $A ];then
    echo "输入不能为空"
    exit
elif [ ! -e $A ];then
    echo "文件不存在"
    exit
elif [ -d $A ];then
    echo "这是一个目录"
    exit
elif [ -L $A ];then
    echo "这是一个链接文件"
    exit
elif [ -f $A ];then
    echo "这是一个普通文件"
    exit
else 
    echo "other file"
    exit

fi

46.编写脚本/root/bin/checkint.sh,判断?户输?的参数是否为正整数

 . /data/scripts38/color.sh
read  -t 30 -p "please input a number:" NUM
if [ -z $NUM ] ; then
    echo -e "$COLOR输入不能为空$COLOREND"
    exit 10
elif [[ "$NUM" =~ ^[0-9]+$ ]] ;then
    echo "这是一个正整数"
    exit 20
else
    echo "这不是一个正整数"
    exit 30 
fi

47.让所有?户的PATH环境变量的值多出?个路径,例如:/usr/local/apache/bin

echo ‘PATH=/usr/local/apache/bin:$PATH‘ >> /etc/profile.d/env.sh
source /etc/profile.d/env.sh

48.?户root登录时,将命令指?符变成红?,并?动启?如下别名:
rm=‘rm –i’
cdnet=‘cd /etc/sysconfig/network-scripts/’
editnet=‘vim /etc/sysconfig/network-scripts/ifcfg-eth0’
editnet=‘vim /etc/sysconfig/network-scripts/ifcfg-eno16777736 或
ifcfg-ens33 ’ (如果系统是CentOS7)

echo "PS1=‘\[\e[1;31m\][\u@\h \W]\$\[\e[0m\]‘" >> /etc/profile.d/env.sh
source /etc/profile.d/env.sh
echo "alias rm=‘rm –i‘" >> ~/.bashrc                                      
echo "alias cdnet=‘cd /etc/sysconfig/network-scripts/‘" >> ~/.bashrc
echo "alias editnet1=‘vim /etc/sysconfig/network-scripts/ifcfg-eth0‘" >> ~/.bashrc
echo "alias editnet2=‘vim /etc/sysconfig/network-scripts/ifcfg-ens33‘" >> ~/.bashrc
source .bashrc

49.查找/var?录下属主为root,且属组为mail的所有?件。

[root@centos7 data]#find /var/ -user root -group mail
/var/spool/mail
/var/spool/mail/root

50.查找/var?录下不属于root、 lp、 gdm的所有?件。

[root@centos7 data]#find /var/ -not \( -user root -o -user lp -o -user gdm \)
/var/tmp/abrt
/var/lib/colord
/var/lib/colord/icc

51.查找当前系统上没有属主或属组,且最近?个周内曾被访问过的?件。

[root@centos7 data]#find /var -atime -7 -nouser -o -nogroup

52.查找/var?录下最近?周内其内容修改过,同时属主不root,也不是 postfix的?件。

[root@centos7 data]#find /var -mtime -7 -not \( -user root -o -group postfix \)
/var/log/cups
/var/spool/mail/test
/var/spool/mail/bash
/var/spool/mail/testbash
/var/spool/mail/basher
/var/spool/mail/sh
/var/spool/mail/nologin
/var/spool/mail/wang
/var/spool/mail/mage
/var/spool/mail/lll
/var/spool/mail/aaa
/var/spool/mail/aaaaa
/var/spool/mail/jiaoyu
/var/spool/mail/chen
/var/spool/postfix/active
/var/spool/postfix/incoming
/var/spool/postfix/maildrop
/var/spool/postfix/private
/var/spool/postfix/public

53.查找/etc?录下?于1M且类型为普通?件的所有?件。

[root@centos7 data]#find /etc/ -size +1M -type f
/etc/udev/hwdb.bin
/etc/selinux/targeted/active/policy.kern
/etc/selinux/targeted/contexts/files/file_contexts.bin
/etc/selinux/targeted/policy/policy.31
/etc/brltty/zh-tw.ctb

54.查找/etc?录下所有?户都没有写权限的?件。

[root@centos7 data]#find /etc/ -not -perm /222 
/etc/pki/ca-trust/extracted/java/cacerts
/etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt
/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
/etc/pki/ca-trust/extracted/pem/email-ca-bundle.pem
/etc/pki/ca-trust/extracted/pem/objsign-ca-bundle.pem
/etc/udev/hwdb.bin
/etc/openldap/certs/password
/etc/gshadow
/etc/shadow
/etc/gshadow-
/etc/ld.so.conf.d/kernel-3.10.0-957.el7.x86_64.conf
/etc/lvm/profile/cache-mq.profile
/etc/lvm/profile/cache-smq.profile
/etc/lvm/profile/command_profile_template.profile
/etc/lvm/profile/lvmdbusd.profile
/etc/lvm/profile/metadata_profile_template.profile
/etc/lvm/profile/thin-generic.profile
/etc/lvm/profile/thin-performance.profile
/etc/dbus-1/system.d/cups.conf
/etc/pam.d/cups
/etc/shadow-
/etc/machine-id
/etc/sudoers

55.查找/etc?录下?少有?类?户没有执?权限的?件。

[root@centos7 data]#find /etc/ -not -perm -111

56.查找/etc/init.d?录下,所有?户都有执?权限,且其它用户有写权限的?件。

[root@centos7 data]#find /etc/init.d/ -perm -113

57.私人配置脚本,还是未完成版,还在完善中

export PS1="\[\e[1;32m\][\u@\h \W]\\$\[\e[0m\]"
export PATH=/app/bin:$PATH
export EDITOR=vim
export HISTTIMEFORMAT="%F %T "
export HISTCONTROL=ignoreboth
EOF
cat > ~/.vimrc << EOF
set ignorecase
set cursorline
set autoindent
autocmd BufNewFile *.sh exec ":call SetTitle()"
func SetTitle()
        if expand("%:e") == ‘sh‘
        call setline(1,"#!/bin/bash") 
        call setline(2,"#") 
        call setline(3,"#********************************************************************") 
        call setline(4,"#Author:                zhangfeng") 
        call setline(5,"#QQ:                    553645084") 
        call setline(6,"#Date:                  ".strftime("%Y-%m-%d"))
        call setline(7,"#FileName:             ".expand("%"))
        call setline(8,"#URL:                   https://blog.51cto.com/14451122")
        call setline(9,"#Description:          The test script") 
        call setline(10,"#Copyright (C):        ".strftime("%Y")." All rights reserved")
        call setline(11,"#********************************************************************") 
        call setline(12,"") 
        endif
endfunc
autocmd BufNewFile * normal G
EOF
#关闭selinux
sed -i  ‘s/SELINUX=enforcing/SELINUX=disabled/ ‘ /etc/selinux/config
#把ens33改为eth0
sed -ri ‘/^[[:space:]]+linux16/s#(.*)#\1 net.ifnames=0#‘ /boot/grub2/grub.cfg
#7版本关闭防火墙
systemctl disable firewalld.service
#6版本关闭防火墙
chkconfig iptables off      

以上是关于linux基础正则表达式shell基础文件查找和压缩的主要内容,如果未能解决你的问题,请参考以下文章

shell脚本之正则表达式

linux基础:shell中的正则表达式用法大全

linux基础:shell中的正则表达式用法大全

shell脚本之正则表达式

Linux学习8之Shell编程--基础正则表达式

Linux学习笔记:Shell基础正则表达式