首页 > 系统服务 > 详细

shell脚本程序的编写

时间:2019-11-19 20:31:13      阅读:88      评论:0      收藏:0      [点我收藏+]

 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

技术分享图片

 

 

shell脚本程序的编写

原文:https://www.cnblogs.com/lf1191298944/p/11892090.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!