One - One Code All

Blog Content

linux shell脚本中引用、调用另一个脚本文件的2种方法

Linux-Mac   2008-11-13 22:23:11

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

注意:
1.两个点之间,有空格,千万注意.
2.两个脚本不在同一目录,要用绝对路径
3.为简单起见,通常用第一种方法

例如:
复制代码 代码如下:

main.sh           #主脚本
subscripts.sh     #子脚本,或者说被调脚本


脚本如下:

### subscripts.sh 脚本内容如下:###  
  
#!/bin/bash  
string="Hello,World! \n"
### main.sh 脚本内容如下###  
  
#!/bin/bash  
. ./subscripts.sh  
echo -e ${string}  
exit 0


输出结果:

# chmod +x ./main.sh  
# ./main.sh  
Hello,World!  
#  
注意:

1.被调脚本可以没有执行权限,调用脚本必须有可执行权限
2.chmod +x ./main.sh   #注意这里要有点,否则bash脚本可能找不到


上一篇: QPS与TPS
下一篇:excel单元格换行alt+enter

The minute you think of giving up, think of the reason why you held on so long.