vi hello.sh
linux@ubuntu:~/test_shell$ cat hello.sh #!/bin/bashecho "hello shell!"
linux@ubuntu:~/test_shell$ cat hello.sh
#!/bin/bash
echo "hello shell!"
linux@ubuntu:~/test_shell$ ls -l hello.sh -rw-rw-r-- 1 linux linux 33 Dec 19 01:14 hello.shlinux@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
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
-rwxrwxr-x 1 linux linux 33 Dec 19 01:14 hello.sh
linux@ubuntu:~/test_shell$ ./hello.sh hello shell!
linux@ubuntu:~/test_shell$ ./hello.sh
hello shell!
linux@ubuntu:~/shell_test$ vi shell.shlinux@ubuntu:~/shell_test$ /bin/sh shell.shhello world!linux@ubuntu:~/shell_test$ ./shell.shbash: ./shell.sh: Permission deniedlinux@ubuntu:~/shell_test$ chmod +x shell.sh linux@ubuntu:~/shell_test$ ./shell.shhello world!linux@ubuntu:~/shell_test$ ls -l shell.sh -rwxrwxr-x 1 linux linux 33 Dec 19 06:56 shell.shlinux@ubuntu:~/shell_test$ cat shell.sh #!/bin/bashecho "hello world!"
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$ ls -l shell.sh
-rwxrwxr-x 1 linux linux 33 Dec 19 06:56 shell.sh
linux@ubuntu:~/shell_test$ cat shell.sh
echo "hello world!"
注意,变量名和等号之间不能有空格,这可能和你熟悉的所有编程语言都不一样。同时,变量名的命名须遵循如下规则:
linux@ubuntu:~/shell_test$ cat shell.sh #!/bin/bashecho "hello world!"your_name="Liu Jing"echo $your_nameecho ${your_name}
your_name="Liu Jing"
echo $your_name
echo ${your_name}
linux@ubuntu:~/shell_test$ ./shell.sh hello world!Liu JingLiu Jing
Liu Jing
your_name = "xiao ming"
linux@ubuntu:~/shell_test$ cat shell.sh #!/bin/bashecho "hello world!"your_name="Liu Jing"echo $your_nameecho ${your_name}for skill in C C++ Java Pthon Andorid; do echo "I am good at ${skill}Language!"donelinux@ubuntu:~/shell_test$ ./shell.sh hello world!Liu JingLiu JingI 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!
for skill in C C++ Java Pthon Andorid; do
echo "I am good at ${skill}Language!"
done
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/bashecho "hello world!"your_name="Liu Jing"echo $your_nameecho ${your_name}for skill in C C++ Java Pthon Andorid; do echo "I am good at $skillLanguage!"donelinux@ubuntu:~/shell_test$ ./shell.sh hello world!Liu JingLiu JingI am good at !I am good at !I am good at !I am good at !I am good at !
echo "I am good at $skillLanguage!"
I am good at !
linux@ubuntu:~/shell_test$ cat shell.sh #!/bin/bashecho "hello world!"your_name="Liu Jing"echo $your_nameyour_name="xiao niu"echo $your_namelinux@ubuntu:~/shell_test$ ./shell.sh hello world!Liu Jingxiao niu
your_name="xiao niu"
xiao niu
linux@ubuntu:~/shell_test$ cat shell.sh #!/bin/bashecho "hello world!"your_name="Liu Jing"echo $your_namereadonly your_nameyour_name="xiao niu"echo $your_namelinux@ubuntu:~/shell_test$ ./shell.sh hello world!Liu Jing./shell.sh: line 9: your_name: readonly variableLiu Jing
readonly your_name
./shell.sh: line 9: your_name: readonly variable
使用 unset 命令可以删除变量。变量被删除后不能再次使用。unset 命令不能删除只读变量。
语法:
linux@ubuntu:~/shell_test$ cat shell.sh #!/bin/bashecho "hello world!"your_name="Liu Jing"echo $your_nameunset your_nameecho $your_nameecho "end"linux@ubuntu:~/shell_test$ ./shell.sh hello world!Liu Jingend
unset your_name
echo "end"
end
字符串是shell编程中最常用最有用的数据类型(除了数字和字符串,也没啥其它类型好用了),字符串可以用单引号,也可以用双引号,也可以不用引号
单引号字符串的限制:
linux@ubuntu:~/shell_test$ cat shell.sh #!/bin/bashecho "hello world!"your_name='Liu Jing'echo 'This is $your_name'echo $your_nameecho "end"linux@ubuntu:~/shell_test$ ./shell.sh hello world!This is $your_nameLiu Jingend
your_name='Liu Jing'
echo 'This is $your_name'
This is $your_name
双引号的优点:
linux@ubuntu:~/shell_test$ cat shell.sh #!/bin/bashecho "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_nameecho "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 Jingend
echo "I know your name is ${your_name},is not?"
echo "I know your name is \"$your_name\"
I know your name is Liu Jing,is not?
I know your name is $your_name,is not?
I know your name is $your_name
linux@ubuntu:~/shell_test$ cat shell.sh #!/bin/bashecho "hello world!"your_name='Liu Jing'all_name="hello, "$your_name" !"other_name="hello, ${your_name} !"echo $your_name $all_name $other_nameecho "end"linux@ubuntu:~/shell_test$ ./shell.sh hello world!Liu Jing hello, Liu Jing ! hello, Liu Jing !end
all_name="hello, "$your_name" !"
other_name="hello, ${your_name} !"
echo $your_name $all_name $other_name
Liu Jing hello, Liu Jing ! hello, Liu Jing !
linux@ubuntu:~/shell_test$ cat shell.sh #!/bin/bashyour_name='Liu Jing'echo ${#your_name}linux@ubuntu:~/shell_test$ ./shell.sh 8
echo ${#your_name}
8
linux@ubuntu:~/shell_test$ cat shell.sh #!/bin/bashyour_name='Liu Jing'echo ${your_name:1:4}linux@ubuntu:~/shell_test$ ./shell.sh iu J
echo ${your_name:1:4}
iu J
查找字符 "a或 n" 的位置,注意: 脚本中 "`" 是反引号,而不是单引号 "'",不要看错了哦。
linux@ubuntu:~/shell_test$ cat shell.sh #!/bin/bashyour_name="Liu Jing is a great man!"echo `expr index "$your_name" an`linux@ubuntu:~/shell_test$ ./shell.sh 7
your_name="Liu Jing is a great man!"
echo `expr index "$your_name" an`
7
以"#"开头的行就是注释,会被解释器忽略。
sh里没有多行注释,只能每一行加一个#号。只能像这样:
#-------------------------------------------- # 这是一个注释 # slogan:学的不仅是技术,更是梦想! #-------------------------------------------- ##### 用户配置区 开始 ##### # # # 这里可以添加脚本描述信息 # # ##### 用户配置区 结束 #####
如果在开发过程中,遇到大段的代码需要临时注释起来,过一会儿又取消注释,怎么办呢?
每一行加个#符号太费力了,可以把这一段要注释的代码用一对花括号括起来,定义成一个函数,没有地方调用这个函数,这块代码就不会执行,达到了和注释一样的效果。
linux@ubuntu:~/shell_test$ cat shell.sh #!/bin/bashabc(){ 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]=liumy_name[1]=jingmy_name[10]=hahaecho ${my_name[@]}echo ${my_name[1]} ${my_name[10]}echo ${#my_name[*]}echo ${#my_name[10]} linux@ubuntu:~/shell_test$ ./shell.sh liu jing hahajing haha34
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]}
liu jing haha
jing haha
3
4
linux@ubuntu:~/shell_test$ cat shell.sh #!/bin/bashecho "Shell args test!"echo "arg0: $0"echo "arg1: $1"echo "arg2: $2"echo "arg3: $3" linux@ubuntu:~/shell_test$ ./shell.sh jing love shellShell args test!arg0: ./shell.sharg1: jingarg2: lovearg3: shell
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/bashecho "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 shellShell args test!arg2: lovearg count: 3jing love shell8003jing love shellhB0
echo "Shell args test!" #
echo "arg2: $2" #
echo "arg count: $#" #
echo "$*" #
echo "$$" #
echo "$!" #空
echo "$@" #
echo "$-" #
echo "$?" #
arg count: 3
jing love shell
8003
hB
0
$* 与 $@ 区别:
linux@ubuntu:~/shell_test$ cat shell.sh #!/bin/bashecho "Shell args test!"echo "$*"echo "$@"echo "---- \$* ----"for i in "$*"; do echo $idoneecho "---- \$@ ----"for i in "$@"; do echo $idone linux@ubuntu:~/shell_test$ ./shell.sh jing love shellShell args test!jing love shelljing love shell---- $* ----jing love shell---- $@ ----jingloveshell
echo "$*"
echo "$@"
echo "---- \$* ----"
for i in "$*"; do
echo $i
echo "---- \$@ ----"
for i in "$@"; do
---- $* ----
---- $@ ----
jing
love
shell