请教一个shell中source使用问题

sh/bash/dash/ksh/zsh等Shell脚本
回复
虫虫2003
帖子: 51
注册时间: 2006-03-27 19:28

请教一个shell中source使用问题

#1

帖子 虫虫2003 » 2011-08-17 15:03

我用是ubuntu10.10,发现在shell脚本中执行source有点问题。
比如在同一目录下有两个脚本
a.sh

代码: 全选

#!/bin/sh
export A=aaaa
b.sh

代码: 全选

#!/bin/sh
source a.sh
在执行b.sh时会报找不到a.sh,而如果b.sh中改成source ./a.sh就可以了。
这是怎么回事?同样的脚本放在Red Hat中试是正常的。
:em23 :em23
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 请教一个shell中source使用问题

#2

帖子 eexpress » 2011-08-17 15:09

路径而已
source可以用.
● 鸣学
虫虫2003
帖子: 51
注册时间: 2006-03-27 19:28

Re: 请教一个shell中source使用问题

#3

帖子 虫虫2003 » 2011-08-17 16:37

eexpress 写了:路径而已
source可以用.
为什么需要路径呢?在当前文件夹下不需要啊!
而且如果我在命令行直接输source a.sh
也可以,不要带路径。
虫虫2003
帖子: 51
注册时间: 2006-03-27 19:28

Re: 请教一个shell中source使用问题

#4

帖子 虫虫2003 » 2011-08-17 21:52

有人知道吗?
jtshs256
帖子: 22323
注册时间: 2010-07-19 21:41
系统: OS X

Re: 请教一个shell中source使用问题

#5

帖子 jtshs256 » 2011-08-17 22:01

bash?dash?……
躺平
虫虫2003
帖子: 51
注册时间: 2006-03-27 19:28

Re: 请教一个shell中source使用问题

#6

帖子 虫虫2003 » 2011-08-19 0:33

jtshs256 写了:bash?dash?……
bash!
我试验了一下,在最新版的debian、fedora、centos上都不行,而那个老版本red hat可以,是不是shell的语法改了??
头像
灰色小狼
帖子: 4585
注册时间: 2008-12-06 10:38
系统: Arch

Re: 请教一个shell中source使用问题

#7

帖子 灰色小狼 » 2011-08-19 0:35

可能是,还是带路径更严谨些吧
kanger
帖子: 86
注册时间: 2011-11-19 18:29
系统: 12.10

Re: 请教一个shell中source使用问题

#8

帖子 kanger » 2013-09-05 16:01

b.sh
代码:
#!/bin/sh
source a.sh

中的sh换用dash代替了bash,所以不可以。
你可以在用到source 命令的shell中:
#!/bin/bash
source a.sh

或者整个都给变了。如下:(来源:http://blog.csdn.net/mvpme82/article/details/7615454
现象: shell脚本中source aaa.sh时提示 source: not found


原因: ls -l `which sh` 提示/bin/sh -> dash

这说明是用dash来进行解析的。


改回方法:
命令行执行:sudo dpkg-reconfigure dash
在界面中选择no

再ls -l `which sh` 提示/bin/sh -> bash


修改成功,source可以用了~
kanger
帖子: 86
注册时间: 2011-11-19 18:29
系统: 12.10

Re: 请教一个shell中source使用问题

#9

帖子 kanger » 2013-09-05 16:20

b.sh
代码:
#!/bin/sh
source a.sh

中的sh换用dash代替了bash,所以不可以。
你可以在用到source 命令的shell中:
#!/bin/bash
source a.sh

或者整个都给变了。如下:(来源:http://blog.csdn.net/mvpme82/article/details/7615454
现象: shell脚本中source aaa.sh时提示 source: not found


原因: ls -l `which sh` 提示/bin/sh -> dash

这说明是用dash来进行解析的。


改回方法:
命令行执行:sudo dpkg-reconfigure dash
在界面中选择no

再ls -l `which sh` 提示/bin/sh -> bash


修改成功,source可以用了~
悟空and兰博
帖子: 34
注册时间: 2012-09-25 18:20
系统: ubuntu

Re: 请教一个shell中source使用问题

#10

帖子 悟空and兰博 » 2013-09-05 16:58

你的a.sh是否有执行权限,./只能执行有执行权限的文件,而source却可以执行没有执行权限的文件。
aerofox
帖子: 1453
注册时间: 2008-05-24 8:30

Re: 请教一个shell中source使用问题

#11

帖子 aerofox » 2013-09-05 19:25

代码: 全选

        .  filename [arguments]
       source filename [arguments]
              读取并在当前       shell       环境中执行      filename      中的命令,返回      filename
              中最后一个命令的返回状态。如果 filename 中不包含斜杠  (slash),系统将在  PATH  中查找包含
              filename 的目录。在 PATH 中搜索的文件不必是可执行的。 如果 bash 不是运行于 posix mode,当
              PATH 中找不到文件时会在当前目录搜索。如果 shopt 内建命令的 sourcepath  选项被关闭,  PATH
              将不会被搜索。如果有任何    arguments    ,它们成为   filename   的位置参数   (positional
              parameters),否则     位置参数不发生变化。     返回状态是脚本中最后一个命令退出时的状态。
              没有执行命令则返回0,没有找到或不能读取 filename 时返回false。
回复