急需高手帮忙指点下简单脚本调用问题

sh/bash/dash/ksh/zsh等Shell脚本
回复
brucehe
帖子: 7
注册时间: 2011-09-27 22:47

急需高手帮忙指点下简单脚本调用问题

#1

帖子 brucehe » 2012-04-02 10:47

该脚本如下:
#!/bin/bash
# father script.
echo "this is the father"
FILM="A Few Good Men"
echo "I like the film :$FILM"
# call the child script
# but export variable first
export FILM
child
echo "back to father"
echo "and the film is :$FILM"
调用的“child”脚本如下:
#!/bin/bash
# child
echo "called from father..i am child"
echo "film name is :$FILM"
FILM="Die Hard"
echo "changing film to :$FILM"
|运行father脚本不能调用child脚本,具体问题为”./father:9:child: not found"。
希望能得到高手指点,菜鸟非常感谢。
头像
枫叶饭团
帖子: 14683
注册时间: 2010-06-16 1:05
系统: Mac OS X
来自: Tencent
联系:

Re: 急需高手帮忙指点下简单脚本调用问题

#2

帖子 枫叶饭团 » 2012-04-02 10:57

代码: 全选

[maplebeats@maplebeats bash]$ cat father 
#!/bin/bash
# father script.
echo "this is the father"
FILM="A Few Good Men"
echo "I like the film :$FILM"
# call the child script
# but export variable first
export FILM
./child
echo "back to father"
echo "and the film is :$FILM"
[maplebeats@maplebeats bash]$ sh father
this is the father
I like the film :A Few Good Men
called from father..i am child
film name is :A Few Good Men
changing film to :Die Hard
back to father
and the film is :A Few Good Men
[maplebeats@maplebeats bash]$ 
头像
redhatlinux10
帖子: 773
注册时间: 2008-01-22 23:24
来自: 三亚
联系:

Re: 急需高手帮忙指点下简单脚本调用问题

#3

帖子 redhatlinux10 » 2012-04-02 11:00

brucehe 写了:该脚本如下:
#!/bin/bash
# father script.
echo "this is the father"
FILM="A Few Good Men"
echo "I like the film :$FILM"
# call the child script
# but export variable first
export FILM
child
echo "back to father"
echo "and the film is :$FILM"
调用的“child”脚本如下:
#!/bin/bash
# child
echo "called from father..i am child"
echo "film name is :$FILM"
FILM="Die Hard"
echo "changing film to :$FILM"
|运行father脚本不能调用child脚本,具体问题为”./father:9:child: not found"。
希望能得到高手指点,菜鸟非常感谢。
你调用child脚本时,不应该是

代码: 全选

child
而应该是

代码: 全选

source /path/to/child.sh
第一种用法是用于调用函数的。
cao627
帖子: 992
注册时间: 2007-12-05 10:57
系统: ubuntu14.04
来自: 金山

Re: 急需高手帮忙指点下简单脚本调用问题

#4

帖子 cao627 » 2012-04-02 11:05

安全原因 shell PATH变量中 没有./ 所以不会在当前目录查找命令
:em11
brucehe
帖子: 7
注册时间: 2011-09-27 22:47

Re: 急需高手帮忙指点下简单脚本调用问题

#5

帖子 brucehe » 2012-04-02 11:19

谢谢枫叶饭团帮忙解决了该问题。谢谢
头像
hoxily
帖子: 39
注册时间: 2011-02-11 21:10

Re: 急需高手帮忙指点下简单脚本调用问题

#6

帖子 hoxily » 2012-04-02 12:32

学习了. :em01
回复