shell脚本
1 脚本
什么是脚本?其实就是把很多条命令放在一个文件中,然后直接运行这么文件,类似于windows中的批处理命令。在很多地方都会使用到脚本,如matlab种新建 m文件,其实就是应用脚本,如下所示:
常用的 m文件就是matlab脚本,shell脚本与之类似。
2 echo
1 创建shell脚本文件
echo 命令:
shell echo命令:
- 1 显示普通字符串 echo "hello world"
- 2 显示转义字符 echo "\"hello world\""
- 3 显示变量 echo "name is $name"
- 4 显示换行 -e echo "hello \n world"
- 5 不显示换行 \c echo "input number:\c"
最简单的shell脚本如下:
#!/bin/bash
echo "hello world!"
注意,刚创建的shell脚本是没有可执行权限的,要运行必须赋予可执行权限;
ding@ding-ubuntu:~/shell$ ls -l
总用量 4
// 可以看到,新创建的shell脚本,没有可执行权限
-rw-r--r-- 1 ding ding 32 2月 11 17:24 test.sh
ding@ding-ubuntu:~/shell$ ./test.sh // 无法执行
bash: ./test.sh: 权限不够
ding@ding-ubuntu:~/shell$ chmod 777 test.sh // 赋予最大权限
ding@ding-ubuntu:~/shell$ ./test.sh // 执行成功
hello world!
3 read
交互式 shell 脚本,使用 read 命令,获取键盘输入。如下所示:
- -a 将分开输入的字符,依次存储到数组中
- -p 指定要显示的提示
- -s 静默输入,一般用于密码
- -n 8 指定输入字符最大长度为8
- -d "字符" 输入结束符,当输入的内容出现这个字符时,立即结束输入
- -t N 超出N秒没有输入,自动退出
#!/bin/bash
read -p "input your age and height: " age height
echo "your age = $age, your height = $height"
脚本执行结果:
ding@ding-ubuntu:~/shell$ ./test.sh
input your age and height: 18 180
your age = 18, your height = 180
4 shell脚本数值计算
shell 脚本的数值计算仅支持整型,数值计算使用:$((表达式)),表达式外有 2 层括号;
// shell 脚本
#!/bin/bash
echo -e "input two numbers: \c"
read num1 num2
echo "$num1 + $num2 = $((num1 + num2))"
// 执行结果
ding@ding-ubuntu:~/shell$ ./test.sh
input two numbers: 15 35
15 + 35 = 50
5 test
shell中的 test 命令用于检查某个条件是否成立,它可以进行数值、字符和文件三个方面的测试
&& 和 || 命令说明:
cmd1 && cmd2 当cmd1执行完并且正确,那么cmd2开始执行,如果cmd1执行完毕错误,那么cmd2不执行;
cmd1 || cmd2 当cmd1执行完毕并且正确,那么cmd2不执行,反之cmd2执行;
1 test 命令查找文件:
脚本内容如下:
#!/bin/bash
echo -e "please input file name: \c"
read file_name
test -e $file_name && echo "$file_name exist!" || echo "$file_name no exist!"
以下是测试结果:
ding@ding-ubuntu:~/shell$ ./test.sh
please input file name: a.txt // 查找不存在的文件
a.txt no exist!
ding@ding-ubuntu:~/shell$ ./test.sh
please input file name: test.sh // 查找到脚本文件本身
test.sh exist!
2 test 命令比较字符串
脚本内容
#!/bin/bash
echo "I will compare two string, ready to input!!!"
read -p "first string: " first_str
read -p "second string: " second_str
test $first_str = $second_str && echo "first string == second string" || echo "first string != second string"
测试结果如下:
ding@ding-ubuntu:~/shell$ ./test.sh
I will compare two string, ready to input!!!
first string: abcd
second string: abcd
first string == second string // 两字符串相等
ding@ding-ubuntu:~/shell$ ./test.sh
I will compare two string, ready to input!!!
first string: aaa
second string: bbb
first string != second string // 两字符串不等
6 中括号 [ ] 判断符
中括号判断符的用法,完全等价于 test 命令(中括号首尾要加入空格)。
同 test 命令一样,中括号脚本如下:
#!/bin/bash
echo "I will compare two string, ready to input!!!"
read -p "first string: " first_str
read -p "second string: " second_str
[ "$first_str" = "$second_str" ] && echo "first string == second string" || echo "first string != second string"
测试结果:
// 和 test 命令一样的执行效果
ding@ding-ubuntu:~/shell$ ./test.sh
I will compare two string, ready to input!!!
first string: abc
second string: abc
first string == second string
ding@ding-ubuntu:~/shell$ ./test.sh
I will compare two string, ready to input!!!
first string: qwer
second string: asdf
first string != second string
使用建议:
使用[ ]判断符,建议对其内变量、字符串使用双引号包围。换句话说,能做字符串比较的时候,不要用数值比较。
如果不使用双引号包围,在输入字符串时如果遇到空格,可能会报错。如下所示:
// 脚本内容
#!/bin/bash
read -p "please input passwd: " passwd
[ $passwd = "passwd 1234" ] && echo "passwd is correct!" || echo "passwd is uncorrect!"
// 测试结果
ding@ding-ubuntu:~/shell$ ./test.sh
please input passwd: passwd 1234 // 输入了正确的字符串,但是报错
./test.sh: 第 3 行: [: 参数太多
passwd is uncorrect!
7 默认变量
$0~$n:表示 shell 脚本的参数,包括 shell 脚本命令本身,shlle 脚本命令本身为$0
$#:表示最后一个参数的标号。
$@:表示$1、$2、$3......
测试脚本:
// 脚本内容
#!/bin/bash
echo "file name: " $0
echo "total param num: " $#
echo "whole param: " $@
// 测试结果
ding@ding-ubuntu:~/shell$ ./test.sh a b c // 脚本带3个参数
file name: ./test.sh
total param num: 3
whole param: a b c