shell 在练习

1. shell合集

1.shell中常用的基础命令

2.文本处理工具:grep、sed、awk

3.shell脚本的基础知识

4.shell脚本中的变量
需练习!

5.shell的执行流控制

2. 练习

1. cut sort uniq 命令练习

  1. 练习1
显示当前主机中不能登陆的用户(默认不能bash登陆)

grep bash -v /etc/passwd | cut -d : -f 1 
  1. 练习2
1.ifconfig 网卡,可以显示此网卡的信息,显示信息中包含此网卡使用的ip地址,
  请用命令过滤此ip并在输出时之显示ip不显示其他信息。
[root@server mnt]# ifconfig ens3 | head -n 2 | tail -n 1 | cut -c 14-27
172.25.254.121

2.找出能登陆系统用户中UID最大的用户,并显示其名称。
[root@server mnt]# grep bash /etc/passwd | sort  -nr -t : -k 3 | cut -d : -f 1 |head -n 1

3.当前主机为web服务器,请抓取访问web服务器次数排在前5的ip地址。
[root@server mnt]# cat /etc/httpd/logs/access_log | cut -d " " 1 | |sort -nr | uniq -c  | head -n 5 |awk '{print $2}'

2. test 命令脚本练习

2.1 test对于文件的判定

编写脚本文成以下条件:
file_check.sh在执行时,如果脚本后未指定检测文件报错“未指定检测文件,请指定”;如果脚本后指定文件不存在报错“此文件不存在”;当文件存在时请检测文件类型并显示到输出中。

#!/bin/bash
[ -z "$1" ] && {
    
    
	echo "Error: no check file , please input file name !!"
	exit
}

[ -e "$1" ] || {
    
    
	echo "$1" is not exist! 
	exit
}

TYPE=$(ls -l $1 | cut -c 1)

[ "$TYPE" = "l" ] && {
    
    
	echo $1 is flexible connection
	exit
}

[ "$TYPE" = "-" ] && {
    
    
	echo $1 is common file
	exit
}

[ "$TYPE" = "s" ] && {
    
    
	echo $1 is socket 
	exit
}

[ -c $1 ] && {
    
    
	echo $1 is character device
	exit
}

[ -b $1 ] && {
    
    
	echo $1 is block device
	exit
}

[ -d $1 ] && {
    
    
	echo $1 is a directory
	exit
}

在这里插入图片描述

2.2 test对于用户的判断

编写脚本完成以下条件:
user_check.sh在执行时,如果脚本后未指定检测用户报错“未指定用户,请指定”;如果脚本后指定用户不存在报错“该用户不存在”;当用户存在时:区分用户类型并显示到输出中。

#!/bin/bash
[ -z $1 ] && {
    
    
	echo "Error: Please input username !!"
	exit
}

id $1 &> /dev/null || {
    
    
	echo "user $1 is not exist !! "
	exit
}

UUID=$(id -u $1)
SSHELL=$( grep $1 /etc/passwd | cut -d : -f 7 )

[ "$UUID" -eq "0" ] && {
    
    
	echo $1 is supper user
	exit
} 

[ "$UUID" -lt "1000" ] && [ ! "$SSHELL" = "/bin/bash" ] && {
    
    
	echo $1 is system user
	exit
}

[ "$UUID" -ge "1000" ] && [ "$SSHELL" = "/bin/bash" ] && {
    
    
	echo $1 is common user
	exit
}

在这里插入图片描述

3.显示当前的主机ip

ip_show.sh 网卡,显示当前的主机ip

#!/bin/bash
1.判断执行脚本后是否跟网卡
[ -z "$1" ] && {
    
    
        echo "ERROR: please input the interface name!"
        exit
}

2.查询ip后丢弃,如果不存在显示网卡没有找到
ifconfig $1 &> /dev/null || {
    
    
        echo "ERROR: this interface $1 is not found!"
        exit
}

3.如果存在,获取网卡的ip
ifconfig $1 | head -n 2 | tail -n 1 | awk '{print $2}'

4. 显示当前主机的名称、ip、用户

host_messages.sh 显示当前主机的名称、ip、登陆当前主机的用户

#!/bin/bash
echo "hostname: `hostname`"
echo "ipaddress:`ifconfig $1 | head -n 2 | tail -n 1 | awk '{print $2}'`"
echo "username: `whoami`"

5. 执行此脚本后可以清空日志

clear_log.sh 执行此脚本后可以清空日志

注意:日志在 /etc/rsyslog.conf

#!/bin/bash
1.只有超级用户才能执行此脚本
[ "`whoami`" = "root" ] || {
        echo "Error: This script must run as root"
        exit
}

2.日志截取执行清空
for Log_Name in `awk '/./&&!/^#/&&!/"/&&$2~/^\/|^\-/{
    
    print $2}' /etc/rsyslog.conf | sed  's/-//g'`
do
        > $Log_Name && {
    
    
                echo $Log_Name is cleared.
        }
done
#!/bin/bash
[ "$USER" = "root" ] || {
    
    
        echo -e "Error: Please run this script with root !!"
        exit 1
}

LOGS=`sed -n '/RULES/,$P' /etc/rsyslog.conf | awk '!/^#/&&/var/{print $2}' | sed 's/-//g'`
截取/etc/rsyslog.onf中RULES至最行一行,获取不以#开头并且含有/var/的第二列,将-换为空格

for LOG in $LOGS
do
        > $LOG && echo -e "$LOG is cleared!!"
done

在这里插入图片描述

6. 脚本Apache_port.sh后接入数字

 - 判断是否安装了apache,没有则进行安装
 - 判断脚本后是否跟了数字,没有就进行报错
 - 当脚本后不为空,判断端口是否正在被使用
 - 替换配置文件中的端口

详细练习与解释

7. check_host.sh检测网络是否通常

用此脚本检测10台与您当前主机直连主机是否网络通常
如果网络通常请显示主机的ip列表

#!/bin/bash
for n in `seq 1 254`
do
    IP="192.168.0.${n[@]}"
    ping -c1 -w1 $IP &>/dev/null && {
    
    
    echo $IP
}
done
#!/bin/bash
for ((IP=1;IP<11;IP++))
do
	ping -c1 -w1 172.25.254.$IP &> /dev/null && echo 172.25.254.$IP
done

8. 建立文件create_user.sh、userfile、 passfile

使userfile中的用户会被全部建立 passfile中的密码会被全部设定

#!/bin/bash
USERNAME()
{
    
    
 	read -p "Please input the username: " username
}
USERADD()
{
    
    
	USERNAME
	useradd $1
	read -p "Please input the password: " -s password
	echo $2 | passwd --stdin $1
	echo "$1 created successfully !!"
}

[ -z "$1" ] && {
    
    
	echo "	Error: no check username,please input username !!"
	exit
}

grep $1 /etc/passwd &> /dev/null && {
    
    
	echo "$1 is already existed!"
	USERNAME
	USERADD
} || {
    
    
	USERNAME
	USERADD
}

在这里插入图片描述

9. 编写一个check_file.sh判断文件类型

check_file.sh
please input filename:file
file is not exist
file is file
file is directory
此脚本会一直询问直到用户输入exit

#!/bin/bash
while true
do
read -p "please input filename: " file
if [ "$file" = "exit" ]
then	
	echo bye
	exit
elif [ ! -e "$file" ]
then
	echo $file is not exist
elif [ -f "$file" ]
then
	echo $file is a common file
elif [ -d "$file" ]
then 
	echo $file is a directory
fi
done	

10. system_watch.sh监控使用情况

system_watch.sh disk memory upload(每秒显示)
disk 监控磁盘使用情况
memory 监控内存使用情况
upload 监控启动负载

#!/bin/bash
read -p "please input word: " word
case $word in 
	disk)
	watch -n 1 "df -h"
	;;
	memory)
	watch -n 1 "free -h"
	;;
	upload)
	top
	;;
	*)
	echo noooooo
esac

11. host_list.sh检测网络是否开启

host_list.sh:
检测172.25.254.1-172.25.254.10网络是否开启
如果网络正常请生成解析列表hosts_list

格式如下:
ip 主机名称(eg:172.25.254.1为开启状态主机名为server1)

hosts_list中:
172.25.254.1 server1
#!/bin/bash
AUTO_SSH()
{
    
    
/usr/bin/expect <<EOF
spawn ssh root@$1 hostname
expect {
    
    
"yes/no" {
    
     send "yes\r" ;exp_continue }
"password:" {
    
     send "$2\r" }
}
expect eof
EOF
}

for Host_Num in {
    
    1..10}
do
	ping -c1 -w1 172.25.254.$Host_Num &> /dev/null
	if [ "$?" = "0" ]
	then 
		Host_Name=`AUTO_SSH 172.25.254.$Host_Num westos | tail -n 1`
		grep $Host_Name /mnt/hosts_list &> /dev/null || {
    
    
			echo "172.25.254.$Host_Num $Host_Name" >> /mnt/hosts_list
		}
	fi
done

sed 's/^M//g' -i /mnt/hosts_list
(^M:ctrl+v ctrl+m)

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_46074899/article/details/111180597