1.更改http服务的端口
编写一个脚本
#!/bin/bash
[ -z "$1" ] &&{
echo -e "\033[31mError: Please input port number for Apache Server\033[0m"
exit
}
sed "/^Listen/cListen $1" -i /etc/httpd/conf/httpd.conf
systemctl restart httpd &> /dev/null
Apache_port=`netstat -antllupe | grep httpd | awk '{print $4}' | cut -d : -f 4`
[ "$1" = "$Apache_port" ] && {
echo -e "\033[32mPort has change to $1 \033[0m"
}||{
echo -e "\033[31mError: Change port failed !!\033[0m"
}
测试:
[root@shell_example mnt]# sh change_port.sh
Error: Please input port number for Apache Server
[root@shell_example mnt]# sh change_port.sh haha
Error: Change port failed !!
[root@shell_example mnt]# sh change_port.sh 8080
Port has change to 8080
##检查端口号与设置的是否相同
[root@shell_example mnt]# netstat -antlupe | grep httpd
tcp6 0 0 :::8080 :::* LISTEN 0 50373 3305/httpd
[root@shell_example mnt]# netstat -antlupe | grep httpd | awk '{print $4}'
:::8080
[root@shell_example mnt]# netstat -antlupe | grep httpd | awk '{print $4}' | cut -d : -f 4
8080
##试验后还原设置
[root@shell_example mnt]# sh change_port.sh 80
Port has change to 80
2.编写一个脚本,使其可以读取文件user_file中的用户和pass_file中的密码,将没有创建的用户进行创建,密码进行修改,如果用户存在,则显示用户已经存在
(1)建立一个user_file文件,在其中写入三个用户
建立一个pass_file文件,写入对应的密码
[root@shell_example mnt]# vim user_file
[root@shell_example mnt]# cat user_file
user1
user2
user3
[root@shell_example mnt]# vim pass_file
[root@shell_example mnt]# cat pass_file
123
123
123
(2)编写满足条件的脚本文件
#!/bin/bash
Max_Line=`sed -n '$=' $1`
for i in $(seq 1 $Max_Line)
do
USERNAME=`sed -n "${i}p" $1`
PASSWORD=`sed -n "${i}p" $2`
id $USERNAME &> /dev/null &&{
echo "$USERNAME is exist"
}||{
useradd $USERNAME
echo $PASSWORD | passwd --stdin $USERNAME &> /dev/null && echo $USERNAME created
}
done
测试:
第一次执行脚本,三个用户都创建成功
[root@shell_example mnt]# vim user_create.sh
[root@shell_example mnt]# sh user_create.sh user_file pass_file
user1 created
user2 created
user3 created
第二次执行脚本,提示这三个用户都存在
[root@shell_example mnt]# sh user_create.sh user_file pass_file
user1 is exist
user2 is exist
user3 is exist
删除一个用户之后再次执行脚本,刚刚删除的用户创建成功,其他两个用户已存在
[root@shell_example mnt]# userdel -r user1
[root@shell_example mnt]# sh user_create.sh user_file pass_file
user1 created
user2 is exist
user3 is exist
注:这里的$1(第一个字符串)代表的是第一个文件(会依次执行文件中每一行的内容)
3.编写一个脚本,将/etc/下所有以.conf结尾的文件都复制到/mnt/backup下,并且将文件名都改成.conf加当前时间
#!/bin/bash
mkdir /mnt/backup
find /etc/ -name "*.conf" -exec cp -rp {} /mnt/backup \;
find /mnt/backup -name "*.conf" -exec mv {} {}.`date +%m-%d` \;
测试:
[root@shell_example mnt]# vim cpfile.sh
[root@shell_example mnt]# sh cpfile.sh
[root@shell_example mnt]# ls
answer.sh casetest.sh filetype.sh ipfile test.exp user_create.sh
ask.sh change_port.sh ifcountdown.sh jishu.sh test.sh user_file
backup countdown.sh iftest.sh numtest.sh time_count1.sh westos
calculator.sh cpfile.sh ip_check.sh pass_file time_count.sh whiletest.sh
[root@shell_example mnt]# cd backup
[root@shell_example backup]# ls
4.编写一个脚本,当根的负载大于80%时出现报错
#!/bin/bash
Disk_Used=$(df -h / | grep / | awk -F " " '{print $5}' | cut -d % -f 1)
ECHO_DEV=`ps | grep $$ | awk -F " " '{print $2}'`
[ "$Disk_Used" -ge "80" ] && {
echo Warning: System root will full,please fix this warning !! > /dev/$ECHO_DEV
}
echo "* * * * * root /mnt/warning.sh" > /etc/cron.d/warning
测试:
(1)划分出两个大文件占用内存
[root@shell_example mnt]# df -h
[root@shell_example mnt]# dd if=/dev/zero of=/mnt/bigfile1 bs=1M count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 10.9159 s, 96.1 MB/s
[root@shell_example mnt]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 10G 4.4G 5.7G 44% /
[root@shell_example mnt]# dd if=/dev/zero of=/mnt/bigfile2 bs=1M count=4000
4000+0 records in
4000+0 records out
4194304000 bytes (4.2 GB) copied, 82.6707 s, 50.7 MB/s
[root@shell_example mnt]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 10G 8.3G 1.8G 83% /
(2)此时/的负载大于80%,执行脚本
[root@shell_example mnt]# sh warning.sh
Warning: System root will full,please fix this warning !!
关于脚本中内容的解释:
1>df -h /是查看/的占用率
[root@shell_example mnt]# df -h /
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 10G 8.3G 1.8G 83% /
2>可以看到上一步中对我们有用的信息只有最后一行,也就是/的信息那行,所以用grep /将他过滤出来
[root@shell_example mnt]# df -h / | grep /
/dev/vda1 10G 8.3G 1.8G 83% /
3>截取上一步中一空格为分隔符的第五个字符串,也就是占用率
[root@shell_example mnt]# df -h / | grep / | awk -F " " '{print $5}'
83%
4>去掉占用率中的%,用百分号前的数字与80作比较
[root@shell_example mnt]# df -h / | grep / | awk -F " " '{print $5}' | cut -d % -f 1
83
5>ps 查看进程
[root@shell_example mnt]# ps
PID TTY TIME CMD
2180 pts/0 00:00:00 bash
12907 pts/0 00:00:00 ps
You have new mail in /var/spool/mail/root
6>grep $$表示过滤出shell本身的PID
[root@shell_example mnt]# ps | grep $$
2180 pts/0 00:00:00 bash
7>打印上一步中以空格为分隔符的第二串字符
[root@shell_example mnt]# ps | grep $$ | awk -F " " '{print $2}'
pts/0
脚本中的内容还有第二种写法
#!/bin/bash
Disk_Used=$(df -h / | grep / | awk -F " " '{print $5}' | cut -d % -f 1)
[ "$Disk_Used" -ge "80" ] && {
echo Warning: System root will full,please fix this warning !! >> /var/log/messages
}
echo "* * * * * root /mnt/warning.sh" > /etc/cron.d/warning
测试:
[root@shell_example mnt]# vim warning.sh
[root@shell_example mnt]# > /var/log/messages
[root@shell_example mnt]# sh warning.sh
[root@shell_example mnt]# cat /var/log/messages
Warning: System root will full,please fix this warning !!
实验完成后将两个大文件删除,避免对后面的实验造成影响
[root@shell_example mnt]# rm -fr bigfile1
[root@shell_example mnt]# rm -fr bigfile2
[root@shell_example mnt]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 10G 3.4G 6.7G 34% /
devtmpfs 906M 0 906M 0% /dev
tmpfs 921M 140K 921M 1% /dev/shm
tmpfs 921M 21M 901M 3% /run
tmpfs 921M 0 921M 0% /sys/fs/cgroup
5.编写一个脚本,检测本机是否可以连通别的ip的主机,如果可以,输出ip is up,如果不可以输出ip is down,如果没有输入任何内容,提示输入ip
#!/bin/bash
[ -z "$1" ] && {
echo "Please input the ip you want to check after command"
}||{
ping -c1 -w1 $1 &> /dev/null && echo $1 is up || echo $1 is down
}
测试:
[root@shell_example mnt]# vim ping.sh
[root@shell_example mnt]# sh ping.sh
Please input the ip you want to check after command
[root@shell_example mnt]# sh ping.sh 172.25.254.25
172.25.254.25 is down
[root@shell_example mnt]# sh ping.sh 172.25.254.127
172.25.254.127 is down
[root@shell_example mnt]# sh ping.sh 172.25.254.68
172.25.254.68 is up