首页 文章 精选 留言 我的

精选列表

搜索[linux],共10000篇文章
优秀的个人博客,低调大师

全新Linux+Python高端运维班第三次作业

1、找出/etc/rc.d/init.d/functions文件中某单词后面跟一组小括号的行,形如:hello(); [root@tom~]#grep-E-o"[_[:alpha:]]+\(\)"/etc/rc.d/init.d/functions fstab_decode_str() checkpid() __readlink() __fgrep() __kill_pids_term_kill_checkpids() __kill_pids_term_kill() __umount_loop() __source_netdevs_fstab() __source_netdevs_mtab() __umount_loopback_loop() __find_mounts() __pids_var_run() __pids_pidof() daemon() killproc() pidfileofproc() pidofproc() status() echo_success() echo_failure() echo_passed() echo_warning() update_boot_stage() success() failure() passed() warning() action() action_silent() strstr() confirm() get_numeric_dev() is_ignored_file() is_true() is_false() apply_sysctl() key_is_random() find_crypto_mount_point() init_crypto() 2、使用echo命令输出一个绝对路径,使用grep取出其基名; 扩展:取出其路径名 [root@tom~]#echo/etc/sysconfig/network-scripts/ifcfg-eth0|grep-E-o"[^/]+$" ifcfg-eth0 #extended# [root@tom~]#echo/etc/sysconfig/network-scripts/ifcfg-eth0|grep-E-o"/.*/" /etc/sysconfig/network-scripts/ 3、找出ifconfig命令结果中的1-255之间数字; [root@tom~]#ifconfig eth0Linkencap:EthernetHWaddr08:00:27:DA:EF:CA inetaddr:172.18.11.121Bcast:172.18.11.127Mask:255.255.255.128 inet6addr:fe80::a00:27ff:feda:efca/64Scope:Link UPBROADCASTRUNNINGMULTICASTMTU:1500Metric:1 RXpackets:21714errors:0dropped:0overruns:0frame:0 TXpackets:915errors:0dropped:0overruns:0carrier:0 collisions:0txqueuelen:1000 RXbytes:1479394(1.4MiB)TXbytes:133705(130.5KiB) loLinkencap:LocalLoopback inetaddr:127.0.0.1Mask:255.0.0.0 inet6addr:::1/128Scope:Host UPLOOPBACKRUNNINGMTU:65536Metric:1 RXpackets:0errors:0dropped:0overruns:0frame:0 TXpackets:0errors:0dropped:0overruns:0carrier:0 collisions:0txqueuelen:0 RXbytes:0(0.0b)TXbytes:0(0.0b) [root@tom~]#ifconfig|egrep-o"\<([1-9]|[0-9]{2}|1[0-9]{2}|2[0-5]{2})\>" 08 00 27 172 18 11 121 172 18 11 127 255 255 255 128 64 1 2 1 224 2 127 1 255 1 128 1 4、查找当前系统上没有属主或属组的文件; 进一步:查找当前系统上没有属主或属组,且最近3天内曾被访问过的文件或目录; [root@tom~]#find/\(-nouser-o-nogroup\)-a-ls|wc-l find:`/proc/21565/task/21565/fd/5':Nosuchfileordirectory find:`/proc/21565/task/21565/fd/5':Nosuchfileordirectory find:`/proc/21565/task/21565/fdinfo/5':Nosuchfileordirectory find:`/proc/21565/task/21565/fdinfo/5':Nosuchfileordirectory find:`/proc/21565/fd/5':Nosuchfileordirectory find:`/proc/21565/fd/5':Nosuchfileordirectory find:`/proc/21565/fdinfo/5':Nosuchfileordirectory find:`/proc/21565/fdinfo/5':Nosuchfileordirectory 1842 #Nextstep# [root@tom~]#find/\(-nouser-o-nogroup\)-a-atime-3-ls|wc-l find:`/proc/21574/task/21574/fd/5':Nosuchfileordirectory find:`/proc/21574/task/21574/fdinfo/5':Nosuchfileordirectory find:`/proc/21574/fd/5':Nosuchfileordirectory find:`/proc/21574/fdinfo/5':Nosuchfileordirectory 214 5、查找/etc目录下大于1M,且类型为普通文件的所有文件; [root@tom~]#find/etc-size+1M-typef-execls-lh{}\; -rw-r--r--.1rootroot8.1MDec1616:43/etc/selinux/targeted/modules/active/policy.kern -rw-r--r--.1rootroot8.1MDec1616:43/etc/selinux/targeted/policy/policy.24 -rw-r--r--.1rootroot2.2MDec1616:10/etc/gconf/gconf.xml.defaults/%gconf-tree.xml 6、查找/etc/init.d/目录下,所有用户都有执行权限,且其它用户有写权限的文件; [root@tom~]#find/etc/init.d/-perm113 [root@tom~]#ls-lh/etc/init.d/ total440K -rwxr-xr-x.1rootroot1.3KMay122016abrt-ccpp -rwxr-xr-x.1rootroot1.6KMay122016abrtd -rwxr-xr-x.1rootroot1.7KMay122016abrt-oops -rwxr-xr-x.1rootroot1.8KFeb172016acpid -rwxr-xr-x.1rootroot2.1KFeb202015atd -rwxr-xr-x.1rootroot3.5KMay112016auditd -rwxr-xr-x.1rootroot4.0KJul122016autofs -r-xr-xr-x.1rootroot1.4KAug2402:37blk-availability -rwxr-xr-x.1rootroot710Nov112010bluetooth -rwxr-xr-x.1rootroot2.1KMay92016certmonger -rwxr-xr-x.1rootroot12KJul242015cpuspeed -rwxr-xr-x.1rootroot2.8KAug2402:36crond -rwxr-xr-x.1rootroot3.0KMay112016cups -rwxr-xr-x.1rootroot1.7KMay112016dnsmasq -rwxr-xr-x.1rootroot3.2KJul92013firstboot -rw-r--r--.1rootroot25KApr122016functions -rwxr-xr-x.1rootroot1.8KOct152014haldaemon -rwxr-xr-x.1rootroot5.9KApr122016halt -rwxr-xr-x.1rootroot2.0KNov1907:49htcacheclean -rwxr-xr-x.1rootroot3.5KNov1907:49httpd -rwxr-xr-x.1rootroot11KJul242015ip6tables -rwxr-xr-x.1rootroot6.6KMay112016ipsec -rwxr-xr-x.1rootroot11KJul242015iptables -rwxr-xr-x.1rootroot1.9KFeb22016irqbalance -rwxr-xr-x.1rootroot21KNov1823:19kdump -rwxr-xr-x.1rootroot652Apr122016killall -r-xr-xr-x.1rootroot2.1KAug2402:37lvm2-lvmetad -r-xr-xr-x.1rootroot3.0KAug2402:37lvm2-monitor -rwxr-xr-x.1rootroot2.6KJun212016mdmonitor -rwxr-xr-x.1rootroot2.2KApr222015messagebus -rwxr-xr-x.1rootroot6.9KMay112016mysqld -rwxr-xr-x.1rootroot3.0KApr122016netconsole -rwxr-xr-x.1rootroot5.2KApr122016netfs -rwxr-xr-x.1rootroot6.3KApr122016network -rwxr-xr-x.1rootroot2.2KMay112016NetworkManager -rwxr-xr-x.1rootroot6.8KOct504:27nfs -rwxr-xr-x.1rootroot3.5KOct504:27nfslock -rwxr-xr-x.1rootroot1.9KMay32016ntpd -rwxr-xr-x.1rootroot2.0KMay32016ntpdate -rwxr-xr-x.1rootroot2.3KMay112016oddjobd -rwxr-xr-x.1rootroot2.5KMay312010openct -rwxr-xr-x.1rootroot2.2KJul242015pcscd -rwxr-xr-x.1rootroot2.0KMay112016portreserve -rwxr-xr-x.1rootroot3.9KNov102015postfix -rwxr-xr-x.1rootroot9.5KNov522:08postgresql-9.2 -rwxr-xr-x.1rootroot1.7KMay112016pppoe-server -rwxr-xr-x.1rootroot1.6KJul172012psacct -rwxr-xr-x.1rootroot2.0KJan72015quota_nld -rwxr-xr-x.1rootroot1.5KNov132015rdisc -rwxr-xr-x.1rootroot1.8KNov1823:30restorecond -rwxr-xr-x.1rootroot1.8KSep222015rngd -rwxr-xr-x.1rootroot2.1KMay112016rpcbind -rwxr-xr-x.1rootroot2.5KOct504:27rpcgssd -rwxr-xr-x.1rootroot2.3KOct504:27rpcidmapd -rwxr-xr-x.1rootroot2.5KOct504:27rpcsvcgssd -rwxr-xr-x.1rootroot2.0KDec102014rsyslog -rwxr-xr-x.1rootroot1.7KNov1823:30sandbox -rwxr-xr-x.1rootroot2.1KFeb272015saslauthd -rwxr-xr-x.1rootroot647Apr122016single -rwxr-xr-x.1rootroot3.0KFeb222013smartd -rwxr-xr-x.1rootroot2.2KAug2402:47snmpd -rwxr-xr-x.1rootroot1.7KAug2402:47snmptrapd -rwxr-xr-x.1rootroot2.5KMay112016spice-vdagentd -rwxr-xr-x.1rootroot4.6KMay122016sshd -rwxr-xr-x.1rootroot2.7KJul122016sssd -rwxr-xr-x.1rootroot1.2KMay112016sysstat -rwxr-xr-x.1rootroot2.3KSep622:51udev-post -rwxr-xr-x.1rootroot16KNov616:42vboxadd -rwxr-xr-x.1rootroot3.0KNov616:43vboxadd-service -rwxr-xr-x.1rootroot21KNov616:43vboxadd-x11 -rwxr-xr-x.1rootroot3.4KMay112016vsftpd -rwxr-xr-x.1rootroot1.7KFeb222013wdaemon -rwxr-xr-x.1rootroot1.6KAug2402:58winbind -rwxr-xr-x.1rootroot1.9KNov1822:26wpa_supplicant -rwxr-xr-x.1rootroot5.0KMay112016ypbind 7、查找/etc目录下最近一周内其内容被修改过,且不属于root或hadoop的文件; [root@tom~]#find/etc-mtime-7-a-not\(-userroot-o-userhadoop\) [root@tom~]# 8、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#; [root@tom~]#cp/etc/rc.d/rc.sysinit/tmp [root@tom~]#ls-lh/tmp total24K drwx------.2rootroot4.0KDec1119:18pulse-0QUeAoK4lJM6 -rwxr-xr-x.1rootroot20KFeb1217:07rc.sysinit [root@tom~]#head-n20/tmp/rc.sysinit #!/bin/bash # #/etc/rc.d/rc.sysinit-runonceatboottime # #TakeninpartfromMiquelvanSmoorenburg'sbcheckrc. # HOSTNAME=$(/bin/hostname) set-m if[-f/etc/sysconfig/network];then ./etc/sysconfig/network fi if[-z"$HOSTNAME"-o"$HOSTNAME"="(none)"];then HOSTNAME=localhost fi if[!-e/proc/mounts];then mount-n-tproc/proc/proc [root@tom~]#vim/tmp/rc.sysinit #在末行模式下输入: :%s@^[[:space:]]\+@#&@g [root@tom~]#head-n20/tmp/rc.sysinit #!/bin/bash # #/etc/rc.d/rc.sysinit-runonceatboottime # #TakeninpartfromMiquelvanSmoorenburg'sbcheckrc. # HOSTNAME=$(/bin/hostname) set-m if[-f/etc/sysconfig/network];then #./etc/sysconfig/network fi if[-z"$HOSTNAME"-o"$HOSTNAME"="(none)"];then #HOSTNAME=localhost fi if[!-e/proc/mounts];then #mount-n-tproc/proc/proc [root@tom~]#head-n20/tmp/rc.sysinit #!/bin/bash # /etc/rc.d/rc.sysinit-runonceatboottime # TakeninpartfromMiquelvanSmoorenburg'sbcheckrc. # HOSTNAME=$(/bin/hostname) set-m if[-f/etc/sysconfig/network];then ./etc/sysconfig/network fi if[-z"$HOSTNAME"-o"$HOSTNAME"="(none)"];then HOSTNAME=localhost fi if[!-e/proc/mounts];then mount-n-tproc/proc/proc 9、删除/tmp/rc.sysinit文件中的以#开头,且后面跟了至少一个空白字符的行行的#和空白字符 [root@tom~]#head-n20/tmp/rc.sysinit #!/bin/bash # #/etc/rc.d/rc.sysinit-runonceatboottime # #TakeninpartfromMiquelvanSmoorenburg'sbcheckrc. # HOSTNAME=$(/bin/hostname) set-m if[-f/etc/sysconfig/network];then #./etc/sysconfig/network fi if[-z"$HOSTNAME"-o"$HOSTNAME"="(none)"];then #HOSTNAME=localhost fi if[!-e/proc/mounts];then #mount-n-tproc/proc/proc [root@tom~]#vim/tmp/rc.sysinit #在末行模式下输入: :%s@^#[[:space:]]\+@@g [root@tom~]#head-n20/tmp/rc.sysinit #!/bin/bash # /etc/rc.d/rc.sysinit-runonceatboottime # TakeninpartfromMiquelvanSmoorenburg'sbcheckrc. # HOSTNAME=$(/bin/hostname) set-m if[-f/etc/sysconfig/network];then ./etc/sysconfig/network fi if[-z"$HOSTNAME"-o"$HOSTNAME"="(none)"];then HOSTNAME=localhost fi if[!-e/proc/mounts];then mount-n-tproc/proc/proc 10、将/etc/yum.repos.d/CentOS-Media.repo文件中所有的enabled=0或gpgcheck=0的最后的0修改为1; [root@tom~]#cat/etc/yum.repos.d/CentOS-Media.repo #CentOS-Media.repo # #ThisrepocanbeusedwithmountedDVDmedia,verifythemountpointfor #CentOS-6.Youcanusethisrepoandyumtoinstallitemsdirectlyoffthe #DVDISOthatwerelease. # #Tousethisrepo,putinyourDVDanduseitwiththeotherrepostoo: #yum--enablerepo=c6-media[command] # #orforONLYthemediarepo,dothis: # #yum--disablerepo=\*--enablerepo=c6-media[command] [c6-media] name=CentOS-$releasever-Media baseurl=file:///media/CentOS/ file:///media/cdrom/ file:///media/cdrecorder/ gpgcheck=1 enabled=0 gpgkey= [root@tom~]#vi/etc/yum.repos.d/CentOS-Media.repo #在末行模式下输入: :%s/\(enabled\|gpgcheck\)=0/\1=1/g [root@tom~]#cat/etc/yum.repos.d/CentOS-Media.repo #CentOS-Media.repo # #ThisrepocanbeusedwithmountedDVDmedia,verifythemountpointfor #CentOS-6.Youcanusethisrepoandyumtoinstallitemsdirectlyoffthe #DVDISOthatwerelease. # #Tousethisrepo,putinyourDVDanduseitwiththeotherrepostoo: #yum--enablerepo=c6-media[command] # #orforONLYthemediarepo,dothis: # #yum--disablerepo=\*--enablerepo=c6-media[command] [c6-media] name=CentOS-$releasever-Media baseurl=file:///media/CentOS/ file:///media/cdrom/ file:///media/cdrecorder/ gpgcheck=1 enabled=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 11、每周2,4,6备份/var/log/messages文件至/backup/messages_logs/目录中,保存的文件名形如messages-20161202 [root@tom~]#crontab-e nocrontabforroot-usinganemptyone crontab:installingnewcrontab [root@tom~]#crontab-l #Backup/var/log/messagesto/backup/messages_logs/ateveryweek2,4,6--sam ****2,4,6cp/var/log/messagesbackup/messages_logs/messages-$(date+%Y%m%D) 12、每天每两小时取当前系统/proc/meminfo文件中的所有以S开头的信息至/stats/memory.txt文件中 [root@tom~]#crontab-e crontab:installingnewcrontab [root@tom~]#crontab-l #Backup/var/log/messagesto/backup/messages_logs/ateveryweek2,4,6--sam ****2,4,6cp/var/log/messagesbackup/messages_logs/messages-$(date+%Y%m%D) #每天每两小时取当前系统/proc/meminfo文件中的所有以S开头的信息至/stats/memory.txt文件中 **/2***cat/proc/meminfo|grep"^S">>/stats/memory.txt 13、写一个脚本创建10用户user10-user19;密码同用户名; [root@tombin]#vimuadd #!/bin/bash #adduser10-user19. for((i=10;i<=19;i++));do iduser$i&>/dev/null&&exit0||useradduser$i&&echo"user$i"|passwd--stdinuser$i done [root@tombin]#ll total4 -rw-r--r--.1rootroot155Feb1218:02uadd [root@tombin]#chmod755uadd [root@tombin]#ll total4 -rwxr-xr-x.1rootroot155Feb1218:02uadd [root@tombin]#uadd Changingpasswordforuseruser10. passwd:allauthenticationtokensupdatedsuccessfully. Changingpasswordforuseruser11. passwd:allauthenticationtokensupdatedsuccessfully. Changingpasswordforuseruser12. passwd:allauthenticationtokensupdatedsuccessfully. Changingpasswordforuseruser13. passwd:allauthenticationtokensupdatedsuccessfully. Changingpasswordforuseruser14. passwd:allauthenticationtokensupdatedsuccessfully. Changingpasswordforuseruser15. passwd:allauthenticationtokensupdatedsuccessfully. Changingpasswordforuseruser16. passwd:allauthenticationtokensupdatedsuccessfully. Changingpasswordforuseruser17. passwd:allauthenticationtokensupdatedsuccessfully. Changingpasswordforuseruser18. passwd:allauthenticationtokensupdatedsuccessfully. Changingpasswordforuseruser19. passwd:allauthenticationtokensupdatedsuccessfully. [root@tombin]#tail/etc/passwd user10:x:3009:3009::/home/user10:/bin/bash user11:x:3010:3010::/home/user11:/bin/bash user12:x:3011:3011::/home/user12:/bin/bash user13:x:3012:3012::/home/user13:/bin/bash user14:x:3013:3013::/home/user14:/bin/bash user15:x:3014:3014::/home/user15:/bin/bash user16:x:3015:3015::/home/user16:/bin/bash user17:x:3016:3016::/home/user17:/bin/bash user18:x:3017:3017::/home/user18:/bin/bash user19:x:3018:3018::/home/user19:/bin/bash

优秀的个人博客,低调大师

Linux高级文本处理之gawk变量的操作符(三)

一、变量 Awk 变量以字母开头,后续字符可以是数字、字母、或下划线。关键字不能用作 awk 变量。awk 变量可以直接使用而不需事先声明。 如果要初始化变量,最好在BEGIN 区域内做,它只会执行一次。Awk 中没有数据类型的概念,一个 awk 变量是 number 还是 string 取决于该变量所处的上下文。 实例1:使用”total”便是用户建立的用来存储公司所有雇员工资总和的变量。 [root@localhost~]#catemp4 101,JohnDoe,CEO,10000 102,JasonSmith,ITManager,5000 103,RajReddy,Sysadmin,4500 104,AnandRam,Developer,4500 105,JaneMiller,SalesManager,3000 [root@localhost~]#catemp.awk BEGIN{ FS=","; total=0; } { print$2"'ssalaryis:"$4; total=total+$4; } END{ print"---\nTotalcompanysalary=$"total; } [root@localhost~]#awk-femp.awkemp4 JohnDoe'ssalaryis:10000 JasonSmith'ssalaryis:5000 RajReddy'ssalaryis:4500 AnandRam'ssalaryis:4500 JaneMiller'ssalaryis:3000 --- Totalcompanysalary=$27000 awk自定义变量的方法: 1.借助-v选项,可以将外部值(并非来自stdin)传递给awk 实例2: [root@localhost~]#awk-vvar="young"'BEGIN{printvar,"\n","---"}{printvar}'./num young --- young young young 2.通过VAR=value的方式定义 实例3: [root@localhost~]#awk'{printv1,v2}'v1="young"v2="geek"./num younggeek younggeek younggeek 二、一元操作符 1.取正取反 只接受单个操作数的操作符叫做一元操作符。 实例1:取反操作 [root@localhost~]#catemp4 101,JohnDoe,CEO,10000 102,JasonSmith,ITManager,5000 103,RajReddy,Sysadmin,4500 104,AnandRam,Developer,4500 105,JaneMiller,SalesManager,3000 [root@localhost~]#awk-F,'{print-$4}'emp4 -10000 -5000 -4500 -4500 -3000 注意:取反只对数值类数据生效,字符串取反结果全部为0. 实例2: [root@localhost~]#catnum -1 -2 -3 [root@localhost~]#awk'{print+$1}'num -1 -2 -3 [root@localhost~]#awk'{print-$1}'num 1 2 3 2.自增自减 VAR1=++VAR或者VAR1=--VAR,表示VAR先增加或者减去1再赋值给VAR1,VAR1=VAR++或VAR1=VAR--,表示先将VAR赋值给VAR1,VAR再增减或者减去1. 实例1:前自加子减 [root@localhost~]#awk-F,'{print++$4}'emp4#前自加 10001 5001 4501 4501 3001 [root@localhost~]#awk-F,'{print--$4}'emp4#前自减 9999 4999 4499 4499 实例2:后自加子减 [root@localhost~]#awk-F,'{$4--;print$4}'emp4#后自减 9999 4999 4499 4499 2999 [root@localhost~]#awk-F,'{$4++;print$4}'emp4#后自加 10001 5001 4501 4501 3001 实例3:打印所有可登陆 shell 的用户总数: [root@localhost~]#awk-F':' >'$NF~/\/bin\/bash/{n++} >END{printn}'/etc/passwd 10 [root@localhost~]#grep-c'/bin/bash$'/etc/passwd 10 实例4: [root@localhost~]#catnum 1 2 1 1 3 4 2 [root@localhost~]#awk'/1/{printNF}'num 1 1 1 [root@localhost~]#awk'/1/{n++}END{printn}'num 3 [root@localhost~]#awk'/2/{n++}END{printn}'num 2 [root@localhost~]#awk'/3/{n++}END{printn}'num 1 [root@localhost~]#awk'/4/{n++}END{printn}'num 1 三、算术运算符 需要两个操作数的操作符,成为二元操作符。 Awk 中有多种基本二元操作符(如算术操作符、 字符串操作符、赋值操作符,等等)。 实例1:将每件单独的商品价格减少 20% 并且将每件单独的商品的数量减少 1 [root@localhost~]#catitems.txt 101,HDCamcorder,Video,210,10 102,Refrigerator,Appliance,850,2 103,MP3Player,Audio,270,15 104,TennisRacket,Sports,190,20 105,LaserPrinter,Office,475,5 [root@localhost~]#catdis.awk BEGIN{ FS=","; OFS=","; discount=0 } { discount=$4*20/100; print$1,$2,$3,$4-discount,$5-1 } [root@localhost~]#awk-fdis.awkitems.txt 101,HDCamcorder,Video,168,9 102,Refrigerator,Appliance,680,1 103,MP3Player,Audio,216,14 104,TennisRacket,Sports,152,19 105,LaserPrinter,Office,380,4 实例2:只打印偶数行 [root@localhost~]#awk'NR%2==0'items.txt 102,Refrigerator,Appliance,850,2 104,TennisRacket,Sports,190,20 四、字符串操作符 (空格)是连接字符串的操作符。 实例1: [root@localhost~]#catstr.awk BEGIN{ FS=","; OFS=","; str1="Audio"; str2="Video"; nustr="100"; str3=str1str2; print"Concatenatestringis:"str3; nustr=nustr+1; print"Strtonu:"nustr; } [root@localhost~]#awk-fstr.awkitems.txt Concatenatestringis:AudioVideo Strtonu:101 四、赋值操作符 实例1: [root@localhost~]#catfz.awk BEGIN{ FS=","; OFS=","; total1=total2=total3=total4=total5=10; total1+=5;printtotal1; total2-=5;printtotal2; total3*=5;printtotal3; total4/=5;printtotal4; total5%=5;printtotal5; } [root@localhost~]#awk-ffz.awkitems.txt 15 5 50 2 0 实例2:打印商品清单 [root@localhost~]#catitems.txt 101,HDCamcorder,Video,210,10 102,Refrigerator,Appliance,850,2 103,MP3Player,Audio,270,15 104,TennisRacket,Sports,190,20 105,LaserPrinter,Office,475,5 [root@localhost~]#awk-F,' >BEGIN{total=0}{total+=$5} >END{print"TotalQutantity:"total}'items.txt TotalQutantity:52 五、比较操作符 实例1:打印数量小于等于临界值 5 的商品信息 [root@localhost~]#awk-F,'$5<=5'items.txt 102,Refrigerator,Appliance,850,2 105,LaserPrinter,Office,475,5 实例2:打印编号为 103 的商品信息 [root@localhost~]#awk-F,'$1==103'items.txt 103,MP3Player,Audio,270,15 实例3:打印除 Video 以外的所有商品 [root@localhost~]#awk-F,'$3!="Video"'items.txt 102,Refrigerator,Appliance,850,2 103,MP3Player,Audio,270,15 104,TennisRacket,Sports,190,20 105,LaserPrinter,Office,475,5 实例4:同实例3,但只打印描述信息 [root@localhost~]#awk-F,'$3!="Video"{print$2}'items.txt Refrigerator MP3Player TennisRacket LaserPrinter 实例5:打印价钱低于 900 或者数量小于等于临界值 5 的商品信息 [root@localhost~]#awk-F,'$5<=5||$4<900'items.txt 101,HDCamcorder,Video,210,10 102,Refrigerator,Appliance,850,2 103,MP3Player,Audio,270,15 104,TennisRacket,Sports,190,20 105,LaserPrinter,Office,475,5 实例6:打印/etc/password 中最大的 UID(以及其所在的整行)。 [root@localhost~]#awk-F':'' >$3>maxuid >{maxuid=$3;maxline=$0} >END{printmaxuid,maxline}'/etc/passwd 1009user3:x:1009:1010::/home/user3:/bin/bash 实例8:打印/etc/passwd 中 UID 和 GROUP ID 相同的用户信息 [root@localhost~]#awk-F':''$3==$4'/etc/passwd root:x:0:0:young,geek,010110110,0101101101:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin dbus:x:81:81:Systemmessagebus:/:/sbin/nologin vcsa:x:69:69:virtualconsolememoryowner:/dev:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin 实例9:打印/etc/passwd 中 UID >= 100 并且用户的 shell 是/bin/sh 的用户 [root@localhost~]#awk-F:'$3>=100&&$7=="/bin/sh"'/etc/passwd user1:x:800:800:testuser:/none:/bin/sh 或者: [root@localhost~]#awk-F':''$3>=100&&$NF~/\/bin\/sh/'/etc/passwd user1:x:800:800:testuser:/none:/bin/sh#正则表达式模式匹配 实例10:打印/etc/passwd 中没有注释信息(第 5 个字段)的用户 [root@localhost~]#awk-F:'$5==""'/etc/passwd abrt:x:173:173::/etc/abrt:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin sys:x:498:1001::/home/sys:/bin/bash natasha:x:1006:1007::/home/natasha:/bin/bash harry:x:1007:1008::/home/harry:/bin/bash sarah:x:497:497::/home/sarah:/bin/nologin 实例11:使用取反(!)运算符打印奇数行 [root@localhost~]#seq10|awk'i=!i' 1 3 5 7 9 实例12:打印偶数行 [root@localhost~]#seq10|awk-vi=1'i=!i' 2 4 6 8 10 六、正则表达式操作符 实例1: [root@localhost~]#catitems.txt 101,HDCamcorder,Video,210,10 102,Refrigerator,Appliance,850,2 103,MP3Player,Audio,270,15 104,TennisRacket,Sports,190,20 105,LaserPrinter,Office,475,5 [root@localhost~]#awk-F,'$2=="Tennis"'items.txt#精确匹配 [root@localhost~]#awk-F,'$2~"Tennis"'items.txt#模糊匹配 104,TennisRacket,Sports,190,20 实例2: [root@localhost~]#awk-F,'$2!~"Tennis"'items.txt#不匹配 101,HDCamcorder,Video,210,10 102,Refrigerator,Appliance,850,2 103,MP3Player,Audio,270,15 105,LaserPrinter,Office,475,5 实例3:打印 shell 为/bin/bash 的用户的总数,如果最后一个字段包含”/bin/bash”,则变量n 增加 1 [root@localhost~]#grep-c'/bin/bash'/etc/passwd 9 [root@localhost~]#awk-F:'$NF~/\/bin\/bash/{n++}END{printn}'/etc/passwd 9 补充说明: awk PATTERN模式的其他形式: empy:空模式,匹配所有行 关系表达式,表达式结果非0为真,则执行后面body中语句;0则为假,不执行。例如 awk-F:'$3>=500{print$1,$3}'/etc/passwd 3.行范围,类似sed或vim中的地址定界: /startpattern/,/endpattern/ 注意:不支持直接给出数字格式 实例: [root@localhost~]#awk'/^root/,/^mail/{print$0}'/etc/passwd root:x:0:0:young,geek,010110110,0101101101:/root:/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

优秀的个人博客,低调大师

Linux 环境下/etc/profile和/etc/profile.d 的区别和用法!

区别: 1. 两个文件都是设置环境变量文件的,/etc/profile是永久性的环境变量,是全局变量,/etc/profile.d/设置所有用户生效 2. /etc/profile.d/比/etc/profile好维护,不想要什么变量直接删除/etc/profile.d/下对应的shell脚本即可,不用像/etc/profile需要改动此文件 用法: CentOS 中每个用户都要指定各自的环境变量,其中包括可执行的 path路径,这些路径决定了每个用户在执行时的命令工具。 一般情况下,可以再每个用户的环境变量里设定各自的 path变量值,然后再执行export PATH使其生效,但如果用户比较多,安装命令工具也原来越多,且出来本身用户可以执行这些工具,root用户或其他用户也可以执行命令,这时在每个用户环境变量里添加就比较复杂了。 所以可以用另外一种方法: 可以再 /etc/profile.d/ 目录下创建一个 path.sh 脚本,脚本内容如下: 例子: 用代码格式表示内容如下: # cat /etc/profile.d/path.sh exportPATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/sbin:/usr/local/nginx/sbin:/usr/local/php/bin:/usr/local/php/sbin:/usr/local/mysql/bin:/usr/local/redis/bin:/usr/local/daemontools/bin:/usr/local/daemontools/sbin:/usr/local/percona-xtrabackup/bin" 可以将需要各个用户需要执行的命令路径都写在该命令中,这样每次操作系统其中后,会自动执行path.sh脚本,将所有的环境变量生效,让各个用户都可以直接执行各自的命令。 以后安装了新软件后,只需要将软件相关路径加入 /etc/profile.d/path.sh 脚本中,不需要再多个地方重复添加,使脚本生效后,各个用户都可以使用了。

优秀的个人博客,低调大师

Linux下Dialog+Shell三层目录专业规范跳板机脚本

#Dialog+Shell介绍 Dialog+Shell结合使用,方能体现出Dialog图形化的效果,在运维的时候,使用图形化界面,能给你一个更直观的感受,相信你会喜欢上他的 #创建脚本目录 [root@localhost~]#mkdir-p/scripts/tiaoban/{qqandroid,qqios,qqwxandroid,wxios,menu_list} #禁止普通用户登陆 [root@localhost~]#cat/etc/profile.d/tiaoban.sh [$UID-ne0]&&sh/scripts/tiaoban/tiaoban.sh #脚本目录结构 [root@localhosttiaoban]#tree/scripts/tiaoban/ /scripts/tiaoban/ ├──menu_list │├──one_menu │└──one_option ├──qqandroid │├──three_directory ││├──ip.txt ││├──three_1_100_tiaoban.sh ││├──three_menu ││└──three_option │├──tow_menu │├──tow_option │└──tow_tiaoban.sh ├──qqios ├──qqwxandroid ├──tiaoban.sh └──wxios 6directories,10files #添加权限 [root@localhosttiaoban]#chmod+x/scripts/tiaoban/menu_list/{one_menu,one_option} [root@localhosttiaoban]#chmod+x/scripts/tiaoban/qqandroid/{tow_menu,tow_option} [root@localhosttiaoban]#chmod+x/scripts/tiaoban/qqandroid/three_directory/{three_menu,three_option} #第一层目录脚本 [root@localhosttiaoban]#cattiaoban.sh #!/bin/bash while: do sttysusp^@#$#屏蔽Crtl+z sttyintr^@$##屏蔽Ctrl+c sttyquit^*#$#屏蔽Ctrl+l LJ=`dirname$0` dialog--title"Menu"--no-cancel--menu"Leveldirectory"12355`cat$LJ/menu_list/one_menu`2>$LJ/menu_list/one_option SELECT=`cat$LJ/menu_list/one_option` if["$SELECT"==1];then sh$LJ/qqandroid/tow_tiaoban.sh elif["$SELECT"==2];then sh$LJ/qqios/tow_tiaoban.sh elif["$SELECT"==3];then sh$LJ/wxandroid/tow_tiaoban.sh else sh$LJ/wxios/tow_tiaoban.sh fi done #第一层目录菜单 [root@localhosttiaoban]#cat/scripts/tiaoban/menu_list/one_menu 1qqandroid 2qqios(功能未开发) 3wxandroid(功能未开发) 4wxios(功能未开发) #第二层目录脚本 [root@localhosttiaoban]#cat/scripts/tiaoban/qqandroid/tow_tiaoban.sh #!/bin/bash while: do LJ=`dirname$0` dialog--title"Menu"--menu"Secondarydirectory"133515`cat$LJ/tow_menu`2>$LJ/tow_option SELECT=`cat$LJ/tow_option` if["$SELECT"==1];then sh$LJ/three_directory/three_1_100_tiaoban.sh elif["$SELECT"==2];then sh$LJ/three_directory/three_101_200_tiaoban.sh elif["$SELECT"-eq3];then sh$LJ/three_directory/three_201_300_tiaoban.sh else sh/scripts/tiaoban/tiaoban.sh fi done #第二层目录菜单 [root@localhosttiaoban]#cat/scripts/tiaoban/qqandroid/tow_menu 1qqandroid1-100服 2qqandroid101-200服(功能未开发) 3qqandroid201-300服(功能未开发) 4返回上一级 #第三层目录脚本 [root@localhosttiaoban]#cat/scripts/tiaoban/qqandroid/three_directory/three_1_100_tiaoban.sh #!/bin/bash while: do LJ=`dirname$0` dialog--title"Menu"--menu"Threedirectory"133515`cat$LJ/three_menu`2>$LJ/three_option SELECT=`cat$LJ/three_option` functionconnect(){ yh=`id` user=`echo$yh|awk-F""'{print$2}'|awk-F"gid="'{print$2}'|awk-F"("'{print$2}'|awk-F")"'{print$1}'` ip=`head-$SELECT$LJ/ip.txt|tail-1` ssh-p22$user@$ip } if["$SELECT"==5];then sh/scripts/tiaoban/qqandroid/tow_tiaoban.sh elif["$SELECT"==6];then sh/scripts/tiaoban/tiaoban.sh else connect fi done 第三层目录菜单 [root@localhosttiaoban]#cat/scripts/tiaoban/qqandroid/three_directory/three_menu 1qq1服 2qqandroid2服(功能未开发) 3qqandroid3服(功能未开发) 4qqandroid...服(功能未开发) 5返回上一级目录 6返回首页 #IP文件展示 [root@localhosttiaoban]#cat/scripts/tiaoban/qqandroid/three_directory/ip.txt 192.168.1.103 192.168.1.104 192.168.1.105 192.168.1.106 192.168.1.107 192.168.1.108 192.168.1.109 192.168.1.110 192.168.1.111 192.168.1.112 #执行效果展示 1)普通用户登陆 2)第一级菜单栏 3)第二级菜单栏 4)第三级菜单栏 5)成功登陆展示 提示:在用普通用户登陆跳板机连接后端服务器的时候,普通用户按Crtl+c和Ctrl+z是没有效果的,所以普通用户只能直接连接到后端的服务器而不能直接登陆到跳板机终端

优秀的个人博客,低调大师

Linux内核设计的艺术:图解Linux操作系统架构设计与实现原理》——1.1 启动BIOS,准备实模式下的中断向量表和中断服务程序

1.1 启动BIOS,准备实模式下的中断向量表和中断服务程序 相信大家都知道一台计算机必须要安装一个所谓“操作系统”的软件,才能让我们使用计算机,否则计算机将是一堆毫无生命力的冰冷的硬家伙。在为计算机安装了操作系统后,当你按下计算机电源按钮的那一刻,计算机机箱传来了嗡嗡的声音。这时你感觉到,计算机开始启动工作了。然而,在计算机的启动过程中,操作系统底层与计算机硬件之间究竟做了哪些复杂的交互动作?下面我们将根据操作系统实际的启动和运行过程对此进行逐步的剖析和讲解。计算机的运行是离不开程序的。然而,加电的一瞬间,计算机的内存中,准确地说是RAM中,空空如也,什么程序也没有。软盘里虽然有操作系统程序,但CPU的逻辑电路被设计为只能运行内存中的程序,没有能力直接从软盘运行操作系统。如果要运行软盘中的操作系统,必须将软盘中的操作系统程序加载到

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册