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

转载于:https://blog.51cto.com/lovelace/1211105

shell学习之shell执行方式及排错相关推荐

  1. shell简介和脚本执行方式

    shell简介和脚本执行方式 1.shell简介 Shell是命令解释器(command interpreter),是Unix操作系统的用户接口,程序从用户接口得到输入信息,shell将用户程序及其输 ...

  2. Linux Shell编程之脚本执行方式

    1.新建bash脚本文件 打开命令终端 touch hello.sh vim hello.sh 2.编辑bash文件 #!/bin/bash # this is a test programerech ...

  3. shell学习之shell基础知识了解

    一.了解bash shell /etc/bash是Linux系统预设的shell.bash是GNU计划中重要的工具软件之一,目前也是Linux distributions的标准 shell. Shel ...

  4. 69:shell脚本介绍 | shell脚本结构 | 执行data命令用法 | shell脚本中变量

    2019独角兽企业重金招聘Python工程师标准>>> 1.shell脚本介绍: shell是一种脚本语言和传统的开发语言相比,会比较简单: shell有自己语法,可以支持逻辑判断. ...

  5. Shell简介:什么是Shell,Shell命令的两种执行方式

    Shell本身是一个用C语言编写的程序,它是用户使用Unix/Linux的桥梁,用户的大部分工作都是通过Shell完成的.Shell既是一种命令语言,又是一种程序设计语言.作为命令语言,它交互式地解释 ...

  6. 『SHELL』--SHELL脚本执行方式(转)

    Shell脚本的执行方式: 注明:wd代表"脚本保存的目录" 1.fork 语法:/wd/shell.sh fork是最普通的, 就是直接在脚本里面用/wd/shell.sh来调用 ...

  7. linux shell脚本的执行方式与区别

    linux shell脚本的执行方式与区别

  8. js执行shell命令的几种方式(Node)

    js执行shell命令的几种方式(Node) nodejs 执行cmd或shell命令 Nodejs调用shell脚本 nodejs调用shell

  9. shell脚本执行方式,更方便更快捷。

    在进行linux测试时编写脚本是必不可少的.最近经常使用Linux,感觉太频繁地敲击键盘有些累了,于是想到了Shell脚本.可以把太多的命令写成一个脚本,这样每次执行一遍  shell文件,就可以省去 ...

最新文章

  1. tensorflow object_detect 操作步骤
  2. 下载python后怎样打开-下载python后如何启动
  3. hdu 1055(贪心)
  4. android 黑边边框,手机屏幕边缘的黑边是什么呢?
  5. CodeForces - 571D Campus(数据结构综合)
  6. python当前日期加n天_利用python获取当前日期前后N天或N月日期的方法示例
  7. python中对文件的操作_Python对文件操作知识
  8. rabbitmq如何清空queue队列数据
  9. 2010-04-02
  10. Wine——在Linux上运行Windows软件
  11. Oracle bpm实现oa,谈谈BPM、工作流引擎与OA的关系
  12. 如何关闭计算机远程桌面连接,怎么用命令行开启和关闭远程桌面?
  13. ACM算法竞赛入门 概述
  14. 如何利用JClassLib修改.class文件
  15. 模电学习1. 三极管基础知识及常用电路
  16. python3操作win32com报错:AttributeError: Open.SaveAs 或者<COMObject Open>“, line 8, in SaveAs pywintypes.c
  17. hourglass论文_Stacked Hourglass networks
  18. [K/3Cloud]关于选单操作
  19. COVID-19席卷全球,看看GIS建模可视化能做些什么
  20. OSChina 周五乱弹 ——喵星生物学:喵和喵奴

热门文章

  1. php 获取所有表,php – 获取所有插件的列表
  2. shell 引号 解析 逗号_关于shell的单引号和双引号转义 以及特殊符号相关
  3. php js统计链接点击次数,JS实现在线统计一个页面内鼠标点击次数的方法
  4. 相机校正与相机内参、外参
  5. 用开满鲜花的情怀对待每一份求知欲
  6. 学习的本质在于触发了你的思考
  7. 不愿说再见 | 自动化系2019年毕业典礼发言
  8. html模板编辑器,可视化编辑网站模板
  9. internetreadfile读取数据长度为0_Hadoop 读取数据
  10. 南京微盟计算机,南京微盟 ME6118A50B3G ME6119A33PG ME6119A50PG 稳压IC