一,hello shell
变量名外面的花括号是可选的,加不加都行,加花括号是为了帮助解释器识别变量的边界
例:
实例证明:
vi hello.sh
写如下内容:
linux@ubuntu:~/test_shell$ cat hello.sh
#!/bin/bash
echo "hello shell!"
注意这时的.sh文件没有执行权限,要更改为可执行状态
linux@ubuntu:~/test_shell$ ls -l hello.sh
-rw-rw-r-- 1 linux linux 33 Dec 19 01:14 hello.sh
linux@ubuntu:~/test_shell$ chmod +x hello.sh
linux@ubuntu:~/test_shell$ ls -l hello.sh
-rwxrwxr-x 1 linux linux 33 Dec 19 01:14 hello.sh
运行hello.sh
linux@ubuntu:~/test_shell$ ./hello.sh
hello shell!
练习:
linux@ubuntu:~/shell_test$ vi shell.sh
linux@ubuntu:~/shell_test$ /bin/sh shell.sh
hello world!
linux@ubuntu:~/shell_test$ ./shell.sh
bash: ./shell.sh: Permission denied
linux@ubuntu:~/shell_test$ chmod +x shell.sh
linux@ubuntu:~/shell_test$ ./shell.sh
hello world!
linux@ubuntu:~/shell_test$ ls -l shell.sh
-rwxrwxr-x 1 linux linux 33 Dec 19 06:56 shell.sh
linux@ubuntu:~/shell_test$ cat shell.sh
#!/bin/bash
echo "hello world!"
二,shell变量
1,定义变量
定义变量时,变量名不加美元符号
注意,变量名和等号之间不能有空格,这可能和你熟悉的所有编程语言都不一样。同时,变量名的命名须遵循如下规则:
- 首个字符必须为字母(a-z,A-Z)。
- 中间不能有空格,可以使用下划线(_)。
- 不能使用标点符号。
- 不能使用bash里的关键字(可用help命令查看保留关键字)。
- 例如:
-
linux@ubuntu:~/shell_test$ cat shell.sh
#!/bin/bash
echo "hello world!"
your_name="Liu Jing"
echo $your_name
echo ${your_name}
输出:linux@ubuntu:~/shell_test$ ./shell.sh
hello world!
Liu Jing
Liu Jing
注意:
-
your_name = "xiao ming"
linux@ubuntu:~/shell_test$ cat shell.sh
#!/bin/bash
echo "hello world!"
your_name="Liu Jing"
echo $your_name
echo ${your_name}
for skill in C C++ Java Pthon Andorid; do
echo "I am good at ${skill}Language!"
done
linux@ubuntu:~/shell_test$ ./shell.sh
hello world!
Liu Jing
Liu Jing
I am good at CLanguage!
I am good at C++Language!
I am good at JavaLanguage!
I am good at PthonLanguage!
I am good at AndoridLanguage!
如果不给skill变量加花括号,写成echo "I am good at $skillScript",解释器就会把$skillLanguage!当成一个变量(其值为空),代码执行结果就不是我们期望的样子了。
推荐给所有变量加上花括号,这是个好的编程习惯。
下面证明其为空:
linux@ubuntu:~/shell_test$ cat shell.sh
#!/bin/bash
echo "hello world!"
your_name="Liu Jing"
echo $your_name
echo ${your_name}
for skill in C C++ Java Pthon Andorid; do
echo "I am good at $skillLanguage!"
done
linux@ubuntu:~/shell_test$ ./shell.sh
hello world!
Liu Jing
Liu Jing
I am good at !
I am good at !
I am good at !
I am good at !
I am good at !
2,修改已定义过的变量
已定义的变量,可以被重新定义,
但注意,第二次赋值的时候不能写$your_name="alibaba",使用变量的时候才加美元符($)。
修改如下:
linux@ubuntu:~/shell_test$ cat shell.sh
#!/bin/bash
echo "hello world!"
your_name="Liu Jing"
echo $your_name
your_name="xiao niu"
echo $your_name
linux@ubuntu:~/shell_test$ ./shell.sh
hello world!
Liu Jing
xiao niu
3,只读变量
linux@ubuntu:~/shell_test$ cat shell.sh
#!/bin/bash
echo "hello world!"
your_name="Liu Jing"
echo $your_name
readonly your_name
your_name="xiao niu"
echo $your_name
linux@ubuntu:~/shell_test$ ./shell.sh
hello world!
Liu Jing
./shell.sh: line 9: your_name: readonly variable
Liu Jing
删除变量
使用 unset 命令可以删除变量。变量被删除后不能再次使用。unset 命令不能删除只读变量。
语法:
linux@ubuntu:~/shell_test$ cat shell.sh
#!/bin/bash
echo "hello world!"
your_name="Liu Jing"
echo $your_name
unset your_name
echo $your_name
echo "end"
linux@ubuntu:~/shell_test$ ./shell.sh
hello world!
Liu Jing
end
Shell 字符串
字符串是shell编程中最常用最有用的数据类型(除了数字和字符串,也没啥其它类型好用了),字符串可以用单引号,也可以用双引号,也可以不用引号
单引号
单引号字符串的限制:
- 单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的;
- 单引号字串中不能出现单引号(对单引号使用转义符后也不行)。
linux@ubuntu:~/shell_test$ cat shell.sh
#!/bin/bash
echo "hello world!"
your_name='Liu Jing'
echo 'This is $your_name'
echo $your_name
echo "end"
linux@ubuntu:~/shell_test$ ./shell.sh
hello world!
This is $your_name
Liu Jing
end
双引号
双引号的优点:
- 双引号里可以有变量
- 双引号里可以出现转义字符
linux@ubuntu:~/shell_test$ cat shell.sh
#!/bin/bash
echo "hello world!"
your_name="Liu Jing"
echo "I know your name is ${your_name},is not?"
echo "I know your name is \"$your_name\"
,is not?"- echo $your_name
echo "end"
linux@ubuntu:~/shell_test$ ./shell.sh
hello world!
I know your name is Liu Jing,is not?
I know your name is $your_name
,is not?Liu Jing
end
拼接字符串
linux@ubuntu:~/shell_test$ cat shell.sh
#!/bin/bash
echo "hello world!"
your_name='Liu Jing'
all_name="hello, "$your_name" !"
other_name="hello, ${your_name} !"
echo $your_name $all_name $other_name
echo "end"
linux@ubuntu:~/shell_test$ ./shell.sh
hello world!
Liu Jing hello, Liu Jing ! hello, Liu Jing !
end
获取字符串长度
linux@ubuntu:~/shell_test$ cat shell.sh
#!/bin/bash
your_name='Liu Jing'
echo ${#your_name}
linux@ubuntu:~/shell_test$ ./shell.sh
8
提取子字符串
以下实例从字符串第
2 个字符开始截取
4 个字符:
linux@ubuntu:~/shell_test$ cat shell.sh
#!/bin/bash
your_name='Liu Jing'
echo ${your_name:1:4}
linux@ubuntu:~/shell_test$ ./shell.sh
iu J
查找子字符串
查找字符 "a或 n" 的位置,注意: 脚本中 "`" 是反引号,而不是单引号 "'",不要看错了哦。
linux@ubuntu:~/shell_test$ cat shell.sh
#!/bin/bash
your_name="Liu Jing is a great man!"
echo `expr index "$your_name" an`
linux@ubuntu:~/shell_test$ ./shell.sh
7
Shell 注释
以"#"开头的行就是注释,会被解释器忽略。
sh里没有多行注释,只能每一行加一个#号。只能像这样:
#-------------------------------------------- # 这是一个注释 # slogan:学的不仅是技术,更是梦想! #-------------------------------------------- ##### 用户配置区 开始 ##### # # # 这里可以添加脚本描述信息 # # ##### 用户配置区 结束 #####
如果在开发过程中,遇到大段的代码需要临时注释起来,过一会儿又取消注释,怎么办呢?
每一行加个#符号太费力了,可以把这一段要注释的代码用一对花括号括起来,定义成一个函数,没有地方调用这个函数,这块代码就不会执行,达到了和注释一样的效果。
linux@ubuntu:~/shell_test$ cat shell.sh
#!/bin/bash
abc(){
your_name=(liujing xiaoniu xiaohu kurong c++)
echo ${your_name[@]}
echo ${your_name[1]} ${your_name[4]}
echo ${#your_name[@]}
echo ${#your_name[1]}
}
my_name[0]=liu
my_name[1]=jing
my_name[10]=haha
echo ${my_name[@]}
echo ${my_name[1]} ${my_name[10]}
echo ${#my_name[*]}
echo ${#my_name[10]}
linux@ubuntu:~/shell_test$ ./shell.sh
liu jing haha
jing haha
3
4
可以看到abc()函数内的内容并没有输出
Shell 传递参数
我们可以在执行 Shell 脚本时,向脚本传递参数,脚本内获取参数的格式为:
$n。
n 代表一个数字,
$0 为执行的文件,$1 为执行脚本的第一个参数,$2 为执行脚本的第二个参数,以此类推……
linux@ubuntu:~/shell_test$ cat shell.sh
#!/bin/bash
echo "Shell args test!"
echo "arg0: $0"
echo "arg1: $1"
echo "arg2: $2"
echo "arg3: $3"
linux@ubuntu:~/shell_test$ ./shell.sh jing love shell
Shell args test!
arg0: ./shell.sh
arg1: jing
arg2: love
arg3: shell
另外,还有几个特殊字符用来处理参数:
linux@ubuntu:~/shell_test$ cat shell.sh
#!/bin/bash
echo "Shell args test!" #
Shell args test!echo "arg2: $2" #
arg2: loveecho "arg count: $#" #
arg count: 3echo "$*" #
jing love shellecho "$$" #
8003echo "$!" #空
echo "$@" #
jing love shellecho "$-" #
hBecho "$?" #
0linux@ubuntu:~/shell_test$ ./shell.sh jing love shell
Shell args test!
arg2: love
arg count: 3
jing love shell
8003
jing love shell
hB
0
$* 与 $@ 区别:
- 相同点:都是引用所有参数。
- 不同点:只有在双引号中体现出来。假设在脚本运行时写了三个参数 1、2、3,,则 " * " 等价于 "1 2 3"(传递了一个参数),而 "@" 等价于 "1" "2" "3"(传递了三个参数)。
linux@ubuntu:~/shell_test$ cat shell.sh
#!/bin/bash
echo "Shell args test!"
echo "$*"
echo "$@"
echo "---- \$* ----"
for i in "$*"; do
echo $i
done
echo "---- \$@ ----"
for i in "$@"; do
echo $i
done
linux@ubuntu:~/shell_test$ ./shell.sh jing love shell
Shell args test!
jing love shell
jing love shell
---- $* ----
jing love shell
---- $@ ----
jing
love
shell