shell linux 缩进,Linux 阶段练习(2)

67、显示CentOS7上所有系统用户的用户名和UID

# cat /etc/passwd |grep '.*:x:\.*'

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

# cat /etc/passwd |grep -E '^(.*):x.*/\1$'       后向引用的\1$ 已经可以确定^(.*)的字符个数了

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

# df |egrep -o '1?[0-9]?[0-9]%'|sort -nr

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

# cat /etc/passwd |grep -E '^(root|wang|mage)' |cut -d: -f3,7

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

# cat /etc/rc.d/init.d/functions |grep -E '^([[:alpha:]]+|_+)[a-zA-Z0-9_]*\('

72、使用egrep取出/etc/rc.d/init.d/functions中其基名

# echo /etc/rc.d/init.d/functions |grep -Eo  '[[:alpha:]]+$'  (注意-v 是选择非匹配的行,不能在匹配的相同行中取非)

73、使用egrep取出上面路径的目录名

# echo /etc/rc.d/init.d/functions |grep -Eo '.*/'

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

# last |grep -Eo '(([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])'|sort |uniq -c

75、利用扩展正则表达式分别表示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]

76、显示ifconfig命令结果中所有IPv4地址

# ifconfig | grep -Eo '(([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])'

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

# echo "welcome to magedu linux" |grep -o '.'| grep -o '[[:alpha:]]' |sort |uniq -c|sort -nr

78、复制/etc/profile至/tmp/目录,用查找替换命令删除/tmp/profile文件中的行首的空白字符

# :%s/^ +// (:%s/^[[:space:]]+//)

79、复制/etc/rc.d/init.d/functions文件至/tmp目录,用查找替换命令为/tmp/functions的每行开头为空白字符的行的行首添加一个#号

# :%s/^[[:space:]]/#&

80、在vim中设置tab缩进为4个字符

# set ts=4

81、复制/etc/rc.d/init.d/functions文件至/tmp目录,替换/tmp/functions文件中的/etc/sysconfig/init为/var/log

# :%s@/etc/sysconfig/init@/var/log@

82、删除/tmp/functions文件中所有以#开头,且#后面至少有一个空白字符的行的行首的#号

# :%s/^#\( \+\)/\1/

83、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小

#!/bin/bash

echo -e "\e[1;31mThe hostname:\e[0m  `hostname`"

echo -e "\e[1;31mThe System IP address:\e[0m  `ifconfig ens33|head -2 |tail -1|tr -s ' '|cut -d' ' -f3`"

echo -e "\e[1;31mCurrent System Version:\e[0m  ` cat /etc/centos-release`"

echo -e "\e[1;31mKernel Version:\e[0m  `uname -r`"

echo -e "\e[1;31mThe Mode of CPU:\e[0m  `lscpu |grep 'Model name'|tr -s ' '|cut -d: -f2`"

echo -e "\e[1;31mThe total space of Memory:\e[0m  `cat /proc/meminfo |grep 'MemTotal'|tr -s ' '|cut -d: -f2`"

echo -e "\e[1;31mThe total space of disk:\e[0m ` lsblk |grep -w "sda"|tr -s ' ' ':'|cut -d: -f5`"

84、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到/root/etcYYYY-mm-dd中

#!/bin/bash

echo -e "\e[1;31m Backup is starting... \e[0m"

[ -d /root/etc`date +%F` ] && { echo "Backup dir has been exist"; read -p "Do you want to overwrite? Yes or No: " key; }

if [[ $key =~ [Yy]+([Ee][Ss])? ]];then

rm -rf /root/etc`date +%F`

cp -av /etc /root/etc`date +%F`

elif [[ $key =~ [Nn][Oo] ]]; then

ran_num=$RANDOM

read -p "Backup name can be added a random number $ran_num, Yes or No: " key1

if [[ $key1 =~ [Yy][Ee][Ss] ]]; then

cp -av /etc /root/etc`date +%F`$ran_num

echo "Backup with a name has been finished."

else

exit

fi

fi

cp -av /etc /root/etc`date +%F`

echo -e "\e[1;31m Backup has been finished.\e[0m"

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

#!/bin/bash

echo -e "\e[1;31mThe the biggest partition usage : \e[0m`df |grep '/dev/sd\+' |tr -s ' ' ':'|cut -d: -f5 |sort -nr |head -1` "

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

#!/bin/bash

netstat -nt |grep 'ESTABLISHED'|tr -s ' ' ':' |cut -d: -f6|sort |uniq -c | sort -nr

87、编写脚本/root/bin/sumid.sh,计算/etc/passwd文件中的第10个用户和第20用户的ID之和

#!/bin/bash

uid10=`cat /etc/passwd| head -10|tail -1|cut -d: -f3`

uid20=`cat /etc/passwd| head -20|tail -1|cut -d: -f3`

echo "Sum=$[uid10+uid20]"

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

#!/bin/bash

file1=$1

file2=$2

file1_blanks=`grep '^[[:space:]]*$' ${file1} |wc -l`

file2_blanks=`grep '^[[:space:]]*$' ${file2} |wc -l`

echo -e "\e[1;31mTotal blanks:\e[0m $[file1_blanks+file2_blanks]"

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

#!/bin/bash

etc_dirs=`ls -Al /etc/ |grep '^d'|wc -l`

usr_dirs=`ls -Al /usr/ |grep '^d'|wc -l`

var_dirs=`ls -Al /var/ |grep '^d'|wc -l`

etc_files=`ls -Al /etc/ |grep '^-'|wc -l`

usr_files=`ls -Al /usr/ |grep '^-'|wc -l`

var_files=`ls -Al /var/ |grep '^-'|wc -l`

echo "etc_dirs=$etc_dirs"

echo "usr_dirs=$usr_dirs"

echo "var_dirs=$var_dirs"

echo "etc_files=$etc_files"

echo "usr_files=$usr_files"

echo "var_files=$var_files"

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

#!/bin/bash

[ "$#" -lt 1 ] && echo "Need a parameter" && exit

[ "$#" -ge 1 ] && echo "Total blanks:`grep '^[[:space:]]*$' $1|wc -l`"

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

#!/bin/bash

ping -c 1 $1 &>/dev/null && echo "Host up" || echo "Host down"

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

#!/bin/bash

inode_usage=`df -i|tr -s ' ' ':' |cut -d: -f5 |sort -nr |head -1 |grep -Eo '[[:digit:]]{1,3}'`

partition_usag=`df |grep '/dev/sda*'|tr -s ' ' ':'|cut -d: -f5|sort -nr |head -1|grep -Eo '[[:digit:]]{1,3}'`

[ $inode_usage -gt 80 -o $partition_usag -gt 80 ] && `wall "warning..."`

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

#!/bin/bash

[ ! -r $1 -a  ! -w $1 ] && echo "The file can't read and write" || echo "The file can read an write"

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

#!/bin/bash

[[ $1 =~ ..*\.sh$ ]] && { echo ".sh"; chmod +x $1; } || echo "no .sh"

95、编写脚本/root/bin/nologin.sh和login.sh,实现禁止和充许普通用户登录系统

#!/bin/bash

users=`cat /etc/passwd|grep -Ew '[[:digit:]]{4}' |cut -d: -f1`

for i in $users; do

usermod -s /sbin/nologin $i

done

#!/bin/bash

users=`cat /etc/passwd|grep -Ew '[[:digit:]]{4}' |cut -d: -f1`

for i in $users; do

usermod -s /bin/bash $i

done

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值