|
本节所讲内容:
1、 shell 基本语法
2、 变量
3、 表达式
4、 判断语句
5、 if表达式
一、什么叫shell
【例】先看一个简单的shell程序
[root@hpc test]# vim example01.sh #写入以下内容
#!/bin/bash
#This is to show what a example looks like.
echo "Our first example"
echo # This inserts an empty line in output.
echo "We are currently in the following directory."
pwd
echo
echo "This directory contains the following files"
ls
[root@hpc test]# chmod +x example01.sh
[root@hpc test]# ./example01.sh
Our first example
We are currently in the following directory.
/root/test
This directory contains the following files
example01.sh
shell就是系统跟计算机硬件交互时使用的中间介质,它只是系统的一个工具。实际上,在shell和计算机硬件之间还有一层东西那就是系统内核了。打个比方,如果把计算机硬件比作一个人的躯体,而系统内核则是人的大脑,至于shell,把它比作人的五官似乎更加贴切些。回到计算机上来,用户直接面对的不是计算机硬件而是shell,用户把指令告诉shell,然后shell再传输给系统内核,接着内核再去支配计算机硬件去执行各种操作。
1.1 shell编程
编程语言:
1.机器语言
2.汇编语言
3.高级语言
(1)静态语言Dynamically Typed Language:编译型语言 c c++ java
(2)动态语言Statically Typed Language:解释型语言 php shell python perl
编译器:(解释器) 将人类理解的语言翻译成机器理解的语言
弱类型:变量随时用随时声明
强类型:变量在使用前,必须事先声明
其中shell是弱类型的编程语言
#!/bin/bash #!跟shell命令的完全路径。作用:显示后期命令以哪种shell来执行这些命令。如不指shell,以当前shell作为执行的shell。
[root@hpc test]# ll /bin/sh #查看/bin/sh的软连接
lrwxrwxrwx. 1 root root 4 Dec 18 2012 /bin/sh -> bash
以shell中以#开始头表示,整个行就被当作一个注释。执行时被忽略。
shell程序一般以.sh结尾,当然主要是看是否为执行,然后看里面有没有#!
1.2 总结
§创建shell程序的步骤:
§ 第一步:创建一个包含命令和控制结构的shell文件。
§ 第二步:修改这个文件的权限使它可以执行。使用chmod u+x
§ 第三步:执行
方法1:./example01.sh
方法2: 使用绝对路径 [root@hpc test]# /root/test/example01.sh
方法3:[root@hpc test]# bash example01.sh
方法4:[root@hpc test]#source/sh example01.sh #注:example01.sh可以不用x权限
二、shell变量及运用
1 shell变量
变量是shell 传递数据的一种方法。变量是用来代表每个值的符号名。
例: x=3
Shell 有两类变量:临时变量和永久变量。
1.1临时变量:是shell 程序内部定义的,其使用范围仅限于定义它的程序,对其它程序不可见。
1.2永久变量:是环境变量,其值不随shell 脚本的执行结束而消失。
【例】$PATH
[root@hpc test]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/root/bin
#用作运行某个命令的时候,本地查找不到某个命令或文件,会到这个声明的目录中去查找。
1.3用户定义变量:由字母或下划线打头,不允许数字开头,后面由字母、数字或下划线组成,并且大小写字母意义不同。变量名长度没有限制。
使用变量值时,要在变量名前加上前缀“$”。
例如:1VAR 是非法变量。
2 主要赋值类型
2.1 变量赋值 赋值号“=”两边应没有空格。
【例】 用“=”对变量赋值
[root@hpc test]# A=aaa
[root@hpc test]# A = aaa
bash: A: command not found
2.2将一个命令的执行结果赋给变量
[root@hpc test]# A=`date`
[root@hpc test]# echo $A
Sat Feb 7 20:54:26 CST 2015
【例】 用“=”对命令赋值
[root@hpc test]# B=$(ls -l)
[root@hpc test]# echo $B
[root@hpc test]# A=$B
[root@hpc test]# echo $A
2.3可以利用变量和其它字符组成一个新的字符串
[root@hpc test]# MYDIR=/home/mk
[root@hpc test]# echo $MYDIR/zhangsan
/home/mk/zhangsan
[root@hpc test]# DAY=mon
[root@hpc test]# echo Today is $DAYday
Today is
[root@hpc test]# echo Today is $DAY day
Today is mon day
[root@hpc test]# echo Today is ${DAY}day
Today is monday
2.4 给变量赋值多个单词
[root@hpc test]# NAME="mike Ron"
[root@hpc test]# echo $NAME
mike Ron
[root@hpc test]# NAME='shen mk'
[root@hpc test]# echo $NAME
shen mk
【例】 赋值时“”和’’的区别
[root@hpc test]# NAME="mike Ron $NAME"
[root@hpc test]# echo $NAME
mike Ron shen mk
[root@hpc test]# NAME='mike Ron $NAME'
[root@hpc test]# echo $NAME
mike Ron $NAME
总结:单引号:之间的内容原封不动地指定给了变量。
双引号:特殊符号的含义保留。
【例】 变量与符号的综合演示
[root@hpc shell]# a=192.168.1.63
[root@hpc shell]# b='192.168.1.63'
[root@hpc shell]# c="192.168.1.63"
[root@hpc shell]# echo "a=$a"
a=192.168.1.63
[root@hpc shell]# echo 'b=$b'
b=$b
[root@hpc shell]# echo "c=${c}" #c=${c}等同c=$c
c=192.168.1.63
2.5 把命令当作变量进行定义
[root@hpc shell]# wen=`date +%F` #利用反引号进行定义
[root@hpc shell]# echo $wen
2015-02-15
[root@hpc shell]# wen=$(date) #利用小括号加$进行定义
[root@hpc shell]# echo $wen
Sun Feb 15 12:42:30 CST 2015
3 对变量的管理
3.1列出所有的变量
set 命令,例:
[root@hpc test]# set | grep DAY
DAY=mon
3.2删除变量
[root@hpc test]# echo $NAME
mike Ron $NAME
[root@hpc test]# unset NAME
[root@hpc test]# echo $NAME
4位置变量和特殊变量
4.1 位置变量
Shell解释执行用户的命令时,将命令行的第一个字作为命令名,而其它字作为参数。由出现在命令行上的位置确定的参数称为位置参数。
使用$N 来表示
$0 获取当前执行shell脚本的文件文件名,包括脚本路径
$n 获取当前脚本的第n个参数 n=0,1,2.....n 当n大于9时 用{n}表示。
【例】快速生成$1-$9
方法一:
[root@hpc shell]# seq 9|sed 's#[0-9]#$&#g'>nsh #seq 9生成1-9之间的数,&为前面需要替换的字符
$1
$2
$3
$4
$5
$6
$7
$8
$9
方法二:
[root@hpc shell]# seq -s " $" 1 9|sed 's#1#$1#'>n.sh #-s为指定分隔符后面的替换为seq -s " $" 1 9生成的第一个数为没$符的所以应替换
$1 $2 $3 $4 $5 $6 $7 $8 $9
[root@hpc shell]# sed -i 's@$1@echo &@ ' n.sh
执行:[root@hpc shell]# sh n.sh `seq -s "" 9` #可快速赋值
1 2 3 4 5 6 7 8 9
4.2 特殊变量
有些变量是一开始执行Script脚本时就会设定,且不能被修改,但我们不叫它只读的系统变量,而叫它特殊变量。这些变量当一执行程序时就有了,以下是一些等殊变量:
$* 这个程序的所有参数
$# 这个程序的参数个数
$$ 这个程序的PID
$! 执行上一个后台程序的PID
$? 执行上一个指令的返回值
【例】 [
root@hpc shell]# cat n.sh
echo $1 $2 $3 $4 $5 $6 $7 $8 $9
echo $* : $# : $@
执行结果:[root@hpc shell]# sh n.sh `seq 9`
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 : 9 : 1 2 3 4 5 6 7 8 9
【例】分别取出路径和文件名
分析:利用$0得到当前执行的路径 分别进行输出
脚本:[root@hpc shell]# cat 0.sh
echo $0
dirname $0
basename $0
执行结果:[root@hpc shell]# sh /shell/0.sh
/shell/0.sh #执行的全路径
/shell #执行的目录
0.sh #执行的文件名
【例】位置变量在服务启动与停止中的应用
[root@hpc shell]# vim /etc/init.d/vsftpd
case "$1" in #相当于执行/etc/init.d/vsftpd $1
start)
start #当选择start时 调用start函数来启动服务
;;
stop)
stop #当选择stop时 调用stop函数来停止服务
;;
restart|reload) #当选择restart或者reload时 先停止服务 在启动服务
stop
start
RETVAL=$?
;;
echo $"Usage: $0 {start|stop|restart|try-restart|force-reload|status}" #当输入的$1不存在时利用$0 来打印当前的执行脚本的方法
补充:关于$?返回值的主要含义
$0
运行成功
$2
权限被拒绝
$1--$125
运行失败,命令错误或者参数错误
$126
找到命令,但是不能执行
$127
未找到改命令
$128
命令被系统强制结束
5小综合实例
5.1变量在shell中的使用
[root@hpc test]# cat z1.sh
#!/bin/bash #写入如下
var1="abcd efg"
echo $var1
var2=1234
echo "The value of var2 is $var2"
echo $HOME
echo $PATH
echo $PWD
执行结果:
[root@hpc test]# ./z1.sh
abcd efg
The value of var2 is 1234
/root
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/root/bin
/root/test
5.2 特殊变量的测试
[root@hpc test]# cat z.sh
#!/bin/bash #写入如下
echo "$* 表示这个程序的所有参数 "
echo "$# 表示这个程序的参数个数"
touch /tmp/a.txt
echo "$$ 表程序的进程ID "
touch /tmp/b.txt &
echo "$! 执行上一个后台指令的PID"
echo "$$ 表程序的进程ID "
执行结果:
[root@hpc test]# ./z.sh aaa bbb ccc
aaa bbb ccc 表示这个程序的所有参数
3 表示这个程序的参数个数
3899 表程序的进程ID
3901 执行上一个后台指令的PID
3899 表程序的进程ID
6 变量的数值计算常用的命令:(())
6.1 (())用于执行简单的整数运算
格式:$((算数运算符))
6.2 常用的算数运算符
运算符
意义
++ ——
递增及递减,可前置也可以后置
+ — ! ~
一元运算的正负号 逻辑与取反
+ — * / %
加减乘除与余数
=
比较大小符号
== !=
相等 不相等
>>2))
0
[root@hpc shell]# echo $((1/dev/null #将所有执行信息重定向到null中
[ $? -eq 0 ] && echo "int" || echo "input erro"#根据expr执行的返回值来判断是否为整数
学神-IT-教育51cto技术交流群:468845589快来上我们公开课吧!
学神MK老师:1273815479
学神ZY老师:3054384936
学神-IT-教育学神-IT-1508班-公瑾同学整理提供
|
|
|
|
|
|
|