简体   繁体   English

在脚本中调用Shell脚本

[英]Calling shell script within a script

Below are my two script codes:
**a.sh**

#!/bin/sh

SRC_PATH="/xx/xxxx"
HOST='ftp.xxx.xxx.com'
USER='xxxx'
PASS='xxx'
FTP_SRC_PATH='out'

**b.sh** 
#!/bin/sh

/xx/xxx/a.sh

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASS
binary
prompt off
cd $FTP_SRC_PATH
lcd $SRC_PATH
mget IMS_*.ZIP
bye
END_SCRIPT

My issue is when I am running b.sh , it is not calling a.sh and using the variables defined in it to connect to ftp server. 我的问题是当我运行b.sh时,它没有调用a.sh并使用其中定义的变量连接到ftp服务器。 I have seen many solutions already online but things doesn't seem to work. 我已经看到许多解决方案已经在线,但似乎无法正常工作。 Please help 请帮忙

您可以在dot命令中包含a.sh

. /xx/xxx/a.sh

You can call the script a from within b as follows: 您可以从b内调用脚本a,如下所示:

sh ./a.sh sh ./a.sh

Make sure both the shell scripts are in same file path. 确保两个外壳程序脚本都在同一文件路径中。 Also, you can pass the value within scripts as follows: 另外,您可以按以下方式在脚本中传递值:

sh ./a.sh $d $e sh ./a.sh $ d $ e

The values passed here can be accessed as $1 $2. 此处传递的值可以作为$ 1 $ 2访问。

Here is an example: 这是一个例子:

a.sh: 灰:

!/bin/bash !/ bin / bash

yourvalue="test"
sh ./b.sh $yourvalue

and I'm able to access the variable passed here in the shell script b as $1. 并且我能够访问在外壳脚本b中以$ 1形式在此处传递的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM