shell 脚本介绍

[root@oldboy02 scripts]# file /etc/init.d/iptables (file 主要是用来查看文件类型)
/etc/init.d/iptables: POSIX shell script text executable
[root@oldboy02 scripts]# ls
ip.sh
[root@oldboy02 scripts]# /server/scripts/ip.sh (这是绝对路径正常的运行脚本)
-bash: /server/scripts/ip.sh: Permission denied
[root@oldboy02 scripts]# ls -l ip.sh
-rw-r--r-- 1 root root 21 Nov 21 09:33 ip.sh (默认创建的ip.sh脚本文件的权限跟普通文件的权限是一样的都是644)
[root@oldboy02 scripts]# sh /server/scripts/ip.sh (sh的这种 方式是最常用的方式)
root@oldboy02 scripts]# x=1 定义了变量
[root@oldboy02 scripts]# echo
[root@oldboy02 scripts]# echo $x   (取变量里面的内容就是用$ )
[root@oldboy02 scripts]# echo ${LANG}
en_US.UTF-8
[root@oldboy02 scripts]# echo $LANG
en_US.UTF-8
[root@oldboy02 scripts]# echo ${LANG} (引用系统环境变量要用${}的方式引用才行)
en_US.UTF-8
[root@oldboy02 scripts]# echo $LANG
en_US.UTF-8
[root@oldboy02 scripts]# echo $LANG $PATH $PS1 这些都是环境变量,也称全局变量
en_US.UTF-8 /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin [\u@\h \W]\$
[root@oldboy02 scripts]#
 
[root@oldboy02 scripts]# vim show.sh
  echo $OLDBOY
"show.sh" [New] 2L, 25C written
[root@oldboy02 scripts]# cat show.sh
#!/bin/bash
echo $OLDBOY
[root@oldboy02 scripts]# OLDBOY=10
[root@oldboy02 scripts]# echo $OLDBOY
10
[root@oldboy02 scripts]# sh /server/scripts/show.sh (执行显示结果为空的原因是$OLDBOY是一个局部变量只能在命令行执行,如果放在脚本里面是不会执行的,要想$OLDBOY可以使用就得
变成全局变量才行,或者变成环境变量就必须在OLDBOY前面加上export 才行)
[root@oldboy02 scripts]# sh show.sh
 
[root@oldboy02 scripts]# export OLDBOY
[root@oldboy02 scripts]# sh /server/scripts/show.sh
10
 
[root@oldboy02 scripts]# sh show.sh
 
[root@oldboy02 scripts]# vim show.sh
#!/bin/bash
echo $LANG
[root@oldboy02 scripts]# sh show.sh (这个系统环境变量或者是全局变量就可以执行结果了)
en_US.UTF-8
[root@oldboy02 scripts]# env 显示系统环境变量
[root@oldboy02 scripts]# unset OLDBOY 取消环境变量 
[root@oldboy02 scripts]# vim /etc/profile.d/  是登录系统就要运行的脚本
colorls.csh  cvs.csh      glib2.csh    lang.csh     less.csh     vim.csh      which2.sh    
colorls.sh   cvs.sh       glib2.sh     lang.sh      less.sh      vim.sh       
[root@oldboy02 scripts]# vim /etc/profile.d/show.sh
#!/bin/bash
ip a s eth0
[root@oldboy02 scripts]# chmod +x  /etc/profile.d/show.sh 要赋予执行权限才能执行
[root@oldboy02 scripts]# ll /etc/profile.d/show.sh
-rwxr-xr-x 1 root root 26 Nov 21 11:01 /etc/profile.d/show.sh
 
Last login: Tue Nov 20 15:40:58 2018 from 10.0.0.1 断开重新连接系统就会自动显示ip地址
 
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:46:5d:0e brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.200/24 brd 10.0.0.255 scope global eth0
    inet6 fe80::20c:29ff:fe46:5d0e/64 scope link
       valid_lft forever preferred_lft forever
我们要把我们写好的脚本放到/etc/profile.d/下面才行,这样系统开机进入系统后就可以自动运行这个脚本了,不需要我们在手动执行每一个脚本了,切记这个要运行的脚本必须要有x 执行权限才可以
 
如 /etc/profile.d/show.sh
  /etc/profile.d/test.sh 这样开机的时候就会自动运行 show.sh和test.sh 这两个脚本了
[root@oldboy02 scripts]# ls
ip.sh  show-arg.sh  show.sh
[root@oldboy02 scripts]# cat show-arg.sh
#!/bin/bash
 
#
#
#
#
echo '$0:' $0 '$1,$2,$3:'  $1 $2 $3 '$#:' $#
$0:在脚本里面$0表示脚本的名字
$1 在脚本里面表示第1个参数 a
$2 在脚本里面表示第2个参数 b
$3  在脚本里面表示第3个参数 c$# 表示脚本一共有多少个参数 参数的个数
$#  表示脚本一共有多少个参数 参数的个数
以上这些都表示的是位置的参数
[root@oldboy02 scripts]# sh show-arg.sh
$0: show-arg.sh $1,$2,$3: $#: 0
[root@oldboy02 scripts]#
[root@oldboy02 scripts]# sh show-arg.sh a b c (show-arg.sh 表示脚本名称)
$0: show-arg.sh $1,$2,$3: a b c $#: 3
 
[root@oldboy02 scripts]# /etc/init.d/iptables
Usage: iptables {start|stop|reload|restart|condrestart|status|panic|save} (Usage 与$0 一样表示脚本名称)
start|stop|reload|restart|condrestart|status|panic|save 这些都相当于是脚本的参数,就是$加数字的方式实现的
 
查看上一个命令的返回值是否正确与否的判断显示
[root@oldboy02 scripts]# llll
-bash: llll: command not found
[root@oldboy02 scripts]# echo $?
127  (返回值为数字表示上一个命令的执行时错误的)
[root@oldboy02 scripts]# touch test.txt
[root@oldboy02 scripts]# echo $?
0 返回值为0表示执行命令式正确的
一般在执行一些安装服务或软件比价多的情况下,你不知道是否执行成功了时,可以用echo $?来检测
如果执行成功结果显示为0,非0表示执行错误
 
向变量里面传参数方法一
[root@oldboy02 scripts]# vim test.sh
#!/bin/bash
x=$1
y=$2
echo $x $y
[root@oldboy02 scripts]# sh test.sh 10 20
10 20
10  传参给$1
20 传给$2
[root@oldboy02 scripts]# read -p "input x y:"  x y -p表示在屏幕上显示输出
input x y:10 20
[root@oldboy02 scripts]# echo $x
10
[root@oldboy02 scripts]# echo $y
20
向 x y 两个变量里面写入了10和20 这种是交互式的向变量里面写东西
向变量里面传参数方法二
[root@oldboy02 scripts]# cat read.sh
#!/bin/bash
 
read -p "input x y:" x y
echo $x $y
[root@oldboy02 scripts]# sh read.sh
input x y:30 50
30 50
shell 里面的循环的条件
[root@oldboy02 scripts]# [ -f /root/oldboy.alex.txt ] 判断这个文件是否存在,如果存在返回值就是0,如果不存在返回值就是非0
[root@oldboy02 scripts]# echo $?
1 (输出结果显示1表示这个文件不存在)
[root@oldboy02 scripts]# [ -f /root/oldboy.txt  ] -f 表示文件
[root@oldboy02 scripts]# echo $?
0 (返回值为0表示文件是存在的)
[root@oldboy02 scripts]# [ -d /root  ]
[root@oldboy02 scripts]# echo $?
0
[root@oldboy02 scripts]# [ -d /rot  ] -d判断目录
[root@oldboy02 scripts]# echo $?
1
[root@oldboy02 scripts]# [ 1 -eq 1 ]
[root@oldboy02 scripts]#
[root@oldboy02 scripts]# echo $?
0
[root@oldboy02 scripts]# [ 1 -eq 10 ]
[root@oldboy02 scripts]# echo $?
1
  ####条件语句的脚本##3
一个条件的单分支结构语句判断
[root@oldboy02 scripts]# cat if.sh
#!/bin/bash
num1=$1
num2=$2
if [ $num1 -gt $num2 ];then
echo $num1 greater  than $num2
fi
[root@oldboy02 scripts]# sh /server/scripts/if.sh 20 10
20 greater than 10
 
 
 
两个条件的双分支条件语句
[root@oldboy02 scripts]# vim if.sh
#!/bin/bash
num1=$1
num2=$2
if [ $num1 -gt $num2 ];then
        echo $num1 greater than $num2
else
        echo $num1 less than $num2
fi
[root@oldboy02 scripts]# sh if.sh 10 20
10 less than 20
[root@oldboy02 scripts]# sh if.sh 30 20
30 greater than 20
 
[root@oldboy02 scripts]# cat if.sh
#!/bin/bash
num1=$1
num2=$2
if [ $# -ne 2 ];then
    echo "Usage: please input 2 number: num1 num2" (Usage 表示说明的意思  )
fi  (这个条件语句表示执行完后没有退出)
 
if [ $num1 -gt $num2 ];then
    echo $num1 greater than $num2
else
    echo $num1 less than $num2
fi
 
[root@oldboy02 scripts]# sh if.sh 10 输入一个数字提示报错信息
Usage: please input 2 number: num1 num2  提示输入两个数字
if.sh: line 8: [: 10: unary operator expected
10 less than
 
[root@oldboy02 scripts]# cat if.sh
#!/bin/bash
num1=$1
num2=$2
if [ $# -ne 2 ];then
    echo "Usage: please input 2 number: num1 num2"
    exit (执行完退出)
fi
 
if [ $num1 -gt $num2 ];then
    echo $num1 greater than $num2
else
    echo $num1 less than $num2
fi
 
 
[root@oldboy02 scripts]# sh if.sh 10 20
10 less than 20
[root@oldboy02 scripts]# sh if.sh 20 10
20 greater than 10
 
 
 [root@oldboy02 scripts]# sh if.sh 10 10
10 eqal 10
 
for 循环语句 
for循环的主要功能是解决重复执行相同的事情
 
for 循环语句格式
for 变量名字 in  列表 (这个列表一般用命令的结果或是序列生成)生成序列的方法有两种分别是: seq 和 {} 
do
        命令
done
 
[root@oldboy02 scripts]# for num in 1 2 3 4 5 6 7
> do
> echo "the $num number is :$num"                               (输出第一个数字是1,第二个数字是2)
> done
the 1 number is :1
the 2 number is :2
the 3 number is :3
the 4 number is :4
the 5 number is :5
the 6 number is :6
the 7 number is :7
 
[root@oldboy02 scripts]# for num in {01..10} 生成序列
> do
> echo "the $num number is :$num"
> done
the 01 number is :01
the 02 number is :02
the 03 number is :03
the 04 number is :04
the 05 number is :05
the 06 number is :06
the 07 number is :07
the 08 number is :08
the 09 number is :09
the 10 number is :10
 
[root@oldboy02 scripts]# vim aa.sh
#!/bin/bash
 
for i in {1..10}
do
        echo "the $i frist is :$i"
 
done
[root@oldboy02 scripts]# sh aa.sh
the 1 frist is :1
the 2 frist is :2
the 3 frist is :3
the 4 frist is :4
the 5 frist is :5
the 6 frist is :6
the 7 frist is :7
the 8 frist is :8
the 9 frist is :9
the 10 frist is :10
 
 
                            
 

猜你喜欢

转载自www.cnblogs.com/yangjuncheng0826/p/10015390.html