当命令或者程序不再命令行执行,而是通过程序文件来执行,这个程序就被称为shell脚本。shell编程属于脚本语言,是相对与编译型语言(如C\C++,java,C#等 )而言的,脚本语言往往是解释运行,而非编译,它由编译器读入脚本程序代码,转化成内部的形式执行,脚本语言的特性(结构简单,使用方便,容易修改,开放产能好)。
1.脚本内容简介
打开文本编辑器,可以使用vim命令来创建脚本文件,新建一个文件test.sh,扩展名为sh,也可以使用其他,但是为了方便识别,sh表明此文件是shell脚本文件。#! 是一个约定的标记,它告诉系统这个脚本需要什么解释器来执行,即使用哪一种Shell,一般的解释器是 bash。
#!/bin/bash
date
查看系统默认shell。 echo $SHELL
[root@foundation77 mnt]# echo $SHELL
/bin/bash
2.脚本执行方法
sh test.sh | bash test.sh 脚本文件没有执行权限时使用
[root@foundation77 mnt]# ll test.sh
-rw-r--r--. 1 root root 17 Dec 27 13:00 test.sh
[root@foundation77 mnt]# sh test.sh
Thu Dec 27 13:10:54 CST 2018
[root@foundation77 mnt]# bash test.sh
Thu Dec 27 13:10:57 CST 2018
/mnt/test.sh | ./test.sh 绝对路径 | 当前目录下
[root@foundation77 mnt]# ls -l test.sh
-rwxr--r--. 1 root root 17 Dec 27 13:00 test.sh
[root@foundation77 mnt]# ./test.sh
Thu Dec 27 13:51:24 CST 2018
[root@foundation77 mnt]# /mnt/test.sh
Thu Dec 27 13:51:28 CST 2018
source .sh | . script.sh sourec与. 执行脚本时,会把shell中的变量值或函数返回给父shell继续使用,如图中test.sh 中存在
cd /tmp 的一条内容,使用sh 执行使只会执行完毕后显示结果, sourec与. 执行后会保留脚本执行后的目录 tmp。
修改test.sh 内容如下。
#!/bin/bash
cd /tmp
echo hello
[root@foundation77 mnt]# vim test.sh
[root@foundation77 mnt]# sh test.sh
hello
[root@foundation77 mnt]# source test.sh
hello
[root@foundation77 tmp]# cd /mnt
[root@foundation77 mnt]# . test.sh
hello
[root@foundation77 tmp]#
3.定义变量与替换变量
变量用于保存有用信息,如路径,文件名,数字等,用户可以使用变量定制其工作环境。
变量赋值 :如下所示,给变量a赋值为hello ,b赋值为 obama-$a。
替换变量 :$ 符号为变量替换符号,a 为hello ,$a 就表示变量值。
(1 赋的值内部的空格必须用双引号括起来 (没有特别要求时,字符串都加双引号较好,否则赋值中存在空格时无法识别,需要原样输出就加单引号)
(2 变量只能包括大小写字母,数字,下划线 等特定符号,并且不能用数字开头,否则无效。
[root@foundation77 mnt]# a=hello
[root@foundation77 mnt]# echo $a
hello
[root@foundation77 mnt]# b=obama-$a
[root@foundation77 mnt]# echo $b
obama-hello
[root@foundation77 mnt]# echo $b $a
obama-hello hello
[root@foundation77 mnt]# 1=westos
bash: 1=westos: command not found...
[root@foundation77 mnt]# a1=westos
[root@foundation77 mnt]# echo $a1
westos
[root@foundation77 mnt]#
4. 特殊变量
$0:获取脚本文件名,如果执行时包含路径,则输出脚本路径
[root@foundation77 /]# cat /mnt/test.sh
#!/bin/bash
cd /tmp
echo hello
echo $0
[root@foundation77 /]# sh /mnt/test.sh
hello
/mnt/test.sh
[root@foundation77 /]# cd /mnt/
[root@foundation77 mnt]# sh test.sh
hello
test.sh
$n 传递参数给脚本变量
[root@foundation77 mnt]# cat test.sh
#!/bin/bash
echo $1 $2
[root@foundation77 mnt]# sh test.sh sos qvq
sos qvq
[root@foundation77 mnt]# sh test.sh hello obama
hello obama
[root@foundation77 mnt]#
[root@foundation77 mnt]# cat test.sh
echo $1 $2 $3 $4 $5 $6 $7 $8 $9 $10
[root@foundation77 mnt]# sh test.sh {1..10}
1 2 3 4 5 6 7 8 9 10
[root@foundation77 mnt]# sh test.sh {1..20}
1 2 3 4 5 6 7 8 9 10
[root@foundation77 mnt]# sh test.sh {a..z}
a b c d e f g h i a0
[root@foundation77 mnt]#
$#:传递到脚本的参数数量
[root@foundation77 mnt]# cat test.sh
#!/bin/bash
echo $#
[root@foundation77 mnt]# sh test.sh {a..z}
26
$* 和$@: 传递到脚本的所有参数
[root@foundation77 mnt]# cat test.sh
#!/bin/bash
echo $#
echo $*
[root@foundation77 mnt]# sh test.sh {a..z}
26
a b c d e f g h i j k l m n o p q r s t u v w x y z
[root@foundation77 mnt]#
$?:表示上条命令执行结果的返回值
0表示执行成功
非0表示有错误
将命令结果赋值给变量
[root@foundation77 mnt]# CMD=$(ls -l)
[root@foundation77 mnt]# echo $CMD
read.sh rhel-server-7.2-x86_64-dvd.iso sos.sh test.sh
read 命令的用法
基本读取
[root@foundation77 mnt]# read a
westos
[root@foundation77 mnt]# echo $a
westos
-p 可在命令行中输入提示符
[root@foundation77 mnt]# read -p "请输入密码:"
请输入密码:
[root@foundation77 mnt]# vim read.sh
[root@foundation77 mnt]# cat read.sh
#!/bin/bash
read -p "输入你的姓名:" a
echo “出门右转 $a”
exit 0
[root@foundation77 mnt]# sh read.sh
输入你的姓名:张
“出门右转 张”
[root@foundation77 mnt]#
-t 等待的时间 ,等待多久没有输入后退出,不会一直等待
[root@foundation77 mnt]# read -p "请输入密码:"
请输入密码:^C # 此时如果不输入内容会一直等待
[root@foundation77 mnt]# read -t 3 -p "请输入密码:" #只等待3秒,3秒后自动退出
请输入密码:[root@foundation77 mnt]#
-s 隐藏输入:如果输入的内容需要隐藏,可以加-s 不显示。
[root@foundation77 mnt]# read -p "请输入密码:"
请输入密码:1234
[root@foundation77 mnt]# read -s -p "请输入密码:"
请输入密码:[root@foundation77 mnt]#
测试网络
[root@foundation77 mnt]# cat test.sh
#!/bin/bash
ping -c1 -w1 172.25.254.$1 &> /dev/null && echo 172.25.254.$1 is up || echo 172.25.254.$1 is down
[root@foundation77 mnt]# sh test.sh 107
172.25.254.107 is up
[root@foundation77 mnt]# sh test.sh 177
172.25.254.177 is down