“来源" Shell脚本中的命令不起作用 [英] "source" command in shell script not working

查看:99
本文介绍了“来源" Shell脚本中的命令不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要在Centos 7中获取的文件. 如果这样做,它就可以正常工作:

I have a file to be sourced in Centos 7. It just works fine if I do :

$ source set_puregev_env

但是,如果我将其放入shell脚本中,它将无法正常工作.

however, if I put this in a shell script, it doesn't work..

$ sh xRUN 
xRUN: line 3: source: set_puregev_env: file not found

这是我的shell脚本:xRUN

this is my shell script : xRUN

#!/bin/bash

source set_puregev_env

谁能告诉我我可能做错了什么,或者想念什么?

can anyone tell me what I might be doing wrong, or missing?

推荐答案

source是在bash中实现的命令,但不是在sh中实现的命令. 有多种方法可以修复脚本.选择一个.

source is a command implemented in bash, but not in sh. There are multiple ways to fix your script. Choose either one.

使用bash解释器运行脚本

在调用xRUN脚本时-您明确告诉它由sh

When you are invoking the xRUN script - you are explicitly telling it to be interpreted by sh

$ sh xRUN 

要使用bash更改和解释脚本,请执行

To change and interpret the script with bash instead do

$ bash xRUN 

这将使bash解释源命令,您的脚本将起作用.

This will make bash interpret the source command, and your script will work.

使用点命令使脚本本源兼容

您还可以使用dot命令更改源代码,该命令执行相同的操作,但bourne和bash均受支持.

You can also change the source with a dot command which does the same thing but is supported in both bourne and bash.

更改行:

source set_puregev_env

使用:

. set_puregev_env 

现在该脚本将与sh或bash一起使用.

Now the script will work with either sh or bash.

使脚本可执行

您还应该直接运行该脚本,以免执行此类混淆,方法是将其设置为可执行文件chmod +x xRUN并调用它,如下所示:

You should also run the script directly to avoid confusions like these by making it executable chmod +x xRUN, and invoking it like this:

$ ./xRUN

然后它将使用shebang中指定的命令,并将脚本的其余部分用作输入.在您的情况下,它将使用bash-因为在shebang中已指定.

It will then use the command specified in the shebang and use the rest of the script as input. In your case it will use bash - since that is specified in the shebang.

这篇关于“来源" Shell脚本中的命令不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆