shell 基本语法

本节所讲内容:
shell 基本语法
变量
表达式
判断语句
if表达式

先看一个简单的shell程序
[root@xuegod60 ~]# vim example01.sh
#!/bin/bash
#This is to show what a example looks like.
echo "Our first example"
echo #"This file contain a empty line"
echo "We are currently in the following directroy"
pwd
echo "This directory contains the following files"
ls

[root@xuegod60 ~]# chmod +x example01.sh
[root@xuegod60 ~]# ./example01.sh
Our first example

We are currently in the following directroy
/root
This directory contains the following files
anaconda-ks.cfg Documents example01.sh Music Public Videos
Desktop Downloads initial-setup-ks.cfg Pictures Templates

注释:
#!/bin/bash #!跟shell命令的完全路径。 作用:显示后期命令以哪种shell来执行这些命令。如不指shell,以当前shell作为执行的shell。
#This is to show what a example looks like. 在shell中以#开始头表示,整个行就被当作一个注释。执行时被忽略。
shell程序一般以.sh结尾

总结:
 创建shell程序的步骤:
 第一步:创建一个包含命令和控制结构的shell文件。
 第二步:修改这个文件的权限使它可以执行。
 使用chmod u+x
 第三步:执行
方法1:./example01.sh
方法2: 使用绝对路径 [root@xuegod63 test]# /root/test/example01.sh
方法3:[root@xuegod63 test]# bash example01.sh
[root@xuegod60 ~]# ll /bin/sh
lrwxrwxrwx. 1 root root 4 Jan 16 03:08 /bin/sh -> bash
bash sh source 一样可以执行脚本文件,执行时不需要给文件可执行权限

shell变量
变量是shell 传递数据的一种方法。变量是用来代表每个值的符号名
[root@xuegod60 ~]# x=3
[root@xuegod60 ~]# echo x 3 Shell 有两类变量:临时变量和永久变量。 临时变量:是shell 程序内部定义的,其使用范围仅限于定义它的程序,对其它程序不可见。 永久变量是环境变量,其值不随shell 脚本的执行结束而消失。 [root@xuegod60 ~]# echoPATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@xuegod60 ~]# echo $HOME
/root

扩展:
自定义环境变量
[root@xuegod60 ~]# mv example01.sh /etc/sysconfig/
[root@xuegod60 ~]# export PATH=/etc/sysconfig:PATH
/etc/sysconfig:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

任务:百度一下如何永久修改环境变量

这种方式一旦终端关闭后,就会失效,要想修改永久生效,可以修改用户目录下. bashrc
在此文件的最后一行,添加export PATH=/etc/sysconfig:$PATH,保存退出
[root@localhost ~]# source .bashrc
不报错,则成功

用户定义变量:由字母或下划线打头。 由字母、数字或下划线组成,并且大小写字母意义不同。变量名长
度没有限制。
使用变量值时,要在变量名前加上“$”
[root@xuegod60 ~]# 123=abc
bash: 123=abc: command not found...

变量赋值:赋值号“=”两边应没有空格。
[root@xuegod60 ~]# a = 1
bash: a: command not found...

[root@xuegod60 ~]# name=rm
[root@xuegod60 ~]# NAME=mk
[root@xuegod60 ~]# echo name
rm

例:将一个命令的执行结果赋给变量
[root@xuegod60 ~]# A=date
[root@xuegod60 ~]# echo $A
Tue Mar 8 21:12:45 CST 2016

[root@xuegod60 ~]# B=B
Tue Mar 8 21:13:33 CST 2016

变量值的可传递性
[root@xuegod60 ~]# A=date
[root@xuegod60 ~]# C=C
Tue Mar 8 21:12:45 CST 2016

可以利用变量和其它字符组成一个新的字符串
[root@xuegod60 ~]# MYDIR=/home/rm/
[root@xuegod60 ~]# echo MYDIRzhangsan
输出空内容
[root@xuegod60 ~]# echo ${MYDIR}zhangsan
/home/rm/zhangsan

给变量赋值多个单词:
[root@xuegod60 ~]# NAME="rm mk docker"
[root@xuegod60 ~]# echo $NAME
rm mk docker

[root@xuegod60 ~]# NAME="rm NAME
rm rm mk docker

[root@xuegod60 ~]# NAME='rm NAME
rm $NAME

单引号和双引号的区别:
 单引号之间的内容原封不动地指定给了变量。
双引号取消了空格的作用,特殊符号的含义保留。

[root@xuegod60 ~]# set

删除变量:
[root@xuegod60 ~]# echo NAME
[root@xuegod60 ~]# unset NAME
[root@xuegod60 ~]# echo $NAME

位置变量和特殊变量
位置变量:Shell解释执行用户的命令时,将命令行的第一个字作为命令名,而其它字作为参数。由出现在命令行上的位置确定的参数称为位置参数。
位置变量:使用1"
echo "第二个目录是$2"

[root@xuegod60 ~]# chmod +x b.sh
[root@xuegod60 ~]# ./b.sh /home/ /etc/
第一个目录是/home/
第二个目录是/etc/

n 这个程序的第n个参数值,n=1..N

特殊变量:
有些变量是一开始执行Script脚本时就会设定,且不能被修改,但我们不叫它只读的系统变量,而叫它特殊变量。这些变量当一执行程序时就有了,以下是一些等殊变量:
# 这个程序的参数个数
$$ 这个程序的PID
? 执行上一个指令的返回值

[root@xuegod60 ~]# vim z.sh
#!/bin/bash
echo "# 表示这个程序参数的个数"

touch /root/a.txt
echo "$$ 表示这个程序的PID"

touch /root/b.txt &
echo "$! 表示上一个后台指令的PID"
echo "$$ 表示程序的进程PID"

[root@xuegod60 ~]# ./z.sh aaa bbb ccc
aaa bbb ccc 表示这个程序的所有参数
3 表示这个程序参数的个数
39839 表示这个程序的PID
39841 表示上一个后台指令的PID
39839 表示程序的进程PID

例: 变量在shell中的使用
[root@xuegod60 ~]# vim z1.sh
#!/bin/bash
var1="abcd efg"
echo var2"
echo PATH
echo $PWD

[root@xuegod60 ~]# chmod +x z1.sh
[root@xuegod60 ~]# ./z1.sh
abcd efg
The value of var2 is 1234
/root
/etc/sysconfig:/etc/sysconfig:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
/root

Read命令:
作用:从键盘读入数据,赋给变量
[root@xuegod60 ~]# read a b c
1 22 33
[root@xuegod60 ~]# echo b $c
1 22 33

和-p -n -t参数结合使用
-p(提示语句)
-n(字符个数)
-t(等待时间)
[root@xuegod60 ~]# read -p "请输入你的准考证号: " ID
请输入你的准考证号: 20160308
[root@xuegod60 ~]# echo $ID
20160308

[root@xuegod60 ~]# vim read.sh
#!/bin/bash
read -p "请输入你的姓名:" NAME
read -p "请输入你的年龄:" AGE
read -p "请输入你的性别:" SEX

echo "你的基本信息如下:"
echo "姓名:AGE"
echo "性别:$SEX"

[root@xuegod60 ~]# chmod +x read.sh
[root@xuegod60 ~]# ./read.sh
请输入你的姓名:孙悟空
请输入你的年龄:500
请输入你的性别:男
你的基本信息如下:
姓名:孙悟空
年龄:500
性别:男

expr 命令
作用:Shell变量的算术运算:
 expr命令:对整数型变量进行算术运算
语法: expr 表达式 #注意 运算符之间要有空格
[root@xuegod60 ~]# expr 3 + 2
5
[root@xuegod60 ~]# expr 8 - 3
5
[root@xuegod60 ~]# expr 8 / 4
2
[root@xuegod60 ~]# expr 8 * 4
32
[root@xuegod60 ~]# expr 8 % 3
2
注:%取余运算

结合变量使用运算符
[root@xuegod60 ~]# var1=16
[root@xuegod60 ~]# var2=4
[root@xuegod60 ~]# expr var2
12

算术运算在脚本中的使用
[root@xuegod60 ~]# vim expr.sh
#!/bin/bash
read -p "Please input the value of a " a
read -p "Please input the value of b " b
read -p "Please input the value of c " c

value1=expr $a + $b + $c
echo "The value of value1 is c / value2"
value3=expr $c \* $b
echo "The value of value3 is a + b`
echo "The value of value4 is $value4"

[root@xuegod60 ~]# ./expr.sh
Please input the value of a 10
Please input the value of b 20
Please input the value of c 30
The value of value1 is 60
The value of value2 is 1
The value of value3 is 600
The value of value4 is 11

复杂运算
[root@xuegod60 ~]# expr (expr 5 + 11) / $var3) + 4
12

你可能感兴趣的:(shell 基本语法)