Shell 元字符 & 变量

Shell 介绍

## 什么是程序
程序就是一组数据和代码逻辑集合的文件

## 什么是进程
进程是程序的运行过程,也可以说是操作系统干活的过程,因为是操作系统负责控制硬件来运行应用程序

ps:进程与进程之间的内存空间是互相隔离的

## 计算机体系的三层结构
应用程序
操作系统
计算机硬件

## 什么是 shell
shell 是一门编程语言,用来与计算机沟通,从而控制计算机的

## 什么是shell解释器
用于解释执行 shell 语言语法 / 命令的一个应用软件
[root@Centos7 ~]# chsh -l
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash

## 什么是 shell script
用 shell 这门编程语言编写的程序

## 运行 shell 程序两种方式
方式一:交互式环境(登录用户之后默认就会进入用户的 shell 解释器交互式环境)
	优点:输入命令立即拿到代码的运行结果,即时运行
    缺点:无法永久保存代码,退出交互式环境,代码全部丢失
方式二:把代码写到文件中,然后运行???
	优点:可以永久保存代码,以便后期重复使用
    缺点:无法即时运行

元字符

什么是元字符?

元字符属于shell这门编程语言的语法,被 shell 解释器解释的特殊字符

ps:grep 命令解释的特殊符号,正则表达式,正则与元字符中的符号都是公用的,但是表示的意义截然不同

如何用元字符?

# 1、~:家目录
# 2、``:取命令的运行结果
$():支持嵌套
[root@Centos7 test]# res=$(ls $(pwd))
[root@Centos7 test]# echo $res
# 3、!
# 3.1 !历史命令
# 3.2 !$取上一条命令的参数
# 3.3 取反
对命令的结果取反:
[root@Centos7 test]# ! pwd
/test
[root@Centos7 test]# echo $?
1
[root@Centos7 test]# 
[root@Centos7 test]# find /test ! -name "*.txt"
[root@Centos7 test]# ls
11.txt  22.txt  33.txt  aa.txt  A.txt   b.txt   c.txt
1.txt   2.txt   3.txt   a.txt   bb.txt  cc.txt
[root@Centos7 test]# ls /test/[0-9].txt
/test/1.txt  /test/2.txt  /test/3.txt
[root@Centos7 test]# ls /test/[!0-9].txt
/test/a.txt  /test/A.txt  /test/b.txt  /test/c.txt
[root@Centos7 test]# 
[root@Centos7 test]# 
[root@Centos7 test]# ls /test/[^0-9].txt
/test/a.txt  /test/A.txt  /test/b.txt  /test/c.txt
[root@Centos7 test]# 

# 4、@:没有特殊意义
# 5、#:注释
原则:给关键代码加注释
地方:
	1、代码的正上方单独一行
    2、代码正后方
# 6、$
取变量的值:
x=111
echo $x
取命令的运行结果
echo $(pwd)
# 7、 +、-、*、/、%:加、减、乘、除、取余数
$[]
$(())
expr
let 
bc

# 示例
[root@Centos7 test]# n=10
[root@Centos7 test]# echo $[$n+1]
11
[root@Centos7 test]# echo $[$n-1]
9
[root@Centos7 test]# echo $[$n/3]
3
[root@Centos7 test]# 
[root@Centos7 test]# echo $[$n%3]
1
[root@Centos7 test]# echo $[$n*3]
30
[root@Centos7 test]# 
[root@Centos7 test]# echo $(($n+1))
11
[root@Centos7 test]# echo $(($n-1))
9
[root@Centos7 test]# echo $(($n/3))
3
[root@Centos7 test]# echo $(($n%3))
1
[root@Centos7 test]# expr $n + 1
11
[root@Centos7 test]# expr $n / 3
3
[root@Centos7 test]# 
[root@Centos7 test]# expr $n / 300
0
[root@Centos7 test]# expr $n+1  #  一定记得在运算符左右两侧加空格
10+1

# let 运算符
[root@Centos7 test]# age=18
[root@Centos7 test]# age=$[$age+1]
[root@Centos7 test]# echo $age
19
[root@Centos7 test]# let age=age+1
[root@Centos7 test]# echo $age
20
[root@Centos7 test]# let age+=1  # age=age+1
[root@Centos7 test]# echo $age
21


[root@Centos7 test]# let i++
[root@Centos7 test]# echo $i
1
[root@Centos7 test]# let ++j
[root@Centos7 test]# echo $j
1

[root@Centos7 test]# unset m
[root@Centos7 test]# unset n
[root@Centos7 test]# unset x
[root@Centos7 test]# unset y
[root@Centos7 test]# 
[root@Centos7 test]# let x=m++
[root@Centos7 test]# let y=++n
[root@Centos7 test]# 
[root@Centos7 test]# echo $x
0
[root@Centos7 test]# echo $y
1

[root@Centos7 test]# echo $(echo "scale=2;33/100" | bc |cut -d. -f2)%
33%




# 8、[]:代表匹配一个字符,该字符属于 [] 内规定的任意字符
[root@Centos7 test]# touch 1.txt
[root@Centos7 test]# touch 2.txt
[root@Centos7 test]# touch 3.txt
[root@Centos7 test]# touch a.txt
[root@Centos7 test]# touch b.txt
[root@Centos7 test]# touch c.txt
[root@Centos7 test]# 
[root@Centos7 test]# touch 11.txt
[root@Centos7 test]# touch 22.txt
[root@Centos7 test]# touch 33.txt
[root@Centos7 test]# touch aa.txt
[root@Centos7 test]# touch bb.txt
[root@Centos7 test]# touch cc.txt
[root@Centos7 test]# ls /test/[0-9].txt
/test/1.txt  /test/2.txt  /test/3.txt
[root@Centos7 test]# ls /test/[a-z].txt
/test/a.txt  /test/b.txt  /test/c.txt
[root@Centos7 test]# touch A.txt
[root@Centos7 test]# ls /test/[a-z].txt
/test/a.txt  /test/A.txt  /test/b.txt  /test/c.txt
[root@Centos7 test]# 
[root@Centos7 test]# 
[root@Centos7 test]# ls /test/[0-9][0-9].txt
/test/11.txt  /test/22.txt  /test/33.txt

[root@Centos7 test]# ls
a1b.txt  a2b.txt  a3b.txt  a-b.txt  a+b.txt
[root@Centos7 test]# 
[root@Centos7 test]# ls /test/a[1-+]b.txt
ls: cannot access /test/a[1-+]b.txt: No such file or directory
[root@Centos7 test]# ls /test/a[1+-]b.txt
/test/a1b.txt  /test/a-b.txt  /test/a+b.txt
[root@Centos7 test]# ls /test/a[!1+-]b.txt
/test/a2b.txt  /test/a3b.txt
[root@Centos7 test]# 
[root@Centos7 test]# ls /test/a[!-1+]b.txt
/test/a2b.txt  /test/a3b.txt

# 9、 ^ 与 !
都表示否定
[root@Centos7 test]# ls /test/a[!-1+]b.txt
/test/a2b.txt  /test/a3b.txt
[root@Centos7 test]# ls /test/a[^-1+]b.txt
/test/a2b.txt  /test/a3b.txt
# 10、 &
将进程放到后台执行(并行)
进程的运行状态:运行态、就绪态、阻塞态
  并发:看起来是同时运行的
  并行:真正意义上的同时运行,只有多核才有并行的可能性

# 11、* 任意多个字符
ls *.txt

# 12、() 在 子shell进程 中运行命令
[root@Centos7 ~]# echo $x

[root@Centos7 ~]# (x=1)
[root@Centos7 ~]# echo $x

[root@Centos7 ~]# 
[root@Centos7 ~]# umask
0022
[root@Centos7 ~]# (umask 666;touch {a..c}.txt)
[root@Centos7 ~]# touch d.txt
[root@Centos7 ~]# ll 
total 4
-rw-------. 1 root root 1523 Jun 29 23:38 anaconda-ks.cfg
----------  1 root root    0 Aug 25 18:53 a.txt
----------  1 root root    0 Aug 25 18:53 b.txt
----------  1 root root    0 Aug 25 18:53 c.txt
-rw-r--r--  1 root root    0 Aug 25 18:53 d.txt
[root@Centos7 ~]# 

# 13、_ 用来命名
[root@Centos7 ~]# tar czf /bak/`date +%F_%H:%M:%S`_etc.bak.gz /etc
tar: Removing leading `/' from member names
[root@Centos7 ~]# ls /bak/
2020-08-25_18:56:29_etc.bak.gz

# 14、= 赋值,== 判断是否相等
x=1
[ $x == 1 ];echo $?

# 15、| 与 xargs

# 16、 转义
[root@Centos7 ~]# echo $RMB

[root@Centos7 ~]# echo "$RMB"

[root@Centos7 ~]# echo "$RMB"
$RMB
[root@Centos7 ~]# echo '$RMB'
$RMB

# 17、{},批量创建文件,隔离变量
touch {1..9}.txt
touch {a..c}{1..3}.txt

[root@Centos7 ~]# 
[root@Centos7 ~]# num=30
[root@Centos7 ~]# echo $num
30
[root@Centos7 ~]# echo $num%
30%
[root@Centos7 ~]# echo $numRMB

[root@Centos7 ~]# echo ${num}RMB
30RMB


# 18、'',""

# 19、:,没啥用,但是属于正确命令
[root@Centos7 ~]# :
[root@Centos7 ~]# echo $?
0
[root@Centos7 ~]# true
[root@Centos7 ~]# echo $?
0

# 20、 ;、&&、||

# 21、? 匹配任意一个字符
[root@Centos7 test]# ls
1111.txt  11.txt  1.txt  2.txt  3.txt  aaaaa.txt
[root@Centos7 test]# ls *.txt
1111.txt  11.txt  1.txt  2.txt  3.txt  aaaaa.txt
[root@Centos7 test]# ls ?.txt
1.txt  2.txt  3.txt
[root@Centos7 test]# ls ??.txt
11.txt
[root@Centos7 test]# ls ???.txt
ls: cannot access ???.txt: No such file or directory
[root@Centos7 test]# ls ????.txt
1111.txt
[root@Centos7 test]# ls [].txt

Bash 解释器执行命令的优先级

# 输入一条命令时, Bash 解释器执行命令的优先级
- Alias
--- 复合命令(; && ||)
----- 函数
------- 内置命令(shell builtin)
--------- hash
----------- PATH
------------- 报错

运行 Shell 脚本的四种方式

  • 方式一:绝对路径,需要当前用户对脚本文件有 rx 权限
[root@Centos7 ~]# /scripts/sample/hello.sh
-bash: /scripts/sample/hello.sh: Permission denied
[root@Centos7 ~]# 
[root@Centos7 ~]# ll !$
ll /scripts/sample/hello.sh
-rw-r--r-- 1 root root 41 Aug 25 19:43 /scripts/sample/hello.sh
[root@Centos7 ~]# chmod +x !$
chmod +x /scripts/sample/hello.sh
[root@Centos7 ~]# /scripts/sample/hello.sh
hello world
  • 方式二:./脚本文件.sh,需要当前用户对脚本文件有 rx 权限
[root@Centos7 sample]# chmod o=x hello.sh 
[root@Centos7 sample]# ll hello.sh 
-rwxr-x--x 1 root root 41 Aug 25 19:43 hello.sh
[root@Centos7 sample]# su - zzzwqh
Last login: Tue Aug 25 19:48:57 CST 2020 on pts/0
[zzzwqh@Centos7 ~]$ cd /scripts/sample/
[zzzwqh@Centos7 sample]$ ./hello.sh 
bash: ./hello.sh: Permission denied
  • 方式三:指定解释器来解释执行脚本程序,需要当前用户对脚本文件有r权限 (解释:我们执行的是 Bash 命令,所有用户对 Bash 命令都有执行权限,所以我们只需要考虑脚本文件的读权限即可)
[root@Centos7 sample]# chmod o=- hello.sh 
[root@Centos7 sample]# 
[root@Centos7 sample]# su - zzzwqh
Last login: Tue Aug 25 19:49:33 CST 2020 on pts/0
[zzzwqh@Centos7 ~]$ cd /scripts/sample/
[zzzwqh@Centos7 sample]$ ll
total 4
-rwxr-x--- 1 root root 26 Aug 25 19:53 hello.sh
[zzzwqh@Centos7 sample]$ bash hello.sh 
bash: hello.sh: Permission denied
[zzzwqh@Centos7 sample]$ exit
logout
[root@Centos7 sample]# chmod o=x hello.sh 
[root@Centos7 sample]# su - zzzwqh
Last login: Tue Aug 25 19:53:31 CST 2020 on pts/0
[zzzwqh@Centos7 ~]$ !cd
cd /scripts/sample/
[zzzwqh@Centos7 sample]$ bash hello.sh 
bash: hello.sh: Permission denied
[zzzwqh@Centos7 sample]$ exit
logout
[root@Centos7 sample]# chmod o=r hello.sh 
[root@Centos7 sample]# su - zzzwqh
Last login: Tue Aug 25 19:54:07 CST 2020 on pts/0
[zzzwqh@Centos7 ~]$ !cd
cd /scripts/sample/
[zzzwqh@Centos7 sample]$ bash hello.sh 
hello world
[zzzwqh@Centos7 sample]$ 
  • 方式四:在当前 Bash 进程中运行(前三种方式都是在子 Bash 进程中运行)
[root@Centos7 ~]# echo $x

[root@Centos7 ~]# source /scripts/sample/hello.sh
hello world
[root@Centos7 ~]# cat !$
cat /scripts/sample/hello.sh
x=111
echo "hello world"

[root@Centos7 ~]# echo $x
111
[root@Centos7 ~]# unset x
[root@Centos7 ~]# 
[root@Centos7 ~]# . /scripts/sample/hello.sh
hello world
[root@Centos7 ~]# echo $x
111
[root@Centos7 ~]# 

变量介绍

# 1、什么是变量
量:记录事物的状态
变:事物的状态是可以发生变化的

# 2、为何要有变量
书面解释:变量是编程语言为我们提供的一种存取内存的机制
大白话:编程语言里之所有有变量这种语法是为了让计算机能够像人一样去记忆事物的状态

# 3、如何用变量
原则:先定义、后引用
x=1  # 等号左右两侧不要有空格

echo $x

# 注意:没有事先定义变量而取值,会取到空,但是不会报错
[root@Centos7 ~]# echo $x

[root@Centos7 ~]# x=111
[root@Centos7 ~]# echo $x
111
[root@Centos7 ~]# 

# 删除变量
[root@Centos7 ~]# x=111
[root@Centos7 ~]# echo $x
111
[root@Centos7 ~]# unset x
[root@Centos7 ~]# echo $x

[root@Centos7 ~]# 

变量名的命名

# 注意:变量名的命令应该见名知意,同时遵循如下规则
以字母或下划线开头,剩下的部分可以是:字母、数字、下划线,最好遵循下述规范:
    1.以字母开头
    2.使用中划线或者下划线做单词的连接
    3.同类型的用数字区分
    4.对于文件最好加上拓展名
例如: sql_bak.tar.gz,log_bak.tar.bz2  
    5、不要带有空格、?、*等特殊字符
    6、不能使用bash中的关键字,例如 if,for,while,do 等
    7、不要和系统环境变量冲突
    
# 示例
[root@Centos7 ~]# xxx=18
[root@Centos7 ~]# age=18
[root@Centos7 ~]# salary=18
[root@Centos7 ~]# num=18
[root@Centos7 ~]# 
[root@Centos7 ~]# gender="male"
[root@Centos7 ~]# ip="1.1.1.1"
[root@Centos7 ~]# 
[root@Centos7 ~]# 
[root@Centos7 ~]# age_of_zzzwqh=18
[root@Centos7 ~]# 
[root@Centos7 ~]# 
[root@Centos7 ~]# AgeOfzzzwqh=18
[root@Centos7 ~]# 

变量值的三种来源

  • 方式一,直接赋值
[root@Centos7 ~]# age=18
[root@Centos7 ~]# salary=3.1
[root@Centos7 ~]# name="zzzwqh"
[root@Centos7 ~]# 
[root@Centos7 ~]# name="zzzwqh"
[root@Centos7 ~]# echo $name
zzzwqh
[root@Centos7 ~]# 
  • 方式二,从键盘输入读取值来赋值给变量名
# -p 参数:指定提示信息
[root@Centos7 ~]# read -p "请输入你的操作>>>: " cmd
请输入你的操作>>>: start
[root@Centos7 ~]# echo $cmd
start

# -t 参数:指定超时时间
[root@Centos7 ~]# 
[root@Centos7 ~]# read -t3 -p ">>>: " x
>>>: [root@Centos7 ~]# echo $x

# -n 参数:指定读取的字符个数
[root@Centos7 ~]# read -n2 -p ">>>: " x
>>>: 11[root@Centos7 ~]# read -n2 -p ">>>: " x
>>>: ^C
[root@Centos7 ~]# read -n3 -p ">>>: " x
>>>: 111[root@Centos7 ~]# echo $x
111
[root@Centos7 ~]# 

# 练习题:
[root@Centos7 sample]# chmod +x login.sh 
[root@Centos7 sample]# ./login.sh 
请输入您的账号: xxx
请输入您的密码: 123
登录失败
[root@Centos7 sample]# ./login.sh 
请输入您的账号: zzzwqh
请输入您的密码: 123
登录成功
[root@Centos7 sample]# cat login.sh 
#!/bin/bash

db_user="zzzwqh"
db_pwd="123"

read -p "请输入您的账号: " username
read -p "请输入您的密码: " password

[ $username == $db_user -a $password == $db_pwd ] && echo "登录成功" || echo "登录失败"
  • 方式三,位置参数:$n
$1 $1 $2 $3..${10} ${12}


[root@Centos7 sample]# ./server.sh start
[root@Centos7 sample]# ./server.sh stop
[root@Centos7 sample]# ./server.sh reload
[root@Centos7 sample]# vim server.sh 
[root@Centos7 sample]# ./server.sh start
./server.sh
start





[root@Centos7 sample]#
[root@Centos7 sample]# cat server.sh 
#!/bin/bash

echo $1
echo $2
echo $3
echo $4
echo $5
echo $6
echo $7
echo $8
echo $9
echo ${10}
echo ${11}

[root@Centos7 sample]# ./server.sh 111 222 333 444 555 666 777 888 999 1000 2000
./server.sh
111
222
333
444
555
666
777
888
999
1000
2000

操作变量值

变量求长度

# 面试题
[root@Centos7 day02]# age=111
[root@Centos7 day02]# echo ${age}
111
[root@Centos7 day02]# echo ${#age}
3
[root@Centos7 day02]# echo $age | wc -L
3
[root@Centos7 day02]# echo $age | awk "{print length}"
3
[root@Centos7 day02]# 

变量的切片(了解)

[root@Centos7 day02]# msg="hello world"
[root@Centos7 day02]# echo ${msg}
hello world
[root@Centos7 day02]# echo ${msg:4}
o world
[root@Centos7 day02]# echo ${msg:4:3}
o w
[root@Centos7 day02]# echo ${msg::3}
hel
[root@Centos7 day02]# 

变量的截断

# 1、删除左边的
[root@Centos7 day02]# url="www.sina.com.cn"
[root@Centos7 day02]# echo ${url#www.}
sina.com.cn
[root@Centos7 day02]# echo ${url#*.}
sina.com.cn
[root@Centos7 day02]# echo ${url##*.}
cn
[root@Centos7 day02]# 
# 2、删除右边的
[root@Centos7 day02]# url="www.sina.com.cn"
[root@Centos7 day02]# echo ${url%.cn}
www.sina.com
[root@Centos7 day02]# echo ${url%.*}
www.sina.com
[root@Centos7 day02]# echo ${url%%.*}
www
[root@Centos7 day02]# echo ${url%%w*}

[root@Centos7 day02]# echo ${url%w*}
ww
[root@Centos7 day02]# 

# 3、示例
[root@www ~]# hostname
www.oldboy.com
[root@www ~]# echo $HOSTNAME
www.oldboy.com
[root@www ~]# echo ${HOSTNAME%%.*}
www
[root@www ~]# echo ${HOSTNAME#www.}
oldboy.com
[root@www ~]# 

变量的替代

# 变量值的替换
[root@www ~]# url="www.sina.com.cn"
[root@www ~]# echo ${url/./}
wwwsina.com.cn
[root@www ~]# echo ${url//./}  # 全部替换
wwwsinacomcn
[root@www ~]# echo ${url//./|}
www|sina|com|cn

# 应用
[root@www test]# ls
zzzwqh_2020_01_linux.txt  zzzwqh_2020_03_linux.txt  zzzwqh_2020_05_linux.txt
zzzwqh_2020_02_linux.txt  zzzwqh_2020_04_linux.txt
[root@www test]# 
[root@www test]# echo `ls /test/`
zzzwqh_2020_01_linux.txt zzzwqh_2020_02_linux.txt zzzwqh_2020_03_linux.txt zzzwqh_2020_04_linux.txt zzzwqh_2020_05_linux.txt
[root@www test]# for fname in `ls /test/`
> do
>     echo $fname
> done
zzzwqh_2020_01_linux.txt
zzzwqh_2020_02_linux.txt
zzzwqh_2020_03_linux.txt
zzzwqh_2020_04_linux.txt
zzzwqh_2020_05_linux.txt
[root@www test]# for fname in `ls /test/`; do mv $fname ${fname/_linux/}; done
[root@www test]# ls 
zzzwqh_2020_01.txt  zzzwqh_2020_02.txt  zzzwqh_2020_03.txt  zzzwqh_2020_04.txt  zzzwqh_2020_05.txt
[root@www test]# 


#1、${parameter-word}: 当调取变量没有定义过,就返回 word 字符串信息
[root@www ~]# unset x
[root@www ~]# echo ${x-111}
111
[root@www ~]# 
[root@www ~]# x=333
[root@www ~]# echo ${x-111}
333
[root@www ~]# x=
[root@www ~]# echo ${x-111}

[root@www ~]# 


#2、${parameter:-word}: 当调取变量没有定义过或者是定义过变量但是值为空, 就返回word字符串信息
[root@www ~]# unset x
[root@www ~]# echo ${x:-111}   # 变量从未定义过
111
[root@www ~]# 
[root@www ~]# x=333
[root@www ~]# echo ${x:-111}  # 变量定义过,并且值为333
333
[root@www ~]# x=
[root@www ~]# echo ${x:-111}  # 变量定义过,并且值为空
111
[root@www ~]# 

#3、{parameter:=word}:当调取变量信息值为空时或未定义,则设置指定字符串为新的变量值
[root@www ~]# unset x
[root@www ~]# echo ${x:=111}
111
[root@www ~]# echo $x
111
[root@www ~]# 
[root@www ~]# x=
[root@www ~]# echo ${x:=111}
111
[root@www ~]# echo $x
111
[root@www ~]# 
[root@www ~]# x=333
[root@www ~]# echo ${x:=111}
333
[root@www ~]# echo $x
333
[root@www ~]# 

#4、${parameter:?word}:当调取变量信息值为空时或未定义,指定为赋值的错误提示信息
[root@www ~]# unset x
[root@www ~]# echo ${x:?变量不存在傻叉}
-bash: x: 变量不存在傻叉
[root@www ~]# 
[root@www ~]# x=
[root@www ~]# echo ${x:?变量不存在傻叉}
-bash: x: 变量不存在傻叉
[root@www ~]# 
[root@www ~]# 
[root@www ~]# x=123
[root@www ~]# echo ${x:?变量不存在傻叉}
123
[root@www ~]# 
#5、${parameter:+word}:当调取变量信息值为空时或未定义,不做任何处理,否则 word 字符串将替代变量值
[root@www ~]# unset x
[root@www ~]# echo ${x:+111}  #  没有定义x,此时属于没有值的范畴

[root@www ~]# echo $x

[root@www ~]# x=
[root@www ~]# echo ${x:+111}  # 定义了x但是x的值为空,此时也属于没有值的范畴

[root@www ~]# echo $x

[root@www ~]# 
[root@www ~]# 
[root@www ~]# x=333
[root@www ~]# echo ${x:+111}  # x有值,有值则替代
111
[root@www ~]# echo $x # 替代不等于赋值
333

只读变量(基本用不到)

[root@www ~]# age=18
[root@www ~]# readonly age
[root@www ~]# 
[root@www ~]# age=19
-bash: age: readonly variable
[root@www ~]# expr $age + 1
19
[root@www ~]# age=`expr $age + 1`
-bash: age: readonly variable
[root@www ~]# 

预定义变量

$*:获取命令行传入的所有的位置参数
$@:获取命令行传入的所有的位置参数
# ps:当调用的脚本格式如下形式,必须使用"$@"
[root@www sample]# ./4.sh aaa bbb ccc "ddd eee"  # 注意"ddd eee"
    


$#:获取命令行传入的所有的位置参数的个数
$$: 获取当前进程的pid  # $PPID  $UID
$?: 获取上一条命令的执行状态,0代表成功,非0代表失败
    
# 示例1
[root@www sample]# cat 1.sh 
#!/bin/bash

echo $*
echo $@
echo $#
echo $$


pwd
echo $?
sleep 1000
[root@www sample]# 

    
# 示例2
[root@www sample]# ./2.sh 
当前进程的pid:9423
当前进程的ppid:9339
[root@www sample]# echo $$
9339
[root@www sample]# 

# 示例3:
[root@www sample]# cat 3.sh 
#!/bin/bash

[ $# == 2 ] && echo "参数个数正确" || echo "必须传入两个参数"
[root@www sample]# 

[root@www sample]# vim 3.sh
[root@www sample]# chmod +x 3.sh 
[root@www sample]# ./3.sh 
必须传入两个参数
[root@www sample]# ./3.sh 11
必须传入两个参数
[root@www sample]# ./3.sh 11 22
参数个数正确
[root@www sample]# 

 
 # 示例4:$*与$@的区别
[root@www sample]# cat 4.sh 
#!/bin/bash

for i in $*
do
    echo $i
done

echo "================"

for i in $@
do
    echo $i
done
[root@www sample]# 

 [root@www sample]# ./4.sh aaa bbb ccc "ddd eee"
aaa
bbb
ccc
ddd
eee
================
aaa
bbb
ccc
ddd
eee

 
 # 示例5:$*与$@的区别
[root@www sample]# cat 5.sh 
#!/bin/bash

#for i in "$*"
#do
#    echo $i
#done

echo "================"

for i in "$@"
do
    echo $i
done
[root@www sample]# 

[root@www sample]# ./5.sh aaa bbb ccc "ddd eee"
================
aaa
bbb
ccc
ddd eee

# 示例6:
[root@www sample]# cat ping.sh 
#!/bin/bash

for ip in $*
do
    ping -c1 $ip &>/dev/null
    [ $? == 0 ] && echo "$ip:up" || echo "$ip:down"

done
[root@www sample]# 
[root@www sample]# chmod +x ping.sh 
[root@www sample]# ./ping.sh 127.0.0.1 10.10.0.10 10.10.0.11 172.168.11.12
127.0.0.1:up
10.10.0.10:down
10.10.0.11:down
172.168.11.12:down

# 示例7
[root@www sample]# cat ping2.sh 
#!/bin/bash

for ip in $*
do
    (ping -c1 $ip &>/dev/null ; [ $? == 0 ] && echo "$ip:up" >> ping.log || echo "$ip:down" >> ping.log) &

done
[root@www sample]# 


记录成长过程
原文地址:https://www.cnblogs.com/zzzwqh/p/13560660.html