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

简介:

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

一、shell的执行方式

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

1
2
3
4
5
6
7
8
9
10
11
12
13
[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  in  {1..100}; do
let  sum +=x
let  x++
done
echo  "The sum is:"  $ sum


1、相对路径执行脚本,给予shell scripts执行权限,然后切换到scripts脚本所在的目录下,执行scripts
./

1
2
3
4
5
6
#查看文件有没有执行权限
[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、绝对路径执行

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


3、直接使用bash或sh来执行scripts
这种方式执行脚本不需要提前给予权限

1
2
3
4
5
6
7
8
9
10
11
12
#使用当脚本有执行权限的时候使用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脚本文件

1
2
3
4
5
6
[root@lovelace 51cto] # cat ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if  [ -f ~/.bashrc ];  then
. ~/.bashrc
fi

使用source执行脚本    source scriptsname.sh

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

 

二、bash  shell排错

在这里我们把脚本稍微修改下,以满足我们下面的排错,我们把done修改为don

1
2
3
4
5
6
7
8
9
10
11
12
#!/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  in  {1..100}; do
let  sum +=x
let  x++
don
echo  "The sum is:"  $ sum

bash排错:
1、bash  -v  scriptsname.sh  :检查语法的指令  提示我们15行附近有语法错误,但没有指定具体哪一行错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[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  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  :查看程序行  只返回语法错误信息

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


3、bash -x scriptsname.sh :追踪执行过程  这是是排错中最常用的(如果有错误就不在往下执行,直接打印出错误),不过对于篇幅很大的脚本文件,排错也是件麻烦事情

1
2
3
[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",打印成功,说明前面的语句都没问题

1
2
3
4
5
[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、单步执行,在命令行中把要写入脚本的文件都执行一边,然后在写入脚本,减少错误

1
2
[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的执行效果

1
2
3
4
5
6
7
[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
1
2
3
4
5
[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脚本编写和排错都是相对的,每个人都有自己总结出来的一套排错套路,但是这里还是建议初写脚本的兄弟们书写的时候尽可能的先在命令行下执行下你所要执行的语句,然后再写入脚本,这样尽管会浪费你一点时间,但是相较于漫无目的的查找错误来说,还是值得的。尤其是针对大篇幅的脚本文档编写的时候。



本文转自lovelace521 51CTO博客,原文链接:http://blog.51cto.com/lovelace/1211105,如需转载请自行联系原作者

相关文章
|
关系型数据库 MySQL Shell
shell学习(十七) 【mysql脚本备份】
shell学习(十七) 【mysql脚本备份】
155 0
|
Linux Shell 文件存储
【Linux技术专题】「夯实基本功系列」带你一同学习和实践操作Linux服务器必学的Shell指令(深入df和dh的区别和探索)
【Linux技术专题】「夯实基本功系列」带你一同学习和实践操作Linux服务器必学的Shell指令(深入df和dh的区别和探索)
493 1
|
监控 Linux Shell
【Linux技术专题】「夯实基本功系列」带你一同学习和实践操作Linux服务器必学的Shell指令(排查问题指令 - 下)
在线上排查问题时,查询日志、查看系统配置和分析操作系统信息是至关重要的。这些操作可以帮助我们深入了解软件和服务的兼容性,并解决潜在的问题。在本次学习中,我们将介绍并深入学习一些我在处理类似问题时常用的指令。通过掌握这些指令,你将能够更加高效地定位和解决线上问题,提高系统的稳定性和性能。让我们一同进入这个学习过程吧!
284 0
【Linux技术专题】「夯实基本功系列」带你一同学习和实践操作Linux服务器必学的Shell指令(排查问题指令 - 下)
|
监控 Unix Shell
shell脚本编程学习
【10月更文挑战第1天】shell脚本编程
333 12
|
Shell Linux
Shell 脚本编程学习
Shell 脚本编程学习
163 0
|
运维 Shell Python
Shell和Python学习教程总结
Shell和Python学习教程总结
|
Shell
shell学习(六) 【case多条件分支语句】
shell学习(六) 【case多条件分支语句】
372 1
|
Shell 应用服务中间件 nginx
shell学习(七) 【shell 函数】
shell学习(七) 【shell 函数】
137 1
|
Shell Perl
shell学习(十) 【shell awk基本语法】
shell学习(十) 【shell awk基本语法】
184 1
|
机器学习/深度学习 Shell Windows
shell学习(八) 【shell 正则 匹配符和限定符】
shell学习(八) 【shell 正则 匹配符和限定符】
151 1