1. shell
- 命令行解释器
- 编程语言
2. 其他符号
‘’ 单引号
在单引号中所有的特殊符号,如 “$” 和 “`” 都没有特殊含义。
“” 双引号
在双引号中特殊符号都没有特殊含义,但是 “$” 和 “`” 和 “” 是例外。拥有“调用变量的值”、“引用命令”和“转义符”的特殊含义。
[admin@host ~]$ name=xiaoyu
[admin@host ~]$ echo '$name'
$name
[admin@host ~]$ echo "$name"
xiaoyu
符号 | 含义 |
---|---|
# | 代表注释 |
$ | 用于调用变量的值,如需要调用变量name的值时,需要用$name的方式得到变量的值。 |
\ | 转义符,跟在\之后的特殊符号将失去特殊含义,变为普通字符。 |
`` | 反引号。 |
$() | $()和反引号作用一样,用来引用系统命令。 |
[admin@host ~]$ echo `ls`
auto-add-tomcat auto-add-tomcat.tar.gz bin cat_file file1 file1.txt file2 file2.txt file.txt hello.sh home join_file1.txt join_file2.txt 196 list list_error list_right logdelete.sh regular_express.txt rootfile sort_file.txt temp.txt test.txt tools uniq_file.txt xaa xab xac
[admin@host ~]$ echo $(ls)
auto-add-tomcat auto-add-tomcat.tar.gz bin cat_file file1 file1.txt file2 file2.txt file.txt hello.sh home join_file1.txt join_file2.txt 196 list list_error list_right logdelete.sh regular_express.txt rootfile sort_file.txt temp.txt test.txt tools uniq_file.txt xaa xab xac
3. 用户自定义变量和环境变量
符号 | 含义 |
---|---|
环境变量 | 这种变量中主要保存的是和系统操作环境相关的数据。 |
位置参数变量 | 这种变量主要是用来向脚本当中传递参数或数据的,变量名不能自定义,变量作用是固定的。 |
预定义变量 | 是bash中已经定义好的变量,变量名不能自定义,变量作用也是固定的。 |
3.1. 用户自定义变量
3.1.1. 变量设置
[admin@host ~]$ name=jinworld
3.1.2. 变量查看
[admin@host ~]$ set
3.1.3. 变量删除
[admin@host ~]$ unset name
3.2. 环境变量
用户自定义变量只在当前的shell中生效,而环境变量会在当前shell和这个shell的所
有子shell当中生效。如果把环境变量写入相应的配置文件,那么这个环境变量就会在所有的shell中生效。
进程树 pstree
3.2.1. 设置环境变量
符号 | 含义 |
---|---|
export 变量名=变量值 | - |
env | 查询环境变量 |
set | 查询所有变量 |
unset 变量名 | 删除变量 |
3.2.2. 常见环境变量
PATH 系统查找命令的路径
3.3. 位置参数变量
符号 | 含义 |
---|---|
$n | $0代表命令本身,$1- 9 代 表 第 1 到 第 9 个 参 数 , 10 以 上 的 参 数 需 要 用 大 括 号 包 含 , 如 9代表第1到第9个参数,10以上的参数需要用大括号包含,如 9代表第1到第9个参数,10以上的参数需要用大括号包含,如{10} |
$* | $*把所有的参数看成一个整体 |
$@ | 代表命令行中所有的参数,不过$@把每个参数区分对待 |
$# | 代表命令行中所有参数的个数 |
3.3.1. $n
[admin@host ~]$ cat example.sh
#!/bin/bash
num1=$1
num2=$2
sum=$(($num1+$num2))
#变量sum的和是num1加num2
echo $sum
[admin@host ~]$ sh example.sh 2 3
5
3.3.2. $# $* $@
[admin@host ~]$ cat example.sh
#!/bin/bash
echo "A total of $# parameters"
#使用$#代表所有参数的个数
echo "The parameters is: $*"
#使用$*代表所有的参数
echo "The parameters is: $@"
#使用$@也代表所有参数
[admin@host ~]$ sh example.sh 3 4 5 6
A total of 4 parameters
The parameters is: 3 4 5 6
The parameters is: 3 4 5 6
3.3.3. $* $@
[admin@host ~]$ cat example.sh
#!/bin/bash
for i in "$*"
# $*中的所有参数看成是一个整体,所以这个for循环只会循环一次
do
echo "The parameters is: $i"
done
x=1
for y in "$@"
# $@中的每个参数都看成是独立的,所以“$@”中有几个参数,就会循环几次
do
echo "The parameter$x is: $y"
x=$(( $x +1 ))
done
[admin@host ~]$ sh example.sh 6 7 8 9
The parameters is: 6 7 8 9
The parameter1 is: 6
The parameter2 is: 7
The parameter3 is: 8
The parameter4 is: 9
4. 脚本
$ echo [选项] [输出内容]
符号 | 含义 |
---|---|
-e | 支持反斜线控制的字符转换 |
\ | 输出\本身 |
\a | 输出警告音 |
\b | 退格键,也就是向左删除键 |
\c | 取消输出行末的换行符。和 “-n” 选项一致 |
\e | ESCAPE键 |
\f | 换页符 |
\n | 换行符 |
\r | 回车键 |
\t | 制表符,也就是Tab键 |
\v | 垂直制表符 |
\0nnn | 按照八进制ASCII码表输出字符。其中0为数字零,nnn是三位八进制数 |
\xhh | 按照十六进制ASCII码表输出字符。其中hh是两位十六进制数 |
-e 支持反斜线控制的字符转换
\b 删除左侧字符
[admin@host ~]$ echo -e "ab\bc"
ac
\t 制表符
[admin@host ~]$ echo -e "a\tb\tc\td\te"
a b c d e
\xhh 按照16进制ASCII码表输出字符
[admin@host ~]$ echo -e "\x61\t\x62\t\x63\t\x64"
a b c d
\e 删除右侧字符
[admin@host ~]$ echo -e "\eabcd"
bcd
4.1. 第一个脚本
[admin@host ~]$ cat hello.sh
#!/bin/bash
echo -e "hello, world"
[admin@host ~]$ sh hello.sh
hello, world
-A 显示隐藏字符
[admin@host ~]$ cat -A hello.sh
#!/bin/bash$
echo -e "hello, world"$
5. 预定义变量
$?最后一次执行的命令的返回状态
如果这个变量的值为0,证明上一个命令正确执行;如果这个变量的值为非0,则证明上一个命令执行不正确。
[admin@host ~]$ echo $?
0
[admin@host ~]$ cat hu
cat: hu: 没有那个文件或目录
[admin@host ~]$ echo $?
1
$$ 当前进程的进程号
[admin@host ~]$ echo $$
116314
$! 后台运行的最后一个进程的进程号
[admin@host ~]$ echo $!
& 把命令放入后台执行
[admin@host ~]$ cat example.sh
#!/bin/bash
echo "The current process is $$"
find /root -name hello.sh &
echo "The last one Daemon process is $!"
[admin@host ~]$ sh example.sh
The current process is 10414
The last one Daemon process is 10415
6. 键盘输入
$ read [选项] [变量名]
符号 | 含义 |
---|---|
-p | 提示信息 |
-t | 秒数 |
-n | 字符数 |
-s | 隐藏输入的数据 |
abcde | abcde |
[admin@host ~]$ cat example.sh
#!/bin/bash
# -t 等待30s -p 提示信息
read -t 30 -p "Please input your name: " name
echo "Name is $name "
# -s 隐藏输入数据
read -s -t 30 -p "Please enter your age: " age
echo -e "\nAge is $age "
read -n 1 -t 30 -p "Please select your gender[M/F]: " gender
echo -e "\nGender is $gender "
[admin@host ~]$ sh example.sh
Please input your name: xiaoyu
Name is xiaoyu
Please enter your age:
Age is 26
Please select your gender[M/F]: M
Gender is M
7. 数值运算与运算符
7.1. declare声明变量类型
[root@localhost ~]# declare [+/-][选项] 变量名
符号 | 含义 |
---|---|
+ | 给变量设定类型属性 |
-i | 取消变量的类型属性 |
-x | 将变量声明为环境变量 |
-p | 显示指定变量的被声明的类型 |
abcde | abcde |
[admin@host ~]$ declare -i b=5
[admin@host ~]$ declare -p b
declare -i b="5"
7.2. 方法1
[admin@host ~]$ aa=11
[admin@host ~]$ bb=22
[admin@host ~]$ declare -i cc=$aa+$bb
[admin@host ~]$ echo $cc
33
7.3. 方法2 expr
[admin@host ~]$ aa=11
[admin@host ~]$ bb=22
[admin@host ~]$ dd=$(expr $aa + $bb)
[admin@host ~]$ echo $dd
33
7.4. 方法3 $((运算式)) 或 $[运算式]
[admin@host ~]$ aa=11
[admin@host ~]$ bb=22
[admin@host ~]$ ff=$(($aa+$bb))
[admin@host ~]$ gg=$[$aa+$bb]
[admin@host ~]$ echo $ff
33
[admin@host ~]$ echo $gg
33
8. 变量测试与内容替换
x=${y-新值}
[admin@host ~]$ unset y
[admin@host ~]$ x=${y-new}
[admin@host ~]$ echo $x
new
[admin@host ~]$ y=""
[admin@host ~]$ x=${y-new}
[admin@host ~]$ echo $x
[admin@host ~]$
[admin@host ~]$ y=old
[admin@host ~]$ x=${y-new}
[admin@host ~]$ echo $x
old
9. if
9.1. 单分支if
if []
then
fi
[admin@host ~]$ cat example.sh
#!/bin/bash
if [ 23 -ge 22 ]
then
echo "hello world!"
fi
[admin@host ~]$ sh example.sh
hello world!
9.2. 双分支if
[admin@host ~]$ cat example.sh
#!/bin/bash
if [ 23 -le 22 ]
then
echo "hello world!"
else
echo "hello world!"
fi
[admin@host ~]$ sh example.sh
hello world!
9.3. 多分支if
if []
then
elif
then
elif
then
fi
case
[admin@host ~]$ cat example.sh
#!/bin/bash
read -p "Please enter yes/no: " -t 30 choose
case $choose in
"yes")
echo "Your choose is yes"
;;
"no")
echo "Your choose is no"
;;
*)
echo "Your choose is error"
;;
esac
[admin@host ~]$ sh example.sh
Please enter yes/no: yes
Your choose is yes
[admin@host ~]$ sh example.sh
Please enter yes/no: no
Your choose is no
[admin@host ~]$ sh example.sh
Please enter yes/no: what
Your choose is error
10. for
10.1. 语法一
[admin@host ~]$ cat example.sh
#!/bin/bash
aa=1
bb=2
cc=3
for i in $aa $bb $cc
do
echo $i
done
[admin@host ~]$ sh example.sh
1
2
3
10.2. 语法二
[admin@host ~]$ cat example.sh
#!/bin/bash
s=0
for((i=1;i<=100;i=i+1))
do
s=$(($s+$i))
done
echo "The sum of 1+2+3+...+100 is: $s"
[admin@host ~]$ sh example.sh
The sum of 1+2+3+...+100 is: 5050
11. while
[admin@host ~]$ cat example.sh
#!/bin/bash
i=1
s=0
while [ $i -le 100 ]
do
s=$(($s+$i))
i=$(($i+1))
done
echo "The sum is: $s"
[admin@host ~]$ sh example.sh
The sum is: 5050
12. until
[admin@host ~]$ cat example.sh
#!/bin/bash
i=1
s=0
until [ $i -gt 100 ]
do
s=$(($s+$i))
i=$(($i+1))
done
echo "The sum is: $s"
[admin@host ~]$ sh example.sh
The sum is: 5050