shell学习之shell执行方式及排错

shell程序组成:
变量设定:
内置命令:
shell的语法结构:
函数及其余命令行的程序所组成
shell

1、shell的执行方式bash

示例脚本(计算1到100的和):ide

[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


一、相对路径执行脚本,给予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

二、绝对路径执行
oop

#查看当前脚本所在路径并使用绝对路径执行该脚本
[root@lovelace 51cto]# pwd
/home/scripts/51cto
[root@lovelace 51cto]# /home/scripts/51cto/sum.sh
The sum is: 5050


三、直接使用bash或sh来执行scripts
这种方式执行脚本不须要提早给予权限spa

#使用当脚本有执行权限的时候使用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
四、在现行的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.ship

[root@lovelace 51cto]# source sum.sh
The sum is: 5050

 

2、bash  shell排错文档

在这里咱们把脚本稍微修改下,以知足咱们下面的排错,咱们把done修改成donit

#!/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排错:
一、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

 

二、bash -n  scriptsname.sh  :查看程序行  只返回语法错误信息

[root@lovelace 51cto]# bash -n sum.sh
sum.sh: line 15: syntax error: unexpected end of file


三、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


四、特定位置放置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

五、shopt定义变量,可避免未定义变量而是用变量

关于shopt的用法:
shopt 
 -s:启用选项
 -u:关闭选项
 -o:使用和set -o相同的选项来设置
 -q:不显示开关状态,已回传状态来判断,0表示启用,非0表示关闭

六、单步执行,在命令行中把要写入脚本的文件都执行一边,而后在写入脚本,减小错误

[root@lovelace 51cto]# declare -i sum=0
[root@lovelace 51cto]# for x in {1..100};do let sum+=x;let x++;echo $sum;done

七、使用函数减少错误范围
八、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脚本编写和排错都是相对的,每一个人都有本身总结出来的一套排错套路,可是这里仍是建议初写脚本的兄弟们书写的时候尽量的先在命令行下执行下你所要执行的语句,而后再写入脚本,这样尽管会浪费你一点时间,可是相较于漫无目的的查找错误来讲,仍是值得的。尤为是针对大篇幅的脚本文档编写的时候。

相关文章
相关标签/搜索