Simple Shell Script Design

Copyright: Susu acridine https://blog.csdn.net/weixin_44774638/article/details/91489141

Issue
in this case requires written three scripts, respectively, to achieve the following objectives:
1) the output of a text on the screen "the Hello World"
2) capable of native quickly with a good Yum warehouse
3) can be quickly assembled a native vsftpd service
program
when a large amount of code of the script, or script to be passed to other collaborators use, the script specification can significantly reduce the difficulty of code maintenance (function may need to change a few months later), to improve the readability of the code (no junior partner frequently to ask you questions like "why is this variable is used").
Note: In the process of learning Shell script, if the code was relatively low, usually omit the Notes.
Shell scripts a standard configuration comprising:
script statement (required interpreter, author information, etc.)
annotation information (step idea, use, etc. Variable Meaning)
executable statement (operation code)
Shell script implementation:
method a, as "command word": Specifies the script file path, provided there is permission x
Second method, as the "parameters": using sh, source or point number to load a script file.
steps
to achieve this case need to follow the steps below.

Step one: write your first script Shell, output "Hello World"

1) for manual test script function
to output a while, you can use the echo command, the specified string enclosed in single quotes to:

[root@svr5 ~]# echo 'Hello World'  
Hello World

2) write a script file according to the operation manual tasks

[root@svr5 ~]# vim  /root/first.sh  
#!/bin/bash
echo  'Hello World' 

[root@svr5 ~]# chmod  +x  /root/first.sh  					//添加可执行权限

3) execute scripts, test results

[root@svr5 ~]# /root/first.sh 
Hello World

Shell scripts are written with a fast native Yum repository: Step Two

1) for manual test script function
for RHEL client configure Yum, you need to go to the next directory /etc/yum.repos.d/ establish designated warehouse configuration file; Also, pay attention to rule out interference from other warehouse configurations. The task of implementation from scratch, after finishing the operation is as follows (in order to trigger the disc mount point / misc / cd for Yum repository, for example).
First, clean up existing Yum repository configuration file:

[root@svr5 ~]# rm -rf /etc/yum.repos.d/*.repo
[root@svr5 ~]# ls /etc/yum.repos.d/*  						//确认清理结果

ls: can not access /etc/yum.repos.d/*: No such file or directory
Then, create a new Yum repository configuration file:

[root@svr5 ~]# vim /etc/yum.repos.d/rhel6.repo 
[rhel6]
name=Red Hat Enterprise Linux 6
baseurl=file:///misc/cd
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

2) to write the script file operation manual tasks according to
established operating rhel6.repo vim editor by typing user interaction is required, inconvenient to use Shell script. However, since the configuration content Yum is fixed, and therefore can be used to redirect the echo configuration and then display the file repository, configurations content in single quotation marks, to the normal line feed.
Script content written reference as follows:

[root@svr5 ~]# vim  /root/el6repo.sh
#!/bin/bash
rm  -rf  /etc/yum.repos.d/*.repo 
echo  '[rhel-packages]
name=Red Hat Enterprise Linux 6
baseurl=file:///misc/cd
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
'  > /etc/yum.repos.d/rhel6.repo 

[root@svr5 ~]# chmod +x /root/el6repo.sh  					//添加可执行权限

3) execute scripts, test results
in order to facilitate inspection results, first clean /etc/yum.repos.d/ catalog:

[root@svr5 ~]# rm -rf /etc/yum.repos.d/*
[root@svr5 ~]# ls /etc/yum.repos.d/*  						//确认清理结果

ls: can not access /etc/yum.repos.d/*: No such file or directory
to perform the configuration script Yum repository:

[root@svr5 ~]# /root/el6repo.sh 
[root@svr5 ~]# 

Checking the Configuration:

[root@svr5 ~]# ls /etc/yum.repos.d/*  						//仓库配置已建立
/etc/yum.repos.d/rhel6.repo

[root@svr5 ~]# yum repolist  								//Yum仓库已可用
rhel-packages                              | 3.9 kB     00:00 ... 
rhel-packages/primary_db                  | 3.1 MB     00:00 ... 
repo id                repo name                       status
rhel-packages         Red Hat Enterprise Linux 6     3,690
repolist: 3,690

Step Three: Writing Shell Scripts quick assembly vsftpd service

1) for manual test script functions
include the following procedures learned in the Linux network services part of the knowledge, assembly vsftpd service.
First, make sure to install the vsftpd package (yum execution can be installed):

root@svr5 ~]# yum -y install vsftpd   					//不管是否已安装
.. ..
然后,确保启动vsftpd服务:
[root@svr5 ~]# service vsftpd restart  				//不管是否已启动
.. ..
最后,可设置vsftpd开机后能够自动运行:
[root@svr5 ~]# chkconfig vsftpd on  					//不管是否已设置

2) The scripting file operation manual tasks
in the order manual task scheduling script content, refer to the following:

[root@svr5 ~]# vim  /root/ftpon.sh
#!/bin/bash
yum  -y  install  vsftpd  &> /dev/null
service  vsftpd  restart
chkconfig  vsftpd  on

[root@svr5 ~]# chmod  +x  /root/ftpon.sh  				//添加可执行权限

3) execute scripts, test results
in order to facilitate inspection results, first remove the vsftpd package:

[root@svr5 ~]# yum -y remove vsftpd
.. ..
[root@svr5 ~]# rpm -q vsftpd  							//确认已卸载
package vsftpd is not installed
执行快速装配vsftpd服务的脚本:
[root@svr5 ~]# /root/ftpon.sh 
关闭 vsftpd:                                              [失败]
为 vsftpd 启动 vsftpd:                                    [确定]
确认脚本执行结果:
[root@svr5 ~]# rpm -q vsftpd
vsftpd-2.2.2-11.el6_4.1.x86_64

[root@svr5 ~]# service vsftpd status
vsftpd (pid 45694) 正在运行...

[root@svr5 ~]# chkconfig --list vsftpd
vsftpd          0:关闭  1:关闭  2:启用  3:启用  4:启用  5:启用  6:关闭

Guess you like

Origin blog.csdn.net/weixin_44774638/article/details/91489141