简体   繁体   English

sh命令未调用shell脚本

[英]sh command is not calling shell script

I've written a simple shell script that adds some aliases to a shell it is executed as part of an assignment for school. 我已经编写了一个简单的shell脚本,该脚本将一些别名添加到作为学校作业的一部分执行的shell中。 Here is the contents of the file testscript.txt : 这是文件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. 分配要求我使用sh命令。 Any suggests to make the command update the aliases and environmental variables? 有没有建议使命令更新别名和环境变量?

Here is a screenshot of the assignment so you can see the requirements I must fulfil: 这是作业的屏幕截图,因此您可以看到我必须满足的要求: 在此处输入图片说明

Thank you for you time and help. 谢谢您的时间和帮助。

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). 相反,您需要将这些命令添加到~/.profile (或系统上的任何等效文件)。 Then they will be executed when you log into the command line console. 然后,当您登录命令行控制台时将执行它们。

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. 只需在Google中搜索“ shebang”即可。 It is basically the absolute path of the launched executable. 它基本上是启动的可执行文件的绝对路径。 http://en.wikipedia.org/wiki/Shebang_(Unix) http://en.wikipedia.org/wiki/Shebang_(Unix)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM