无法在 redhat linux 中执行 bash 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7720306/
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 00:56:38  来源:igfitidea点击:

Cannot execute bash script in redhat linux

linuxbashshell

提问by Spark

I write a simple shell script to clean log files in redhat:

我写了一个简单的 shell 脚本来清理 redhat 中的日志文件:

Filename: clean.sh

文件名:clean.sh

#!/bin/bash
rm -f *.log core.*

But when I typed clean or clean.sh, it always prompt

但是当我输入clean或clean.sh时,总是提示

-bash: clean: command not found
-bash: clean.sh: command not found

What's the problem?

有什么问题?

回答by Keith Thompson

You probably don't have .(the current directory) in your $PATH(and that's a good thing; having .in your $PATHcan be dangerous.)

您可能没有.(当前目录)$PATH(这是一件好事;.在您的目录中$PATH可能很危险。)

Try this:

尝试这个:

./clean.sh

And if the script file's name is clean.shyou can't run it as just clean, with or without a directory. The file name is clean.sh, and that's how you need to execute it.

如果脚本文件的名称是,则无论有没有目录,clean.sh您都不能将其作为 运行clean。文件名是clean.sh,这就是您需要执行它的方式。

Or you can change the name from clean.shto just clean. Unix-like systems (that includes Linux) don't depend on file extensions the way Windows does.

或者您可以将名称从 更改clean.sh为 just clean。类 Unix 系统(包括 Linux)不像 Windows 那样依赖于文件扩展名。

回答by yati sagade

problem 1: maybe the execute permission on clean.sh is not set. Do this:

问题一:可能是没有设置clean.sh的执行权限。做这个:

chmod +x ./clean.sh

problem 2: RH Linux does not include CWD on the path by default. So, when you are in the same directory as clean.sh, type:

问题二:RH Linux默认路径上不包含CWD。因此,当您在与 clean.sh 相同的目录中时,键入:

./clean.sh

That should execute it.

那应该执行它。