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

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

概述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 " linux基础正则表达式、shell基础、文件查找和压缩

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

color="3[1;36m"colorEND="3[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地址

[[email protected] scripts38]#ifconfig ens33 |grep netmask|tr -s ‘ ‘|cut -d‘ ‘ -f3192.168.11.131
[[email protected] ~]#ifconfig ens33 |grep ask | grep -o "\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}"|head -1192.168.6.136

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

[[email protected] scripts38]#df|grep ‘/dev/sd‘|tr -s ‘ ‘|cut -d‘ ‘ -f5|tr -d %|sort -nr|head -n 117

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

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

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

[[email protected] ~]#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开头的行(要求:使用两种方法)

[[email protected] ~]#grep ^[sS]    /proc/meminfo SwapCached:         2288 kBSwapTotal:       2097148 kBSwapFree:        2058044 kBShmem:             16796 kBSlab:              88744 kBSReclaimable:      39980 kBSUnreclaim:        48764 kB
[[email protected] ~]#grep ‘^s\|^S‘    /proc/meminfo SwapCached:         2288 kBSwapTotal:       2097148 kBSwapFree:        2058044 kBShmem:             16796 kBSlab:              88744 kBSReclaimable:      39980 kBSUnreclaim:        48764 kB
[[email protected] ~]#grep ^[s\|S]    /proc/meminfo SwapCached:         2288 kBSwapTotal:       2097148 kBSwapFree:        2058044 kBShmem:             16796 kBSlab:              88744 kBSReclaimable:      39980 kBSUnreclaim:        48764 kB
[[email protected] ~]#grep -w \<‘s\|S.*‘ /proc/meminfo SwapCached:         2288 kBSwapTotal:       2097148 kBSwapFree:        2058044 kBShmem:             16796 kBSlab:              88716 kBSReclaimable:      39980 kBSUnreclaim:        48736 kB

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

[[email protected] ~]#cat /etc/passwd |grep -v  ‘/bin/bash$‘bin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologin

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

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

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

[[email protected] ~]#cat /etc/passwd |grep   "\<[0-9]\{2,3\}\>"mail:x:8:12:mail:/var/spool/mail:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/sbin/nologinnobody:x:99:99:Nobody:/:/sbin/nologinsystemd-network:x:192:192:systemd Network Management:/:/sbin/nologindbus:x:81:81:System message bus:/:/sbin/nologinpolkitd:x:999:998:User for polkitd:/:/sbin/nologinlibstoragemgmt:x:998:995:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologincolord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin

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

[[email protected] ~]#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‘  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后跟任意多个空白字符结尾的行

[[email protected] ~]#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

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

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

[[email protected] ~]#grep ‘\(.*\+\):.*\<$‘  /etc/passwdsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltnologin:x:1026:1032::/home/nologin:/sbin/nologin
[[email protected] ~]#egrep "(.*+):.*\<$" /etc/passwdsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltnologin:x:1026:1032::/home/nologin:/sbin/nologin

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

[[email protected] ~]#df |grep ‘/dev/sd‘|tr -s ‘ ‘|cut -d‘ ‘ -f5 |sort -nr17%5%1%
[[email protected] ~]#df |grep ‘/dev/sd‘|grep -o ‘[0-9]\{0,3\}%‘ |sort -nr 17%5%1%

16.查看linux大版本。

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

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

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

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

[[email protected] 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中其基名和?录名

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

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

[[email protected] data]#last |grep ‘root‘   | egrep -o \([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地址

[[email protected] 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.136255.255.255.0192.168.6.255172.18.7.7255.255.0.0172.18.255.255127.0.0.1255.0.0.0192.168.122.1255.255.255.0192.168.122.255

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

[[email protected] 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="3[1;36m"colorEND="3[0m"echo -e "${color}Starting backup ...$colorEND"cp -av /etc/   /data/etc_backup-`date +%F`echo -e "${color}Backup is finished$colorEND"

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

color="3[1;36m"colorEND="3[0m"echo -e "${color}Starting Hard disk inspection ...$colorEND"                              echo -e "${color}`df|grep "/dev/sd" |tr -s " "|cut -d ‘ ‘ -f5|sort -nr|head -1`$colorEND"echo -e "${color}Hard disk inspection is finished$colorEND"

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

color="3[1;36m"colorEND="3[0m"echo -e "${color}linsk ...$colorEND"                              echo -e "${color}` netstat -tun|grep ESTAB|tr -s " " : |cut -d: -f6|sort -nr|uniq -c`$colorEND"echo -e "${color}linsk$colorEND"

27.通用版信息存放地点

[[email protected] data]#cat /proc/partitions硬盘大小存放位置[[email protected] data]#cat /proc/meminfo内存大小存放位置[[email protected] data]#cat /proc/cpuinfocpu等存放位置

28.远程执行脚本。

先开启服务,6上面自带服务[@L_419_30@ ~]#service httpd startStarting httpd: httpd: apr_sockaddr_info_get() Failed for centos6.10.localdomainhttpd: Could not reliably determine the server‘s fully qualifIEd domain name,using 127.0.0.1 for Servername                                                           [  OK  ]
把脚本拷贝到6的主机上[[email protected] 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)? yesWarning: Permanently added ‘192.168.6.129‘ (RSA) to the List of kNown hosts.[email protected]‘s password: 2systeminfo.sh                            100%  964   581.7KB/s   00:00
查看是否拷贝到了6的主机上[[email protected] ~]#ls /var/www/HTML/2systeminfo.sh
在其他主机上执行就可以实现了[[email protected] 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”命令的结果中的?件路径

[[email protected] 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?件中的??的空?字符

[[email protected] data]#cp /etc/profile /tmp/[[email protected] data]#vim /tmp/profile :%s/^[[:space:]]\+//g

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

[[email protected] data]#cp /etc/rc.d/init.d/functions /tmp/[[email protected] 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 ‘^$‘  |wc -l`NUM2=` grep ‘^$‘  |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 ‘^$‘  |wc -l`echo "The  blank lines =$NUM1"

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

. /data/scripts38/color.shread -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 "${color}No input file${colorEND}" ; exit 10;}[[ ! -e [email protected] ]] && { echo -e "${color}file does not exist${colorEND}" ; exit 20;}[ ! -r [email protected] -a ! -w [email protected] ] && { echo -e "${color}The file is unreadable and not writable${colorEND}" ;}[  -r [email protected] -a ! -w @L_301_44@ ] && { echo -e "${color}The file is readable and not writable${colorEND}" ;}[  -r [email protected] -a  -w [email protected] ] && { echo -e "${color}The file is readable and  writable${colorEND}" ;}

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

. /data/scripts38/color.sh[[ $# -lt 1 ]] && { echo -e "${color}No input file${colorEND}" ; exit 10;}[[ ! -e [email protected] ]] && { echo -e "${color}file does not exist${colorEND}" ; exit 20;}[ ! -f [email protected] ] && { echo -e "${color}Not an ordinary document${colorEND}" ; exit 30;}[[ [email protected] =~ .sh$ ]] && (chmod a+x [email protected] ; echo -e "${color}[email protected]是一个脚本文件${colorEND}" ) || { echo -e "${color}User Non-script files${colorEND}" ; exit 40;}

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

. /data/scripts38/color.sh[ -f /etc/nologin ] && echo -e "${color}normal users are prohibited from logging in${colorEND}"|| (touch /etc/nologin ; echo -e "${color}normal users are prohibited from logging in${colorEND}")
. /data/scripts38/color.sh[ -f /etc/nologin ] && (rm -f /etc/nologin ; echo -e "${color}Ordinary users can log in${colorEND}")||echo -e "${color}Ordinary users can log in${colorEND}"

40.年龄

read -p "input your age: " AGEif [[ ! "$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:" headread -p "please input the foot number:" FOOTRabbit=$[FOOT/2-head]Chook=$[head-Rabbit]echo "Number of rabbits:" $Rabbitecho "Number of chook:" $Chook

把ens33改为eth0

[[email protected] ~]#sed -ri ‘/^[[:space:]]+linux16/s#(.*)# net.ifnames=0#‘ /boot/grub2/grub.cfg
[[email protected] ~]#sed -ir ‘/GRUB_CMDliNE_liNUX/[email protected]"[email protected] net.ifnames=0"@‘ /etc/default/grub

关闭selinux

[[email protected] data]#sed -i  ‘s/SEliNUX=enforcing/SEliNUX=Disabled/   ‘ /etc/selinux/config

42.

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

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

if [ -z "" ];then    read -p "please input user:" USER    if        [ -z $USER ] ;then        echo "Password cannot be empty"        exit 10;    else        name=$USER    fielse     name=$USERfiif      ID $name &> /dev/null ; then    echo "$name 账户已经存在"    exit 20else    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): " ANSWERcase $ANSWER in[Yy]|[Yy][Ee][sS])    echo YES    ;;[Nn]|[Nn][Oo])    echo NO    ;;*)    echo input falseesac

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

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

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

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

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

echo ‘PATH=/usr/local/apache/bin:$PATH‘ >> /etc/profile.d/env.shsource /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\][\[email protected]\h \W]$\[\e[0m\]‘" >> /etc/profile.d/env.shsource /etc/profile.d/env.shecho "alias rm=‘rm –i‘" >> ~/.bashrc                                      echo "alias cdnet=‘cd /etc/sysconfig/network-scripts/‘" >> ~/.bashrcecho "alias editnet1=‘vim /etc/sysconfig/network-scripts/ifcfg-eth0‘" >> ~/.bashrcecho "alias editnet2=‘vim /etc/sysconfig/network-scripts/ifcfg-ens33‘" >> ~/.bashrcsource .bashrc

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

[[email protected] data]#find /var/ -user root -group mail/var/spool/mail/var/spool/mail/root

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

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

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

[[email protected] data]#find /var -atime -7 -nouser -o -nogroup

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

[[email protected] 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且类型为普通?件的所有?件。

[[email protected] 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?录下所有?户都没有写权限的?件。

[[email protected] 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?录下?少有?类?户没有执?权限的?件。

[[email protected] data]#find /etc/ -not -perm -111

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

[[email protected] data]#find /etc/init.d/ -perm -113
总结

以上是内存溢出为你收集整理的linux基础正则表达式、shell基础、文件查找和压缩全部内容,希望文章能够帮你解决linux基础正则表达式、shell基础、文件查找和压缩所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/1020121.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存