shell脚本

[root@centos7.7 tmp]# cd /data/
[root@centos7.7 data]# ls
[root@centos7.7 data]# mkdir newsh
[root@centos7.7 data]# cd newsh/.
[root@centos7.7 newsh]# ls
[root@centos7.7 newsh]# ll
总用量 0

#1 编写脚本文件 vi Jackie_shell.sh
[root@centos7.7 newsh]# vim jackie_shell.sh
#!/bin/bash
#auto mkdir
#by author jackie.net
mkdir -p /tmp/20161028 #在tmp下新建目录
echo -e “\033[35m Helld,world.\033[0m”
~
执行这个脚本文件
[root@centos7.7 newsh]# /bin/bash jackie_shell.sh
Hello,world.
<1>查看脚本执行权限
[root@centos7.7 newsh]# ll jackie_shell.sh
-rw-r–r--. 1 root root 55 10月 31 19:10 jackie_shell.sh
给脚本文件赋权
[root@centos7.7 newsh]# chmod o+x jackie_shell.sh
[root@centos7.7 newsh]# ll jackie_shell.sh
-rw-r–r-x. 1 root root 55 10月 31 19:10 jackie_shell.sh
<2> 执行这个执行脚本
[root@centos7.7 newsh]# ./jackie_shell.sh
Hello,world.
调试脚本报错 # -x
[root@centos7.7 newsh]# /bin/bash -x jackie_shell.sh

  • mkdir -p /tmp/20161028
  • echo Hello,world.
    Hello,world.

#2 编写脚本文件 vi jackie_lamp.sh
#!/bin/bash
#auto make install LAMP
#by author jackie.txt 2016
echo “----------------”
yum install httpd httpd-devel php php-devel -y
echo “The LAMP env install success”
[root@centos7.7 newsh]# sh jackie_lamp.sh

#优化字符输出
#-e 扩展参数
#\n 换行
echo -e “\033[32m Start scripts—\n----------------\033[0m”
echo -e “\033[35m Hello,world.\033[0m”
~

<2>输出增改脚本文件 vi jackie_lamp.sh
#LAMP 变量
vim jackie_lamp.sh
#!/bin/bash
#auto make install LAMP
#by author jackie.txt 2016
echo -e “\034[35m Start scripts—\n----------------\033[0m”
LAMP=“vim tree httpd”
#######
yum install $LAMP
yum install $LAMP

shell 常见的系统变量解析
$0 当前脚本的名称
$n 当前脚本的第n个参数n=1,2,9
$* 当前脚本的所有参数(不包括程序本身)
$# 当前脚本的参数个数(不包括程序本身)
$? 命令或执行完后的状态,一般返回0表示执行成功
$$ 脚本的PID
$UID 当前用ID
$PWD 当前所在目录
#1 $?
[root@centos7.7 ~]# pwd
/root
[root@centos7.7 ~]# echo $?
0
[root@centos7.7 ~]# pwdabc
bash: pwdabc: 未找到命令…
[root@centos7.7 ~]# echo $?
127
[root@centos7.7 ~]#

#\ 转意符号
#3 编写脚本变量 vim var.sh
#!/bin/bash
#auto print variables
#by wugk 2020-10-31
echo -e ‘\033[35m--------------\033[0m’
echo “This $0 is $0 param !”
echo “This $1 is $1 param !”
echo “This $2 is $2 param !”
echo -e ‘\033[35m--------------\033[0m’
echo
echo “This $* is $* param !”
echo “This $# is $# param !”
echo “This $? is $? param !”
echo “This $$ is $$ param !”

$0 当前脚本的名称
$n 当前脚本的第n个参数n=1,2,9
$* 当前脚本的所有参数(不包括程序本身)
$# 当前脚本的参数个数(不包括程序本身)
$? 命令或执行完后的状态,一般返回0表示执行成功 *上条命令是否成功
$$ 脚本的PID
[root@centos7.7 newsh]# vim var.sh


This $0 is var.sh param !
This $1 is a param !This


This $* is a b c param !
This $# is 3 param !
This $? is 0 param !
This $$ is 122795 param !

#4 编写变量脚本 vim jackie_install_software
#!/bin/bash
#jackie install software
#by author jackie.net
#define Path variables
soft_ware1="$1"
soft_ware2="$2"
yum install $soft_ware $soft_ware2 -y
~
[root@centos7.7 newsh]# sh jackie_install_software.sh vim mysql
[root@centos7.7 newsh]# /bin/bash jackie_install_software.sh vim mysql

<1>增改的脚本文件vim jackie_install_sofeware
soft_ware="$*" #表示所有参数都会装上
[root@centos7.7 newsh]# sh jackie_install_software.sh vim mysql centos tomcat

#5 编写脚本文件 vi jackie_apache_menu.sh
#!/bin/bash
#auto install httpd
#by jackie 2020-11-1
#====================
echo -e ‘\033[35m--------------\033[0m’

files=httpd-2.2.31.tar.bz2
url=http://mirrors.cnnic.cn/apache/httpd/
prefix=/usr/local/apache2

echo -e ‘\033[35mPlease Select Install Menu:\033[0m’
echo
echo “1)官方下载Httpd文件包.”
echo “2)解压Httpd源码包.”
echo “3)编译安装Httpd服务器.”
echo “4)启动Httpd服务器.”
echo -e ‘\033[35m--------------\033[0m’
sleep 20 #等待20秒在运行下面指令
echo Done

<1>增改的脚本文件 vi Jackie_apache_menu.sh
echo “1)官方下载 f i l e s . " e c h o " 2 ) 解 压 files." echo "2)解压 files."echo"2)files源码包.”
echo “3)编译安装httpd.”
echo “4)启动HTTPD服务器.”
#启动脚本文件


Please Select Install Menu:

1)官方下载httpd-2.2.31.tar.bz2.
2)解压httpd-2.2.31.tar.bz2源码包.
3)编译安装httpd.


Done.

<2>增改的脚本文件 vi Jackie_apache_menu.sh
echo -e “\033[31mUsage: { /bin/sh $0 1|2|3|4|help}\033[0m”
#启动脚本文件
[root@centos7.7 newsh]# /bin/bash jackie_apache.menu.sh
Usage: { /bin/sh jackie_apache.menu.sh 1|2|3|4|help}

if条件语句学习
if条件 if for while case
#(()) 代表运算
#[] if运行区间代表运算
#;then 固定格式
#if 以fi结尾
#-d 判断目录
#-f 判断文件
#!-d 如果目录不存在
#%s/dir/files/g 替换命令 files替换dir
#-eq 等价符号 相当于等于
#configure --pref x= #定义安装的路径
#-z 为空
#wget -c 断点续传命令
#-lt 小于0
if
语句1
else
语句2

#1 编写脚本文件
[root@centos7.7 newsh]# vim number.sh
#!/bin/bash
NUM=100

if (($NUM > 4));then
echo “The NUM is $NUM greater 4.”
else
echo “The NUM is $NUM little 4.”
fi #以fi结尾
[root@centos7.7 newsh]# /bin/bash number.sh
The NUM is 100 greater 4.

<1>增改的脚本文件 vim number.sh
NUM=$1 #$1脚本的第一个参数
[root@centos7.7 newsh]# /bin/bash number.sh 10
The NUM is 10 greater 4.
[root@centos7.7 newsh]# /bin/bash number.sh 2
The NUM is 2 little 4.

#2 编写脚本文件 测试是否存在,不存在则新建
[root@centos7.7 newsh]# vim if_dir.sh
#!/bin/bash

DIR=/data/2020/11/1
if [ ! -d $DIR ];then
mkdir -p $DIR
echo " The $DIR create success."
else
echo “The $DIR is exist.”
exit
fi
输出脚本文件
[root@centos7.7 newsh]# /bin/bash if_dir.sh
The /data/2020/11/1 create success.

#3 编写脚本文件 vim if_files.sh
#!/bin/bash

files=$1
if [ ! -f $files ];then
touch $files
echo " The $files create success."
else
echo “The $files is exist.”
exit
fi
[root@centos7.7 newsh]# /bin/bash if_files.sh /etc/hosts
The /etc/hosts is exist.
[root@centos7.7 newsh]# /bin/bash if_files.sh /data/test.txt
The /data/test.txt create success.

<1>增改的脚本文件 vim if_files.sh
#!/bin/bash

INPUT=$1
##########
if [ $INPUT == “Yes” ];then
echo “Please install LAMP.”
else
echo “Please exit.”
fi
[root@centos7.7 newsh]# /bin/bash if_files.sh 2
Please exit.
[root@centos7.7 newsh]# /bin/bash if_files.sh Yes
Please install LAMP.

脚本格式
H_FILES=httpd-2.2.27.tar.bz2 # 依赖文件
H_FILES_DIR=httpd-2.2.27 # 进入目录
H_URL=https://mirrors.cnnic.cn/apache/httpd/httpd-2.2.27.tar.bz2 #下载镜像
H_PREFIX=/usr/local/apache2/ #定义安装的路径

<2>增改脚本文件 vi jackie_apache.menu.sh
if [ 1 − e q 1 ] ; t h e n w e g e t − c 1 -eq 1 ];then weget -c 1eq1];thenwegetch_url/$h_files
tar -jxf $h_files ;cd h f i l e s d i r ; c o n f i g u r e − − p r e f i x = h_files_dir;configure --prefix= hfilesdir;configureprefix=h_prefix
make && make install

shell 脚本编写思路

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值