互力互通网站建设,网站建设初期工作方案,wordpress安装主题需要ftp,wap手机网站描述正确的是有时会在一个shell脚本(如test_call_other_shell.sh)中调用另外一个shell脚本(如parameter_usage.sh)#xff0c;这里总结几种可行的方法#xff0c;这些方法在linux上和windows上(通过Git Bash)均适用#xff1a; 1.通过source: 运行在相同的进程#xff0c;在test_… 有时会在一个shell脚本(如test_call_other_shell.sh)中调用另外一个shell脚本(如parameter_usage.sh)这里总结几种可行的方法这些方法在linux上和windows上(通过Git Bash)均适用 1.通过source: 运行在相同的进程在test_call_other_shell.sh中调用parameter_usage.sh后parameter_usage.sh中的变量和函数在test_call_other_shell.sh中可直接使用 2.通过/bin/bash: 运行在不同的进程 3.通过sh: 运行在不同的进程 4.通过.: 运行在相同的进程在test_call_other_shell.sh中调用parameter_usage.sh后parameter_usage.sh中的变量和函数在test_call_other_shell.sh中可直接使用 parameter_usage.sh内容如下
#! /bin/bash# 参数的使用# 我们可以在执行Shell脚本时,向脚本传递参数,脚本内获取参数的格式为:$n. n代表一个数字,1为执行脚本的第一个参数2为执行脚本的第二个参数以此类推if [ $# ! 3 ]; thenecho usage: $0 param1 param2 param3echo e.g: $0 1 2 3exit 1
fiecho 执行文件名 $0
echo param1: $1; echo param2: $2; echo param3: $3parameters$*# 特殊字符用来处理参数
# $#: 传递到脚本的参数个数
echo 参数个数为 $#
# $*: 以一个单字符串显示所有向脚本传递的参数
echo 传递的参数作为一个字符串显示: $*
# $: 与$*相同但是使用时加引号并在引号中返回每个参数
echo 传递的参数作为字符串显示: $for i in $*; do # 循环一次echo loop; echo $i
doneecho
for i in $; do # 循环三次echo loop; echo $i
doneget_csdn_addr()
{echo csdn addr: https://blog.csdn.net/fengbingchun/
} test_call_other_shell.sh内容如下
#! /bin/bashparams(source /bin/bash sh .)usage()
{echo Error: $0 needs to have an input parameterecho supported input parameters:for param in ${params[]}; doecho $0 ${param}doneexit -1
}if [ $# ! 1 ]; thenusage
fiflag0
for param in ${params[]}; doif [ $1 ${param} ]; thenflag1breakfi
doneif [ ${flag} 0 ]; thenecho Error: parameter \$1\ is not supportedusageexit -1
fiecho test $1 $1 parameter_usage.sh 1 2 3
echo parameters: ${parameters}
get_csdn_addr$1 parameter_usage 123
#ret$?
#if [[ ${ret} ! 0 ]]; then
# echo ##### Error: some of the above commands have gone wrong, please check: ${ret}
# exit ${ret}
#fi
if [ $? -ne 0 ]; thenecho ##### Error: some of the above commands have gone wrong, please checkexit -1
fiecho test finish 在linux上的执行结果如下 在windows上执行结果如下 在linux下也可以将另外一个shell脚本所在的路径添加到$PATH环境变量然后你就可以把它作为普通命令调用。 GitHub https://github.com/fengbingchun/Linux_Code_Test