只是 bash 脚本不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22150233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:47:30  来源:igfitidea点击:

simply bash script doesn't work

bashecho

提问by user3164700

#!/bin/bash  
echo "hello"

I run this script and I have got only empty space why? I am sure my code is okay, so why don't show text's?

我运行这个脚本,但我只有空白空间,为什么?我确定我的代码没问题,那么为什么不显示文本呢?

回答by MLSC

please specify what the problem is.

请说明是什么问题。

If your script doesn't run:

如果您的脚本没有运行:

For running scripts you should make it executable:

要运行脚本,您应该使其可执行:

chmod +x script.sh

Another thing that you should know is about SELINUXyou should make sure it is sat or not...and specify at which directory it is running?

您应该知道的另一件事是关于SELINUX,您应该确保它是否已安装...并指定它在哪个目录中运行?

please run this command and let me know:

请运行此命令并让我知道:

ls -l scipt.sh

IF echocommand doesn't work:

IFecho命令不起作用:

At first run it in your terminal and see the result...may be the command echois removed or not...

首先在您的终端中运行它并查看结果...可能是该命令echo已删除或未删除...

try to run this bash:

尝试运行这个 bash:

#!/bin/bash
echo "hello"
echo $?

and try this one:

试试这个:

#!/bin/bash          
STR="Hello World!"
echo $STR 

And let me know about output. better to ask your question at unix Q&Aor superuser

让我知道输出。最好在unix Q&A超级用户处提问

回答by Gurubaran

Execute the below commands in the terminal to troubleshoot.

在终端中执行以下命令进行故障排除。

  • Check to see what shell you are on.
  • 检查您使用的是什么外壳。

The below command would tell you your current shell.

下面的命令会告诉你你当前的 shell。

echo $SHELL 
  • Once you confirm that make sure that you have the bash shell present in the /bin directory.

    /bin/bash
    echo "hello"

  • 一旦确认,请确保 /bin 目录中存在 bash shell。

    /bin/bash
    回声“你好”

回答by CloseISQ

I ran into the same puzzling issue when running my hello world script from a terminal window, using Ubuntu. I was editing the file using gedit, giving all permissions from the properties gedit functionality, and when running ./hello_world.sh, further chmoded to +x from a terminal window as well, it simply wouldn't show the result of my echo "Hello World" command.

使用 Ubuntu 从终端窗口运行我的 hello world 脚本时,我遇到了同样令人费解的问题。我正在使用 gedit 编辑文件,从属性 gedit 功能中授予所有权限,并且在运行 ./hello_world.sh 时,也从终端窗口进一步 chmoded 为 +x,它根本不会显示我的 echo 的结果“你好世界”命令。

Needless to say as I have been through all the above answers and all showed my script should have worked.

不用说,因为我已经完成了上述所有答案,并且都表明我的脚本应该有效。

I simply solved it by editing the script using vim from a terminal window! Then chmod +x again just in case, ran the script and Hello World finally appeared!

我只是通过在终端窗口中使用 vim 编辑脚本来解决它!然后再次 chmod +x 以防万一,运行脚本,Hello World 终于出现了!

But I don't know why it didn't work using the gedit edition;

但我不知道为什么它在使用 gedit 版本时不起作用;

回答by xiaojueguan

make sure u've already made your content to your script file. In some case, if u use some editor like vscode it has some option that won't save content to the file.If this is your case, u may change the option.

确保您已经将您的内容添加到您的脚本文件中。在某些情况下,如果您使用诸如 vscode 之类的编辑器,它有一些选项不会将内容保存到文件中。如果是这种情况,您可以更改该选项。