shell程序组成:
变量设定:
内置命令:
shell的语法结构:
函数及其他命令行的程序所组成
一、shell的执行方式
示例脚本(计算1到100的和):
[root@lovelace 51cto]# cat sum.sh
#!/bin/bash
#Verson:0.1
#Auther:lovelace
#Pragram:This program is and calculate from 1 to 100
#define i an integer
declare -i sum=0
#loop and from 1 to 100
for x in {1..100};do
let sum+=x
let x++
done
echo "The sum is:" $sum
1、相对路径执行脚本,给予shell scripts执行权限,然后切换到scripts脚本所在的目录下,执行scripts
./#查看文件有没有执行权限
[root@lovelace 51cto]# ll sum.sh
-rwxr-xr-x 1 root root 217 05-20 12:37 sum.sh
#赋权并执行程序
[root@lovelace 51cto]# chmod +x sum.sh ; ./sum.sh
The sum is: 5050
2、绝对路径执行#查看当前脚本所在路径并使用绝对路径执行该脚本
[root@lovelace 51cto]# pwd
/home/scripts/51cto
[root@lovelace 51cto]# /home/scripts/51cto/sum.sh
The sum is: 5050
3、直接使用bash或sh来执行scripts
这种方式执行脚本不需要提前给予权限#使用当脚本有执行权限的时候使用sh或bash的执行结果
[root@lovelace 51cto]# sh sum.sh
The sum is: 5050
[root@lovelace 51cto]# bash sum.sh
The sum is: 5050
#取消脚本的执行权限,再次以sh或bash执行查看结果
[root@lovelace 51cto]# chmod -x sum.sh ; ll sum.sh
-rw-r--r-- 1 root root 231 05-20 12:52 sum.sh
[root@lovelace 51cto]# bash sum.sh
The sum is: 5050
[root@lovelace 51cto]# sh sum.sh
The sum is: 5050
以上脚本执行毁在当前shell下开启一个子shell,然后在子shell中执行该脚本,当脚本执行完之后再
关闭相应的子shell
4、在现行的shell中执行脚本
. /home/test/51cto/test.sh
.和/之间最少要有一个空格 (这种格式在脚本调用中常见)参见~/.bash_profile脚本文件[root@lovelace 51cto]# cat ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
使用source执行脚本 source scriptsname.sh[root@lovelace 51cto]# source sum.sh
The sum is: 5050
二、bash shell排错
在这里我们把脚本稍微修改下,以满足我们下面的排错,我们把done修改为don
#!/bin/bash
#Verson:0.1
#Auther:lovelace
#Pragram:This program is and calculate from 1 to 100
#define i an integer
declare -i sum=0
#loop and from 1 to 100
for x in {1..100};do
let sum+=x
let x++
don
echo "The sum is:" $sum
bash排错:
1、bash -v scriptsname.sh :检查语法的指令 提示我们15行附近有语法错误,但没有指定具体哪一行错误[root@lovelace 51cto]# bash -v sum.sh
#!/bin/bash
#Verson:0.1
#Auther:lovelace
#Pragram:This program is and calculate from 1 to 100
#define i an integer
declare -i sum=0
#loop and from 1 to 100
for x in {1..100};do
let sum+=x
let x++
don
echo "The sum is:" $sum
sum.sh: line 15: syntax error: unexpected end of file
2、bash -n scriptsname.sh :查看程序行 只返回语法错误信息[root@lovelace 51cto]# bash -n sum.sh
sum.sh: line 15: syntax error: unexpected end of file
3、bash -x scriptsname.sh :追踪执行过程 这是是排错中最常用的(如果有错误就不在往下执行,直接打印出错误),不过对于篇幅很大的脚本文件,排错也是件麻烦事情[root@lovelace 51cto]# bash -x sum.sh
+ declare -i sum=0
sum.sh: line 15: syntax error: unexpected end of file
4、特定位置放置echo:在for循环上面添加一句:echo “print test",打印成功,说明前面的语句都没问题[root@lovelace 51cto]# bash -x sum.sh
+ declare -i sum=0
+ echo 'print test'
print test
sum.sh: line 16: syntax error: unexpected end of file
5、shopt定义变量,可避免未定义变量而是用变量
关于shopt的用法:
shopt
-s:启用选项
-u:关闭选项
-o:使用和set -o相同的选项来设置
-q:不显示开关状态,已回传状态来判断,0表示启用,非0表示关闭
6、单步执行,在命令行中把要写入脚本的文件都执行一边,然后在写入脚本,减少错误[root@lovelace 51cto]# declare -i sum=0
[root@lovelace 51cto]# for x in {1..100};do let sum+=x;let x++;echo $sum;done
7、使用函数减小错误范围
8、set -x,在脚本中添加一行:set -x 此功能类似 bash -x scripts的执行效果[root@lovelace 51cto]# cat sum.sh
#!/bin/bash
#Verson:0.1
#Auther:lovelace
#Pragram:This program is and calculate from 1 to 100
#在脚本中添加下面这一行
set -x[root@lovelace 51cto]# ./sum.sh
+ declare -i sum=0
+ echo 'print test'
print test
./sum.sh: line 16: syntax error: unexpected end of file
小贴士:有些时候我们编辑scripts的时候可能出现没有保存的情况,这个时候每次打开这个
scripts都会弹出一堆信息,很是烦人,这个时候你可以找到这个脚本的编辑目录下
有一个和脚本命令加上.swp的后缀的隐藏文件,把这个隐藏文件删除即可。
其他:scripts中很多时候空格很重要,如若不然就可能出现错误。
set :设定bash shell的属性,若不加任何参数和选项,她会显示所有的shell变量和函数的内容。
-o: 开启某个选项
+o: 关闭某个选项
后记:shell脚本编写和排错都是相对的,每个人都有自己总结出来的一套排错套路,但是这里还是建议初写脚本的兄弟们书写的时候尽可能的先在命令行下执行下你所要执行的语句,然后再写入脚本,这样尽管会浪费你一点时间,但是相较于漫无目的的查找错误来说,还是值得的。尤其是针对大篇幅的脚本文档编写的时候。