【无标题】shell脚本的基本命令+编写shell脚本

打印 上一主题 下一主题

主题 880|帖子 880|积分 2644

shell脚本
一.shell根本
1.shell概念
2.shell脚本

3.shell脚本编写留意事项

二.编写shell脚本
1.编写一个helloworld脚本,运行脚本
  1. [root@shell ~]# vim helloworld.sh
  2. #!/bin/bash  //声明
  3. echo "hello world!"
  4. ls -lh /etc/
  5. 运行脚本(四种方式):
  6. [root@shell ~]# bash helloworld.sh
  7. [root@shell ~]# sh helloworld.sh
  8. [root@shell ~]# source helloworld.sh
  9. [root@shell ~]# chmod +x helloworld.sh
  10. [root@shell ~]# ./helloworld.sh
复制代码
2.编写一个nginx安装脚本
  1. [root@shell ~]# mkdir /root/shell
  2. [root@shell ~]# vim /root/shell/install_nginx.sh
  3. #!/bin/bash
  4. yum -y install gcc gcc-c++ make pcre-devel oprnssl-devel wget
  5. cd /usr/local/scr/
  6. wget 'https://nginx.org/download/nginx-1.26.1.tar.gz'
  7. tar xf nginx-1.26.1.tar.gz
  8. cd nginx-1.26.1
  9. ./configure --prefix=/usr/local/nginx
  10. make -j 4
  11. make install
  12. [root@shell shell]# cd /root/shell
  13. [root@shell shell]# sh install_nginx.sh
复制代码
三.变量
1.自定义变量
  1. [root@shell shell]# set a=3
  2. [root@shell shell]# echo a
  3. a
  4. [root@shell shell]# unset a
  5. [root@shell shell]# echo a
  6. a
  7. [root@shell shell]# echo $a
复制代码
2.环境变量
由体系维护,用于设置工作环境
脚本或者应用需要参数的时间利用
$PWD
$SHELL
$USER
  1. [root@shell shell]# vim test1.sh
  2. #!/bin/bash
  3. echo $1
  4. echo $2
  5. echo $3
  6. echo $4
  7. echo $5
  8. [root@shell shell]# bash test1.sh 1 2 3 4 5
  9. 1
  10. 2
  11. 3
  12. 4
  13. 5
复制代码
  1. [root@shell shell]# bash test1.sh t j j f b
  2. t
  3. j
  4. j
  5. f
  6. b
复制代码
3.位置变量
  1. [root@shell ~]# vim creat.sh
  2. #!/bin/bash
  3. useradd $1
  4. echo $2|passwd --stdin $1
  5. [root@shell ~]# sh creat.sh
复制代码
4.预定义变量

  1. [root@shell ~]# vim ss.sh
  2. #!/bin/bash
  3. #将所有的脚本参数输出
  4. for x in "$*"
  5. do
  6.   echo $x
  7. done
  8. [root@shell ~]# sh ss.sh a b c
  9. a b c
  10. [root@shell ~]# sh ss.sh g q i g d
  11. g q i g d
  12. 将*改为@运行
  13. [root@shell ~]# sh ss.sh g q i g d
  14. g
  15. q
  16. i
  17. g
  18. d
复制代码
四.判定语法
shell的返回值:运行一条命令,都会有一个返回值,0表示实行正常,非0表示实行非常。
  1. [root@shell ~]# test 3 -gt 2
  2. [root@shell ~]# echo $?
  3. 0
复制代码
1.条件判定---if语句
  1. [root@shell ~]# vim if.sh
  2. #!/bin/bash
  3. echo "1:"
  4. read a
  5. echo "2:"
  6. read b
  7. if [ $a -eq $b ]; then
  8.        echo "相等"
  9. else
  10.       echo "不相等"
  11. fi
  12. [root@shell ~]# sh if.sh
  13. 1:
  14. 3
  15. 2:
  16. 5
  17. 不相等
复制代码
2.创建检测脚本,检测网络是否流畅
  1. [root@shell ~]# vim web.sh
  2. #!/bin/bash
  3. read -p "输入网址:" web  //read:命令行输入web的值
  4. ping -c 3 $web &> /dev/null  //连接某个网站三次
  5. if [ $? -eq 0 ]; then   //如果ping命令执行成功
  6.     echo "网络畅通"
  7. else
  8.     echo "网络异常"
  9. fi
  10. root@shell ~]# sh web.sh
  11. 输入网址:www.baidu.com
  12. 网络畅通
复制代码
五.字符串判定格式:

1.字符串的比较
  1. [root@shell ~]# aaa="abc"
  2. [root@shell ~]# echo $aaa
  3. abc
  4. [root@shell ~]# test $aaa == "abc"
  5. [root@shell ~]# echo $?
  6. 0
  7. [root@shell ~]# test $aaa == "dggn"
  8. [root@shell ~]# echo $?
  9. 1
  10. [root@shell ~]# unset aaa
  11. [root@shell ~]# echo $aaa
复制代码
2.案例
(1)创建简朴的字符串判定脚本
  1. [root@shell ~]# vim zfc.sh
  2. #!/bin/bash
  3. read -p "请输入账号" user
  4. echo $user
  5. if [ $user == "admin" ]; then
  6.     echo "欢迎登录:$user"
  7. else
  8.    echo "账号或密码错误"
  9. fi
  10. [root@shell ~]# sh zfc.sh
  11. 请输入账号admin
  12. admin
  13. 欢迎登录:admin
  14. [root@shell ~]# sh zfc.sh
  15. 请输入账号egu
  16. egu
  17. 账号或密码错误
复制代码
(2)创建rpm查询软件是否安装
  1. [root@shell ~]# vim rpm.sh
  2. #!/bin/bash
  3. rpm -qa|grep nginx
  4. #echo $?
  5. yum -y install epel.release
  6. if [ $? -eq 1 ];then
  7.      yum -y install nginx
  8. else
  9.      yum -y remove nginx
  10.      yum -y install nginx
  11. fi
  12. [root@shell ~]# sh rpm.sh
  13.        
复制代码
六.文件,目录,权限的判定
1.格式:
2.常用操作符:

  1. [root@shell ~]# touch abc
  2. [root@shell ~]# ls -lh abc
  3. -rw-r--r--. 1 root root 0 7月  26 11:26 abc
  4. [root@shell ~]# [ -e "abc" ]
  5. [root@shell ~]# echo $?
  6. 0
  7. [root@shell ~]# [ -e "ewf" ]
  8. [root@shell ~]# echo $?
  9. 1
  10. [root@shell ~]# [ -e "aaaaaa" ]
  11. [root@shell ~]# echo $?
  12. 1
  13. [root@shell ~]# [ -w "aaaaaa" ]
  14. [root@shell ~]# echo $?
  15. 1
  16. [root@shell ~]# [ -x "abc" ]
  17. [root@shell ~]# echo $?
  18. 1
  19. [root@shell ~]# chmod +x abc
  20. [root@shell ~]# [ -x "abc" ]
  21. [root@shell ~]# echo $?
  22. 0
  23. [root@shell ~]# ls -l abc
  24. -rwxr-xr-x. 1 root root 0 7月  26 11:26 abc
复制代码
3.案例
nginx安装脚本优化,判定是否已经安装nginx
七.与或判定
判定多个条件
多个条件其中一个成立,或
多个条件都要成立,与
或运算判定:||
与运算判定:&&

1.或运算
  1. [root@shell ~]# vim huo.sh
  2. #!/bin/bash
  3. read -p "请输入名称" name
  4. if [ $name == "haha" ]||[ $name == "xixi" ];then
  5.    echo "成立"
  6. else
  7.    echo "不成立"
  8. fi
  9. [root@shell ~]# sh huo.sh
  10. 请输入名称haha
  11. 成立
复制代码
2.与运算
  1. [root@shell ~]# vim yu.sh
  2. #!/bin/bish
  3. read -p "请输入年龄" age
  4. read -p "请输入性别" gender
  5. if [ $age -ge 30 ] && [ $gender == "女" ];then
  6.    echo "涨工资"
  7. else
  8.    echo"不加"
  9. fi
  10. [root@shell ~]# sh yu.sh
  11. 请输入年龄34
  12. 请输入性别女
  13. 涨工资
复制代码
3.多层判定
  1. [root@shell ~]# vim dc.sh
  2. #!/bin/bash
  3. echo "1新增文件 2删除文件 3修改文件 4查找文件"
  4. read -p "请选择序号" m
  5. if [ $m == 1 ];then
  6.     touch aaaaa.txt
  7. elif [ $m == 2 ]; then
  8.     rm -fr aaaaa.txt
  9. else
  10.     echo "其他功能正在开发"
  11. fi
  12. [root@shell ~]# sh dc.sh
复制代码

八.shell读取用户输入
1.read
格式:read -选项
  1. [root@shell ~]# read -p "请输入" -s s
  2. 请输入[root@shell ~]# echo $s
  3. aadswdqdweqhjhbnhbuuiwefggywgywygfe
  4. [root@shell ~]# read -p "请输入" s
  5. 请输入dtdyuj
  6. [root@shell ~]# echo $s
  7. dtdyuj
  8. [root@shell ~]# read -p "三个变量" a b c
  9. 三个变量12 13 14
  10. [root@shell ~]# echo $a
  11. 12
  12. [root@shell ~]# echo $b
  13. 13
  14. [root@shell ~]# echo $c
  15. 14
  16. [rot@shell ~]# vim pas.sh
  17. #!/bin/bash
  18. read -p "username:" username
  19. read -p "password:" -s password
  20. useradd $useranme
  21. echo $password|passwd --stdin $username
  22. if [ $? -eq 0 ]; then
  23.     echo "账户$username注册成功"
  24. fi
复制代码
九.循环语法
1.for循环
  1. [root@shell ~]# vim int.sh
  2. #!/bin/bash
  3. for x in $*
  4. do
  5.   echo $x
  6. done
  7. [root@shell ~]# sh int.sh 我 是 秦始皇
  8. 秦始皇
  9. [root@shell ~]# vim int.sh
  10. #!/bin/bash
  11. for city in 青岛 北京 天津
  12. do
  13.   echo "$city是个好地方"
  14. done
  15. [root@shell ~]# sh city.sh
  16. 青岛是个好地方
  17. 北京是个好地方
  18. 天津是个好地方
复制代码
在命令效果中循环
  1. 在命令结果中循环:
  2. [root@shell ~]# vim 1.sh
  3. #!/bin/bash
  4. for u in $(awk -F':' '{print $1}' /etc/passwd)
  5. do  
  6.     echo "$u"
  7. done
  8. [root@shell ~]# sh 1.sh
复制代码
检查某个网段的存活主机
  1. [root@shell ~]# vim ping.sh
  2. #!/bin/bash
  3. for IP in $(echo 12.168.2.{1..150})
  4. do
  5.   ping -c 2 -i 0.1 $IP &> /dev/null
  6. if [ $? -eq 0 ]; then
  7.    echo "$IPisup "
  8. fi
  9. done
  10. [root@shell ~]# bash ping.sh
复制代码
2.while循环
3.循环的break和continue
break ---直接退出
continue---退出当前循环,进入下次循环
九九乘法表
  1. [root@shell ~]# vim jj.sh
  2. #!/bin/bash
  3. for i in {1..9};do
  4. for j in {1..9};do
  5. echo -n "$j*$i=$(($i*$j)) "
  6. if [ $j == $i ];then
  7. echo -e '\n'
  8. break
  9. fi
  10. done
  11. done
  12. [root@shell ~]# bash jj.sh
  13. 1*1=1
  14. 1*2=2 2*2=4
  15. 1*3=3 2*3=6 3*3=9
  16. 1*4=4 2*4=8 3*4=12 4*4=16
  17. 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
  18. 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
  19. 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
  20. 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
  21. 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
复制代码
十.sed
sed是文本处理工具,用来读取文件内容
格式:sed '过滤+动作' 文件路径
sed选项:


  1. [root@shell ~]# rm -rf *
  2. [root@shell ~]# cp /etc/sysconfig/network-scripts/ifcfg-ens33 ./
  3. [root@shell ~]# sed -n '2p' ifcfg-ens33 //打印第二行
  4. PROXY_METHOD=none
  5. [root@shell ~]# sed -n '1,3p' ifcfg-ens33  //打印1到3行
  6. TYPE=Ethernet
  7. PROXY_METHOD=none
  8. BROWSER_ONLY=no
  9. [root@shell ~]# sed -n '1p;3p' ifcfg-ens33 //打印1,3行
  10. TYPE=Ethernet
  11. BROWSER_ONLY=no
复制代码
配置一个自动设置静态ip以及关闭selinux服务,关闭NetWorkManager,修改主机名脚本,ip和主机名利用read输入,这个操作只能在root下实行
[root@shell ~]# vim zd.sh
#!/bin/bash
#备份
cp /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-ens33.bak
read -p "请输入ip地址:" ip
sed -i '/dhcp/s/dhcp/none/g' /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i '$aIPADDR='"$ip"'' /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i '$aNETMASK=255.255.255.0' /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i '$aGATEWAY=192.168.2.1' /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i '$aDNS1=8.8.8.8' /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i '$aDNS2=114.114.114.114' /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i '/UUID/cUUID='"$(uuidgen)"'' /etc/sysconfig/network-scripts/ifcfg-ens33
#修改主机名
read -p "请输入主机名称:" hn
hostnamectl set-hostname $hn
#停用selinux
setenforce 0
sed -i '/SELINUX/cSELINUX=disabled' set /etc/selinux/config
#停用防火墙
systemctl stop firewalld
systemctl disable firewalld
#停用NetworkManager
systemctl stop NetworkManager
systemctl disable NetworkManager
[root@shell ~]# bash zd.sh
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

刘俊凯

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表