当前位置: > 投稿>正文

运行shell脚本的三种方式,shell脚本的三种运行方式(关于shell脚本执行的几种方法)

04-11 互联网 未知 投稿

关于【运行shell脚本的三种方式】,shell脚本的三种运行方式,今天犇涌小编给您分享一下,如果对您有所帮助别忘了关注本站哦。

1、运行shell脚本的三种方式:关于shell脚本执行的几种方法

如果此时需要在终端中输入一系列命令完成一些操作,但是我们一条一条输入命令,这样就会很麻烦。

shell 脚本类似 windows 的批处理文件,shell 脚本就是将连续执行的命令写成一个文件。

shell 脚本提供数组、循环、条件判断等功能。shell 脚本一般是 Linux 运维或者系统管理员需要掌握的。

1.采用banh或sh+脚本的相对路径或绝对路径(不用赋予脚本+x权限),本质是解释器帮你执行脚本

bash test.shsh test.sh

2.采用脚本的绝对路径或者相对路径执行脚本(必 须赋予脚本执行权限),本质是脚本自己执行,所以需要权限。

chmod +x test.sh./test.sh

封面自提:

2、运行shell脚本的三种方式,shell脚本的三种运行方式

shell脚本的三种运行方式?#shell编程##linux##我要头条推荐##常见的调试命令工具,今天小编就来说说关于shell脚本的三种运行方式?下面更多详细答案一起来看看吧!

运行shell脚本的三种方式,shell脚本的三种运行方式(关于shell脚本执行的几种方法)

shell脚本的三种运行方式

#shell编程##linux##我要头条推荐#

#常见的调试命令工具

1.使用bash命令参数调试

#使用[root@game ~]# sh [-xvn] test.sh#参数解释:-x:将执行的脚本内容输出出来,可以看到执行的过程-n:不执行脚本,检查脚本语法是否有问题,给出错误的提示-v:执行脚本时,先将脚本的内容输出到屏幕上,再执行脚本,如果有错误给出错误提示

#示例

使用-n参数进行语法检查

#说明:不会执行脚本,只检查有无语法错误,如果没有检测到,就无输出[root@game scripts]# cat test7.sh #!/bin/bashecho "guoke123"[root@game scripts]# sh -n test7.sh #脚本没有语法错误,所以没有输出#演示脚本存在语法错误#!/bin/bashif [ `netstat -untpl | grep httpd | wc -l` -gt 0 ];then echo "httpd is Runningelse echo "httpd service down" | mail -s "httpd" 10588@qq.com systemctl restart httpdfi[root@game scripts]# sh -n test1.sh test1.sh: line 5: unexpected EOF while looking for matching `"'test1.sh: line 8: syntax error: unexpected end of file#提示:第5行结尾没有双引号

-v参数:打印错误

[root@game scripts]# sh -v test1.sh #!/bin/bashif [ `netstat -untpl | grep httpd | wc -l` -gt 0 ];then echo "httpd is Runningelse echo "httpd service down" | mail -s "httpd" 10588@qq.com systemctl restart httpdfitest1.sh: line 5: unexpected EOF while looking for matching `"'test1.sh: line 8: syntax error: unexpected end of file

-x参数:打印执行过程

#!/bin/bashif [ `netstat -untpl | grep httpd | wc -l` -gt 0 ];then echo "httpd is Running"else echo "httpd service down" | mail -s "httpd" 10588@qq.com systemctl restart httpdfi#打印执行过程[root@game scripts]# sh -x test1.sh netstat -untpl wc -l grep httpd '[' 0 -gt 0 ']' echo 'httpd service down' mail -s httpd 10588@qq.com systemctl restart httpd

2.使用set命令调试

#常用选项set -n :读命令但并不执行set -v : 显示读取的所有行set -x : 显示所有命令及其参数

#使用

使用set -x可以缩小调试的作用域范围set -x开启调试功能,set x关闭调试功能#示例#!/bin/bashset -xfor i in `seq 9`do for n in `seq 9` do [ $i -ge $n ] && echo -en "$i x $n" = $(expr $i \* $n) doneset xecho " "done#执行效果[root@game scripts]# sh test6.sh seq 9 for i in '`seq 9`' seq 9 for n in '`seq 9`' '[' 1 -ge 1 ']' expr 1 '*' 1 echo -en '1 x 1' = 11 x 1 = 1 for n in '`seq 9`' '[' 1 -ge 2 ']'..... for n in '`seq 9`' '[' 1 -ge 9 ']' set x#提示:只调试了set -x 和set x 这个作用域

3.echo命令调试

一般在可能出现问题的脚本的重要部分加入echo命令#示例[root@game scripts]# cat test8.sh #!/bin/bashread -p "please input tow num:" a becho $a $bexit#执行效果[root@game scripts]# sh test8.sh please input tow num:1 21 2

4.bashdb

shell调试器bashdb是一个类似GDB的调试工具,可以完成对shell脚本的断点设置、单步执行、变量观察等许多功能。

5.shellcheck

shellcheck是一个可检查sh/bash脚本和命令语法的小工具

#常见的shell脚本错误示例

#1.中括号两端没有空格

[root@game scripts]# cat test.sh #!/bin/bashyum install net-tools -y >/dev/nullif [$? -eq 0] then echo "install success" else echo "install fail"fi#执行:报错[root@game scripts]# sh test.sh test.sh: line 4: [0: command not foundinstall fail#提示:错误在第四行

#2.成对的符号没有写全,漏写

#成对的符号例如:()、[]、""、''等#示例[]中括号没有写全[root@game scripts]# cat test1.sh #!/bin/bashif [ `netstat -untpl | grep httpd | wc -l` -gt 0 ;then echo "httpd is Running"else echo "httpd service down" | mail -s "httpd" 10588@qq.com systemctl restart httpdfi#执行效果[root@game scripts]# sh test1.sh test1.sh: line 3: [: missing `]'

#3.if条件语句缺少结尾关键字

[root@game scripts]# cat test2.sh #!/bin/bashif [ `netstat -untpl | grep mysqld | wc -l` -gt 0 ];then echo "mysqld is Running"else echo "mysqld service down" | mail -s "mysqld" 1792988@qq.com systemctl restart mysqld#执行效果[root@game scripts]# sh test2.sh test2.sh: line 8: syntax error: unexpected end of file#执行脚本会提示第8行语法错误

#4.循环语句缺少关键字

本文关键词:运行shell脚本的三种方式是,运行shell脚本程序的三种方式?,运行shell脚本命令 sh,shell脚本中执行命令,shell脚本执行shell脚本。这就是关于《运行shell脚本的三种方式,shell脚本的三种运行方式(关于shell脚本执行的几种方法)》的所有内容,希望对您能有所帮助!更多的知识请继续关注《犇涌向乾》百科知识网站:http://www.029ztxx.com!

版权声明: 本站仅提供信息存储空间服务,旨在传递更多信息,不拥有所有权,不承担相关法律责任,不代表本网赞同其观点和对其真实性负责。如因作品内容、版权和其它问题需要同本网联系的,请发送邮件至 举报,一经查实,本站将立刻删除。

猜你喜欢