First, the basic writing process of the practice script:
1. Determine the task and the command to complete the task
2. Scripting Integration Tasks
3. Authorization and test execution
Second, exercise:
A. First I create a directory, put the script we write into, so it's not too messy
[Email protected] ~]# mkdir-p/root/bin
[Email protected] ~]# Cd/root/bin
B. Start compiling below
[Email protected] bin]# VI sysinfo.sh
#!/bin/bash # #从/etc/shells Select a shell to parse the content below
#by Linuxfan
#20160114
#system info.
#我的第一个脚本, this is a comment. In addition to the first line of interpretation right, the other band # belongs to the comment
#查看网络相关信息
IP A
Hostname
Cat/etc/hosts
Cat/etc/resolv.conf
IP r
Usage of #查看设备包括内存, CPU, disk, etc.
Cat/proc/cpuinfo
Lscpu
Cat/proc/meminfo |grep-i Total
Free
Cat/proc/partitions
Df-ht
# #查看进程和服务情况
PS aux |wc-l
Service--status-all
Lang=c
Chkconfig--list |grep 3:on
: Wq
C. Authorizing the implementation of a document
[Email protected] bin]# chmod +x sysinfo.sh # #授权
[Email protected] bin]#/root/bin/sysinfo.sh # #路径执行
[Email protected] bin]# sh-x sysinfo.sh # #测试时常用不需要执行权限
[[email protected] tmp]# Source sysinfo.sh # #一般用于让配置文件生效等, do not need to execute permissions
Third, we can combine the variables to write a script to install Apache
[Email protected] bin]# vim install_httpd.sh
#!/bin/bash
#by Fage 2016-01-14
#install Apache httpd!
############ #download httpd#####
hv=httpd-2.2.17
wget ftp://ftp.linuxfan.cn/tools/$HV. Tar.gz-p/root
######### #解压缩, and install #############
Tar zxvf/root/$HV. tar.gz-c/usr/src/>/dev/null
cd/usr/src/$HV/
./configure--prefix=/usr/local/httpd--enable-so--enable-cgi--enable-rewrite--enable-ssl&&make & &make Install >&>/dev/null
: Wq
And so on, write scripts to install MySQL and PHP
chmod +x install_httpd.sh
/root/bin/install_httpd.sh # #执行脚本并安装
Ls/usr/local/httpd
Iv. Practice Redirection and piping
[[email protected] bin] #ip a |grep eth0 |grep inet |awk {' Print $ '} |awk-f '/' {' Print $ '}
[Email protected] bin]# vim hosts.sh
#!/bin/bash
#2016 -01-14 by Linuxfan
# #设置hosts解析, for www.linuxfan.cn
ip=$ (IP a |grep eth0 |grep inet |awk {' Print $ '} |awk-f '/' {' Print $ '})
Read-p "input FQDN:" NANE
echo "$IP $NANE" >>/etc/hosts
: Wq
[[email protected] bin] #sh-X hosts.sh
[[email protected] bin] #cat/etc/hosts
This article is from the "11982647" blog, please be sure to keep this source http://11992647.blog.51cto.com/11982647/1873315
The most basic shell scripting