sh命令不调用shell脚本

时间:2021-08-14 14:39:12

I've written a simple shell script that adds some aliases to a shell it is executed as part of an assignment for school. Here is the contents of the file testscript.txt:

我编写了一个简单的shell脚本,它将一些别名添加到shell中,它作为学校作业的一部分执行。以下是文件testscript.txt的内容:

#!/usr/bin

alias dir="ls -l"
alias alerts="cat /etc/motd"
PS1="What next Matt ? "

When I use the following command:

当我使用以下命令时:

sh testscript.txt

The command appears to run since no errors are returned, however, the alias and command prompt variables have not been changed.

该命令似乎运行,因为没有返回任何错误,但是,别名和命令提示符变量尚未更改。

I know that the script works properly because I have tried the following three alternatives, which all worked without a hitch:

我知道该脚本正常工作,因为我尝试了以下三种替代方案,这些方法都顺利运行:

. ./testscript.txt
. testscript.txt
source testscript.txt

The assignment requires that I use the sh command. Any suggests to make the command update the aliases and environmental variables?

赋值需要我使用sh命令。有没有建议让命令更新别名和环境变量?

Here is a screenshot of the assignment so you can see the requirements I must fulfil: sh命令不调用shell脚本

这是作业的屏幕截图,因此您可以看到我必须满足的要求:

Thank you for you time and help.

谢谢你的时间和帮助。

2 个解决方案

#1


0  

Any environment variables you set up inside a stand-alone script will only be visible from within that script. instead, you need to add these commands to ~/.profile (or whatever its equivalent is on your system). Then they will be executed when you log into the command line console.

您在独立脚本中设置的任何环境变量只能在该脚本中可见。相反,您需要将这些命令添加到〜/ .profile(或其系统上的等效命令)。然后,当您登录命令行控制台时,它们将被执行。

#2


-1  

The shebang (first line) is not correct.

shebang(第一行)不正确。

In your case, you should replace the first line by either #!/bin/sh or #!/bin/bash.

在您的情况下,您应该用#!/ bin / sh或#!/ bin / bash替换第一行。

Just search for 'shebang' in Google. It is basically the absolute path of the launched executable. http://en.wikipedia.org/wiki/Shebang_(Unix)

只需在Google中搜索“shebang”即可。它基本上是启动的可执行文件的绝对路径。 http://en.wikipedia.org/wiki/Shebang_(Unix)

#1


0  

Any environment variables you set up inside a stand-alone script will only be visible from within that script. instead, you need to add these commands to ~/.profile (or whatever its equivalent is on your system). Then they will be executed when you log into the command line console.

您在独立脚本中设置的任何环境变量只能在该脚本中可见。相反,您需要将这些命令添加到〜/ .profile(或其系统上的等效命令)。然后,当您登录命令行控制台时,它们将被执行。

#2


-1  

The shebang (first line) is not correct.

shebang(第一行)不正确。

In your case, you should replace the first line by either #!/bin/sh or #!/bin/bash.

在您的情况下,您应该用#!/ bin / sh或#!/ bin / bash替换第一行。

Just search for 'shebang' in Google. It is basically the absolute path of the launched executable. http://en.wikipedia.org/wiki/Shebang_(Unix)

只需在Google中搜索“shebang”即可。它基本上是启动的可执行文件的绝对路径。 http://en.wikipedia.org/wiki/Shebang_(Unix)