shell脚本程序的编写

 1.更改字体或背景的颜色

echo -e \033[背景颜色代码;字体颜色代码m内容\033[0m

字背景颜色:(40黑)(41深红)(42绿)(43黄)(44蓝)(45紫)(46深绿)(47白)

字颜色:(30黑)(31红)(32绿)(33黄)(34蓝)(35紫)(36深绿)(37白)

1.显示字体的颜色为红色=

[root@shell_example mnt]# echo -e "\033[31mhello\033[0m"

 

2.显示背景颜色为黑色,字体颜色为红色

[root@shell_example mnt]# echo -e "\033[40;31mhello\033[0m"

 

 2.更改http服务的端口

编写一个shell脚本:

[root@shell_example mnt]# vim apache_port.sh

脚本内容如下:

#!/bin/bash

[ -z "$1" ] && {

        echo -e "\033[31mError: Please input port number Apache Server\033[0m"

        exit

}

sed "/^Listen/cListen $1" -i /etc/httpd/conf/httpd.conf

systemctl restart httpd &>/dev/null

port=`netstat -antlupe | grep httpd | awk '{print $4}' | cut -d : -f 4`

[ "$1" -eq "$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 apache_port.sh   

输入非法内容时:

[root@shell_example mnt]# sh apache_port.sh hh    

输入其他端口号时:

[root@shell_example mnt]# sh apache_port.sh 8080    

检测端口号是否与设置的相同

[root@shell_example mnt]# netstat -antlp | grep httpd

[root@shell_example mnt]# netstat -antlp | grep httpd | awk '{print $4}'

[root@shell_example mnt]# netstat -antlp | grep httpd | awk '{print $4}' | cut -d : -f 4

 

 3.编写一个shell脚本使其使用for循环,从1输出到10

1)编辑一个shell脚本

[root@shell_example mnt]# vim file.sh

 

脚本内容如下:

#!/bin/bash

for i in $(seq 1 10)

do

        echo $i

done

 

运行脚本:

[root@shell_example mnt]# sh file.sh

 

 4.建立一个shell脚本,使其可以读取user_file(三个已经给了的用户)和pass_file(三个已经给了的密码)中的内容,没有创建的用户进行创建,密码进行修改,如果用户已存在,则显示用户已经存在

1)建立一个user_file文件,写入三个用户

   [root@shell_example mnt]# vim user_file

   [root@shell_example mnt]# cat user_file

 

2)建立一个pass_file文件,写入三个密码

[root@shell_example mnt]# vim pass_file

[root@shell_example mnt]# cat pass_file

 

3)编写一个shell脚本

[root@shell_example mnt]# vim user_create.sh

脚本内容如下:

#!/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 CREATE

}

done

 

4)脚本运行结果如下:

[root@shell_example mnt]# sh user_create.sh user_file pass_file

[root@shell_example mnt]# userdel -r user1

[root@shell_example mnt]# sh user_create.sh user_file pass_file

 

 5.使用shell脚本判断文件的类型

当没有给文件时,输出Please show me a file after script",当文件不存在时,输出"文件名称 is not exist",当文件的类型是其他类型时,输出每个文件的文件类型名称

编写一个shell脚本:

[root@shell_example mnt]# vim check_file.sh

脚本内容如下:

#!/bin/bash

[ -z "$1" ] && {

        echo "Error: Please show me a file after script"

        exit

}

[ -e "$1" ] || {

        echo "$1 is not exist"

        exit

}

[ -L "$1" ] && {

        echo "$1 is a link file"

        exit

}

[ -f "$1" ] && {

        echo "$1 is a common file"

        exit

}

[ -d "$1" ] && {

        echo "$1 is a directory"

        exit

}

[ -c "$1" ] && {

        echo "$1 is a char file"

        exit

}

[ -b "$1" ] && {

        echo "$1 is a block file"

        exit

}

[ -S "$1" ] && {

        echo "$1 is a socket file"

        exit

}

运行脚本,进行测试:

没有输入任何文件时:

[root@shell_example mnt]# sh check_file.sh

当输入的文件是链接文件时:

[root@shell_example mnt]# sh check_file.sh file

当输入的是一个目录时:

[root@shell_example mnt]# sh check_file.sh /mnt

当输入的文件不存在时:

[root@shell_example mnt]# sh check_file.sh file2

当输入的文件是一个块设备时:

[root@shell_example mnt]# sh check_file.sh /dev/sda

当输入的文件是一个字符设备时;

[root@shell_example mnt]# sh check_file.sh /dev/pts/0

当输入的文件是一个套接字时:

[root@shell_example mnt]# sh check_file.sh /var/lib/mysql/mysql.sock

/var/lib/mysql/mysql.sock is a socket file

 

 

 6.让一个文件中所有的内容都变为大写或者小写

编辑westos文件:

[root@shell_example mnt]# vim westos

[root@shell_example mnt]# cat westos

hahaLLdodoEH

 

westos文件中的内容都变为大写:

[root@shell_example mnt]# tr 'a-z' 'A-Z' < westos

HAHALLDODOEH

 

westos文件中的内容都变为小写:

[root@shell_example mnt]# tr 'A-Z' 'a-z' < westos

hahalldodoeh

猜你喜欢

转载自www.cnblogs.com/lf1191298944/p/11892090.html