RET*_*NCE 3 bash shell sh command-line-interface
我有一个要在Centos 7中获取的文件。如果这样做,它可以正常工作:
$ source set_puregev_env
Run Code Online (Sandbox Code Playgroud)
但是,如果我将其放在shell脚本中,它将无法正常工作。
$ sh xRUN
xRUN: line 3: source: set_puregev_env: file not found
Run Code Online (Sandbox Code Playgroud)
这是我的shell脚本:xRUN
#!/bin/bash
source set_puregev_env
Run Code Online (Sandbox Code Playgroud)
谁能告诉我我可能做错了什么,或者想念吗?
source是在bash中实现的命令,但不是在sh中实现的命令。有多种方法可以修复脚本。选择一个。
使用bash解释器运行脚本
调用xRUN脚本时-您明确地告诉脚本要由sh
$ sh xRUN
Run Code Online (Sandbox Code Playgroud)
要使用bash更改和解释脚本,请执行以下操作
$ bash xRUN
Run Code Online (Sandbox Code Playgroud)
这将使bash解释源命令,并且脚本将起作用。
使用dot命令使脚本bourne兼容
您也可以使用dot命令更改源,该命令执行相同的操作,但bourne和bash均受支持。
更改行:
source set_puregev_env
Run Code Online (Sandbox Code Playgroud)
带有:
. set_puregev_env
Run Code Online (Sandbox Code Playgroud)
现在该脚本将与sh或bash一起使用。
使脚本可执行
您还应该直接运行该脚本,以使其避免执行此类混乱,方法是将其设置为可执行文件chmod +x xRUN,然后像这样调用它:
$ ./xRUN
Run Code Online (Sandbox Code Playgroud)
然后它将使用shebang中指定的命令,并将脚本的其余部分用作输入。在您的情况下,它将使用bash-因为在shebang中指定了bash。