bash 从批处理调用时找不到cywin bash脚本命令

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

cywin bash script command not found when called from batch

bashcygwin

提问by rtacconi

#!/bin/bash
echo "Testing"
cd "/cygdrive/x/Internal Communications/Riccardo/"
filename=htdocs-`date +%A`.tar.gz
tar cvzf $filename "/cygdrive/c/Program Files/Zend/Apache2/htdocs"

The above script is working when it is called inside cygwin console, but when I try to call it from a batch file I get "command not found" for date and tar command. I think that bash.exe does not have the PATH set up.

上面的脚本在 cygwin 控制台中被调用时正在工作,但是当我尝试从批处理文件中调用它时,我收到 date 和 tar 命令的“未找到命令”。我认为 bash.exe 没有设置 PATH。

I need to run that script from that batch file because I want to add the script to the task scheduler.

我需要从该批处理文件运行该脚本,因为我想将该脚本添加到任务调度程序。

采纳答案by matt b

Put your cygwin bin directory (likely C:\cygwin\bin) on your PATH environment variable.

将您的 cygwin bin 目录(可能C:\cygwin\bin)放在您的 PATH 环境变量中。

This would also give you the benefit of being able to use commands like tar, ls, rm, etc. from a regular console windows and not just a Cygwin console.

这也将使您能够从常规控制台窗口使用诸如、、 等命令tar,而不仅仅是 Cygwin 控制台。lsrm

回答by Michael Aaron Safyan

As has already been said, you need to add the Cygwin binaries to your path. To do so, right click on "My Computer", click "Properties", then "Advanced", then "Environment Variables".

如前所述,您需要将 Cygwin 二进制文件添加到您的路径中。为此,请右键单击“我的电脑”,单击“属性”,然后单击“高级”,然后单击“环境变量”。

Create a new environment variable with name "CYGWIN_HOME" and value "C:\cygwin" (or wherever you installed cygwin. The default location is "C:\cygwin\" so this should probably work for you).

创建一个名为“CYGWIN_HOME”且值为“C:\cygwin”的新环境变量(或您安装 cygwin 的任何位置。默认位置是“C:\cygwin\”,因此这可能对您有用)。

Then edit the environment variable named "PATH", and tack on the following to the end:

然后编辑名为“PATH”的环境变量,并将以下内容添加到最后:

;%CYGWIN_HOME%\bin;%CYGWIN_HOME%\sbin;%CYGWIN_HOME%\usr\bin;%CYGWIN_HOME%\usr\sbin;%CYGWIN_HOME%\usr\local\bin;%CYGWIN_HOME%\usr\local\sbin

;%CYGWIN_HOME%\bin;%CYGWIN_HOME%\sbin;%CYGWIN_HOME%\usr\bin;%CYGWIN_HOME%\usr\sbin;%CYGWIN_HOME%\usr\local\bin;%CYGWIN_HOME%\usr\local\sbin

Close your command prompt, then reopen it. The cygwin binaries should now be available. You can double-check this by typing "which bash". It should report the location of your bash executable.

关闭命令提示符,然后重新打开它。cygwin 二进制文件现在应该可用。您可以通过输入“which bash”来仔细检查。它应该报告您的 bash 可执行文件的位置。

回答by seth

FWIW, Cygwin has cron.

FWIW,Cygwin 有cron

Are you calling your script like this?

你是这样调用你的脚本吗?

bash --login -i ./myscript.sh

回答by Tim Henigan

If this script is invoked from a Windows command shell, the first line will result in an error since #!/bin/bashis not a recognized Windows command and #is not a valid comment delimiter in a batch file.

如果从 Windows 命令外壳调用此脚本,则第一行将导致错误,因为#!/bin/bash它不是可识别的 Windows 命令,#也不是批处理文件中的有效注释分隔符。

So, the bottom line is that this script runs as a regular batch file rather than from within Cygwin's bash. As noted by matt b, you likely do not have the Cygwin executable path in your PATH environment variable. Without this, the batch file cannot find the Cygwin utilities (tarand date).

因此,最重要的是该脚本作为常规批处理文件运行,而不是在 Cygwin 的 bash 中运行。正如 所指出的matt b,您的 PATH 环境变量中可能没有 Cygwin 可执行文件路径。如果没有这个,批处理文件将无法找到 Cygwin 实用程序(tardate)。

回答by Itaipu

I just had this problem.

我刚遇到这个问题。

Editing the environment variable works great. But if you are have no admin rights you can′t do that. In this case you can execute your commands by using the absolute path like:

编辑环境变量效果很好。但是如果你没有管理员权限,你就不能这样做。在这种情况下,您可以使用绝对路径来执行您的命令,例如:

/usr/bin/tar cvzf $filename
/usr/bin/cat $filename

If you do so your bash script works even if you call it from a batch file.

如果您这样做,即使您从批处理文件中调用它,您的 bash 脚本也能正常工作。