bash 如何从终端运行 .sh 或 .bat 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17015449/
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
How do I run .sh or .bat files from Terminal?
提问by almel
I have a pretty basic problem here, that has happened so haphazardly to me that up until now, I've just ignored it. I downloaded tomcat web server and "Murach's Java Servlets and JSP" book is telling me to navigate to the tomcat/bin directory and start the server my typing in Terminal
我在这里有一个非常基本的问题,它发生在我身上是如此偶然,直到现在,我才忽略它。我下载了 tomcat Web 服务器,“Murach 的 Java Servlets 和 JSP”一书告诉我导航到 tomcat/bin 目录并启动我在终端中输入的服务器
$ startup
$启动
However, I get the error
但是,我收到错误
-bash: startup: command not found
The relevant files in this directory are startup.sh and startup.bat. Typing both of these returns the same error message
该目录下的相关文件是startup.sh和startup.bat。键入这两个返回相同的错误消息
So my questions are, what are .bat and sh files, and how do I run these files? I've read several tutorials for different languages and software programs, and some times when the tutorial says execute a bunch of files in the command line, I get a "command not found" error. Sometimes it works, sometimes it doesn't. This is perplexing to me, so what are some common solutions to solving these sort of "command not found" Terminal problems?
所以我的问题是,什么是 .bat 和 sh 文件,我该如何运行这些文件?我已经阅读了几个针对不同语言和软件程序的教程,有时当教程说在命令行中执行一堆文件时,我会收到“找不到命令”的错误消息。有时它有效,有时它不起作用。这让我很困惑,那么解决这些“找不到命令”终端问题的一些常见解决方案是什么?
回答by pilsetnieks
The .sh
is for *nix systems and .bat
should be for Windows. Since your example shows a bash error and you mention Terminal, I'm assuming it's OS X you're using.
的.sh
是* nix系统,并.bat
应适用于Windows。由于您的示例显示了 bash 错误并且您提到了终端,因此我假设您使用的是 OS X。
In this case you should go to the folder and type:
在这种情况下,您应该转到该文件夹并键入:
./startup.sh
./
just means that you should call the script located in the current directory. (Alternatively, just type the full path of the startup.sh
). If it doesn't work then, check if startup.sh
has execute permissions.
./
只是意味着您应该调用位于当前目录中的脚本。(或者,只需键入 的完整路径startup.sh
)。如果它不起作用,请检查是否startup.sh
具有执行权限。
回答by KamikazeCZ
This is because the script is not in your $PATH. Use
这是因为脚本不在您的 $PATH 中。用
./scriptname
You can also copy this to one of the folders in your $PATH or alter the $PATH variable so you can always use just the script name. Take care, however, there is a reason why your current folder is not in $PATH. It might be a security risk.
您还可以将其复制到 $PATH 中的文件夹之一或更改 $PATH 变量,以便您始终可以只使用脚本名称。但是,请注意,当前文件夹不在 $PATH 中是有原因的。这可能是一个安全风险。
If you still have problems executing the script, you might want to check its permissions - you must have execute permissions to execute it, obviously. Use
如果您在执行脚本时仍然遇到问题,您可能需要检查其权限 - 显然,您必须具有执行权限才能执行它。用
chmod u+x scriptname
A .sh
file is a Unix shell script. A .bat
file is a Windows batch file.
一个.sh
文件是一个Unix shell脚本。一个.bat
文件是Windows批处理文件。
回答by user3752086
Type bash script_name.sh
or ./script_name
in linux terminal. Before using ./script_name
make you script executeable by sudo chmod 700 script_name
and type script_name.bat
in windows.
键入bash script_name.sh
或./script_name
在Linux终端。在使用之前,./script_name
让您的脚本可由Windows执行sudo chmod 700 script_name
并键入script_name.bat
。
回答by Basil Bourque
Drag-And-Drop
拖放
Easiest way for a lazy Mac user like me: Drag-and-drop the startup.sh
file from the Finder to the Terminal window and press Return.
对于像我这样的懒惰 Mac 用户来说,最简单的方法是:将startup.sh
文件从 Finder拖放到终端窗口,然后按 Return。
To shutdown Tomcat, do the same with shutdown.sh
.
要关闭 Tomcat,请使用shutdown.sh
.
You can delete all the .bat
files as they are only for a Windows PC, of no use on a Mac to other Unix computer. I delete them as it makes it easier to read that folder's listing.
您可以删除所有.bat
文件,因为它们仅适用于 Windows PC,在 Mac 和其他 Unix 计算机上没有用。我删除了它们,因为这样可以更轻松地阅读该文件夹的列表。
File Permissions
文件权限
I find that a fresh Tomcat download will not run on my Mac because of file permission restrictions throwing errors during startup. I use the BatChmod
app which wraps a GUI around the equivelant Unix commands to reset file permissions.
我发现新的 Tomcat 下载不会在我的 Mac 上运行,因为文件权限限制在启动期间抛出错误。我使用BatChmod
将 GUI 包裹在等效 Unix 命令周围的应用程序来重置文件权限。
Port-Forwarding
转发端口
Unix systems protect access to ports numbered under 1024. So if you want to use port 80 with Tomcat you will need to learn how to do "port-forwarding" to forward incoming requests to port 8080 where Tomcat listens by default. To do port-forwarding, you issue commands to the packet-filtering (firewall) app built into Mac OS X (and BSD). In the old days we used ipfw
. In Mac OS X 10.7 (Lion) and later Apple is moving to a newer tool, pf
.
Unix 系统保护对编号低于 1024 的端口的访问。因此,如果您想在 Tomcat 中使用端口 80,您将需要学习如何进行“端口转发”以将传入请求转发到 Tomcat 默认侦听的端口 8080。要进行端口转发,您可以向 Mac OS X(和BSD)中内置的数据包过滤(防火墙)应用程序发出命令。在过去,我们使用ipfw
. 在 Mac OS X 10.7 (Lion) 和更高版本中,Apple 正在转向更新的工具pf
.
回答by Majid Laissi
On windows type either startup
or startup.bat
在 Windows 上键入startup
或startup.bat
On unix type ./startup.sh
在 unix 类型上 ./startup.sh
(assuming you are located in tomcat/bin directory)
(假设您位于 tomcat/bin 目录中)
回答by Frozztie
Type in
输入
chmod 755 scriptname.sh
In other words, give yourself permission to run the file. I'm guessing you only have r/w permission on it.
换句话说,给自己运行文件的权限。我猜你只有 r/w 权限。
回答by gazdagergo
Based on IsmailS' commentthe command which worked for me on OSX was:
根据IsmailS 的评论,在 OSX 上对我有用的命令是:
sudo sh ./startup.sh
回答by Devyn Collier Johnson
Batch files can be run on Linux. This article explains how (http://www.linux.org/threads/running-windows-batch-files-on-linux.7610/).
批处理文件可以在 Linux 上运行。本文解释了如何(http://www.linux.org/threads/running-windows-batch-files-on-linux.7610/)。
回答by Steve
I had this problem for *.sh files in Yosemite and couldn't figure out what the correct path is for a folder on my Desktop...after some gnashing of teeth, dragged the file itself into the Terminal window; hey presto!!
我在 Yosemite 中遇到了 *.sh 文件的这个问题,无法弄清楚桌面上文件夹的正确路径是什么......咬牙切齿后,将文件本身拖到终端窗口中;嘿快点!!
回答by T BC
My suggestion does not come from Terminal; however, this is a much easier way.
我的建议不是来自终端;然而,这是一种简单得多的方法。
For .bat files, you can run them through Wine. Use this video to help you install it: https://www.youtube.com/watch?v=DkS8i_blVCA. This video will explain how to install, setup and use Wine. It is as simple as opening the .bat file in Wine itself, and it will run just as it would on Windows.
对于 .bat 文件,您可以通过 Wine 运行它们。使用此视频来帮助您安装它:https: //www.youtube.com/watch?v=DkS8i_blVCA。该视频将解释如何安装、设置和使用 Wine。它就像在 Wine 本身中打开 .bat 文件一样简单,它会像在 Windows 上一样运行。
Through this, you can also run .exe files, as well .sh files.
通过这种方式,您还可以运行 .exe 文件以及 .sh 文件。
This is much simpler than trying to work out all kinds of terminal code.
这比试图计算出各种终端代码要简单得多。