温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

最基础的shell脚本编写

发布时间:2020-07-16 20:09:15 来源:网络 阅读:382 作者:linuxfan_小君 栏目:开发技术

一、练习脚本的基本编写流程:

     1.确定任务及完成任务的命令
     2.编写脚本整合任务
     3.授权并测试执行



二、练习:

a.首先我先创建一个目录,把我们编写的脚本都放入,这样不会太乱

[root@www ~]# mkdir -p /root/bin
[root@www ~]# cd /root/bin

b.下面开始编译

[root@www bin]# vi sysinfo.sh
#!/bin/bash    ##从/etc/shells中选择一个shell解析下面的内容
#by linuxfan
#20160114
#system info.
#我的第一个脚本,这是一个注释。除了第一行的解释权,其他带#属于是注释
#查看网络相关信息
ip a
hostname
cat /etc/hosts
cat /etc/resolv.conf
ip r
#查看设备包括内存、cpu、磁盘等使用情况
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.对文件进行授权执行

[root@www bin]# chmod +x sysinfo.sh   ##授权
[root@www bin]# /root/bin/sysinfo.sh   ##路径执行
[root@www bin]# sh -x sysinfo.sh  ##测试时常用不需要执行权限
[root@www tmp]# source sysinfo.sh  ##一般用于让配置文件生效等,不需要执行权限



三、我们可以结合变量编写安装apache的脚本

[root@www 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
##########解压缩,并安装#############
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
以此类推,编写安装mysql及php的脚本
chmod +x install_httpd.sh
/root/bin/install_httpd.sh   ##执行脚本并安装
ls /usr/local/httpd



四、练习重定向和管道

[root@www bin]#ip a |grep eth0 |grep inet |awk {'print $2'} |awk -F '/' {'print $1'}
[root@www bin]# vim  hosts.sh
#!/bin/bash
#2016-01-14 by linuxfan
##设置hosts解析,为www.linuxfan.cn
IP=$(ip a |grep eth0 |grep inet |awk {'print $2'} |awk -F '/' {'print $1'})
read -p "input FQDN:" NANE
echo "$IP     $NANE"  >>/etc/hosts
:wq
[root@www bin]#sh -x hosts.sh
[root@www bin]#cat /etc/hosts



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI