shell脚本

shell脚本怎么调用其他shell脚本

在Shell中要如何调用别的shell脚本,或别的脚本中的变量,函数呢?
方法一: . ./subscript.sh
方法二: source ./subscript.sh

方法三:bash /path/to/get.sh


注意:
1.两个dao点之间,有空格,千万注意.
2.两个脚本不在同一目录,要用绝对路径
3.为简单起见,通常用第一种方法
例如:
复制代码代码如下:
main.sh #主脚本
subscripts.sh #子脚本,或者说被调脚本
[code]
[code]
###subscripts.sh 脚本内容如下:###
#!/bin/bash
string="Hello,World! "
复制代码代码如下:
###main.sh 脚本内容如下###
#!/bin/bash
. ./subscripts.sh
echo -e ${string}
exit 0
输出结果:
复制代码代码如下:
# chmod +x ./main.sh
# ./main.sh
Hello,World!
#

如何调试shell脚本?

方法一:sh -x script.sh
-x选项会将运行到bai的脚本内容显示在屏幕上,前面du有个+号。这zhi样就知道哪句被执行到了dao。对调试很有帮助。
方法二:在脚本中使用debug开关
适用于只调试部分脚本的情况。
set -x
......(要调试的代码段)
set +x
然后sh script.sh运行脚本
方法三:在脚本中添加打印
比如搞个log输出函数,专门用于打印调试相关信息。
_log()
{
if [ "$_DEBUG" = "true" ]; then
echo "调试信息"
fi
}
只要在外面控制$_DEBUG开关即可。

# bash --help
GNU bash, version 4.2.46(2)-release-(x86_64-redhat-linux-gnu)
Usage: bash [GNU long option] [option] ...
bash [GNU long option] [option] script-file ...
GNU long options:
--debug
--debugger
--dump-po-strings
--dump-strings
--help
--init-file
--login
--noediting
--noprofile
--norc
--posix
--protected
--rcfile
--rpm-requires
--restricted
--verbose
--version
Shell options:
-irsD or -c command or -O shopt_option (invocation only)
-abefhkmnptuvxBCHP or -o option
Type `bash -c "help set"' for more information about shell options.
Type `bash -c help' for more information about shell builtin commands.
[root@node4 ~]# bash -c help
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
These shell commands are defined internally. Type `help' to see this list.
Type `help name' to find out more about the function `name'.
Use `info bash' to find out more about the shell in general.
Use `man -k' or `info' to find out more about commands not in this list.

A star (*) next to a name means that the command is disabled.

job_spec [&] history [-c] [-d offset] [n] or hist>
(( expression )) if COMMANDS; then COMMANDS; [ elif C>
. filename [arguments] jobs [-lnprs] [jobspec ...] or jobs >
: kill [-s sigspec | -n signum | -sigs>
[ arg... ] let arg [arg ...]
[[ expression ]] local [option] name[=value] ...
alias [-p] [name[=value] ... ] logout [n]
bg [job_spec ...] mapfile [-n count] [-O origin] [-s c>
bind [-lpvsPVS] [-m keymap] [-f filen> popd [-n] [+N | -N]
break [n] printf [-v var] format [arguments]
builtin [shell-builtin [arg ...]] pushd [-n] [+N | -N | dir]
caller [expr] pwd [-LP]
case WORD in [PATTERN [| PATTERN]...)> read [-ers] [-a array] [-d delim] [->
cd [-L|[-P [-e]]] [dir] readarray [-n count] [-O origin] [-s>
command [-pVv] command [arg ...] readonly [-aAf] [name[=value] ...] o>
compgen [-abcdefgjksuv] [-o option] > return [n]
complete [-abcdefgjksuv] [-pr] [-DE] > select NAME [in WORDS ... ;] do COMM>
compopt [-o|+o option] [-DE] [name ..> set [-abefhkmnptuvxBCHP] [-o option->
continue [n] shift [n]
coproc [NAME] command [redirections] shopt [-pqsu] [-o] [optname ...]
declare [-aAfFgilrtux] [-p] [name[=va> source filename [arguments]
dirs [-clpv] [+N] [-N] suspend [-f]
disown [-h] [-ar] [jobspec ...] test [expr]
echo [-neE] [arg ...] time [-p] pipeline
enable [-a] [-dnps] [-f filename] [na> times
eval [arg ...] trap [-lp] [[arg] signal_spec ...]
exec [-cl] [-a name] [command [argume> true
exit [n] type [-afptP] name [name ...]
export [-fn] [name[=value] ...] or ex> typeset [-aAfFgilrtux] [-p] name[=va>
false ulimit [-SHacdefilmnpqrstuvx] [limit>
fc [-e ename] [-lnr] [first] [last] o> umask [-p] [-S] [mode]
fg [job_spec] unalias [-a] name [name ...]
for NAME [in WORDS ... ] ; do COMMAND> unset [-f] [-v] [name ...]
for (( exp1; exp2; exp3 )); do COMMAN> until COMMANDS; do COMMANDS; done
function name { COMMANDS ; } or name > variables - Names and meanings of so>
getopts optstring name [arg] wait [id]
hash [-lr] [-p pathname] [-dt] [name > while COMMANDS; do COMMANDS; done
help [-dms] [pattern ...] { COMMANDS ; }

1) 检查语法错误: 一般来说我们可以通过修改shell脚本的源代码,令其输出相关的调试信息来定位错误,那有没有不修改源代码来调试shell脚本的方法呢?答案就是使用shell的执行选,下面是一些常用选项的用法: -n 只读取shell脚本,但不实际执行 -x 进入跟踪方式,显示所执行的每一条命令 -c "string" 从strings中读取命令“-n”可用于测试shell脚本是否存在语法错误,但不会实际执行命令。在shell脚本编写完成之后,实际执行之前,首先使用“-n”选项来测试脚本是否存在语法错误是一个很好的习惯。因为某些shell脚本在执行时会对系统环境产生影响,比如生成或移动文件等,如果在实际执行才发现语法错误,您不得不手工做一些系统环境的恢复工作才能继续测试这个脚本。“-c”选项使shell解释器从一个字符串中而不是从一个文件中读取并执行shell命令。当需要临时测试一小段脚本的执行结果时,可以使用这个选项,如下所示: sh -c 'a=1;b=2;let c=$a+$b;echo "c=$c"'"-x"选项可用来跟踪脚本的执行,是调试shell脚本的强有力工具。“-x”选项使shell在执行脚本的过程中把它实际执行的每一个命令行显示出来,并且在行首显示一个"+"号。 "+"号后面显示的是经过了变量替换之后的命令行的内容,有助于分析实际执行的是什么命令。 “-x”选项使用起来简单方便,可以轻松对付大多数的shell调试任务,应把其当作首选的调试手段。2) 调试工具-bashdb 使用shell调试器bashdb,这是一个类似于GDB的调试工具,可以完成对shell脚本的断点设置,单步执行,变量观察等许多功能。使用bashdb进行debug的常用命令 1.列出代码和查询代码类: l 列出当前行以下的10行 - 列出正在执行的代码行的前面10行 . 回到正在执行的代码行 w 列出正在执行的代码行前后的代码 /pat/ 向后搜索pat ?pat?向前搜索pat2.Debug控制类:h 帮助help 命令 得到命令的具体信息 q 退出bashdb x 算数表达式 计算算数表达式的值,并显示出来 !!空格Shell命令 参数 执行shell命令 使用bashdb进行debug的常用命令(cont.) 控制脚本执行类: n 执行下一条语句,遇到函数,不进入函数里面执行,将函数当作黑盒 s n 单步执行n次,遇到函数进入函数里面 b 行号n 在行号n处设置断点 del 行号n 撤销行号n处的断点 c 行号n 一直执行到行号n处R 重新启动Finish 执行到程序最后。

bash --verbose info.sh

 
原文地址:https://www.cnblogs.com/BillyLV/p/13596732.html