如何在终端中执行 bash 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2177932/
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 execute a bash script in Terminal?
提问by Ballon
I have a bash script like:
我有一个 bash 脚本,如:
#!/bin/bash
echo Hello world!
How do I execute this in Terminal?
如何在终端中执行此操作?
采纳答案by prodigitalson
$prompt: /path/to/script
and hit enter. Note you need to make sure the script has execute permissions.
$prompt: /path/to/script
并按回车键。请注意,您需要确保脚本具有执行权限。
回答by Paused until further notice.
Yet another way to execute it (this time without setting execute permissions):
另一种执行它的方法(这次没有设置执行权限):
bash /path/to/scriptname
回答by John Boker
cd to the directory that contains the script, or put it in a bin folder that is in your $PATH
cd 到包含脚本的目录,或将其放在 $PATH 中的 bin 文件夹中
then type
然后输入
./scriptname.sh
if in the same directory or
如果在同一目录中或
scriptname.sh
if it's in the bin folder.
如果它在 bin 文件夹中。
回答by xCra2yx
You could do:sh scriptname.sh
你可以这样做:sh scriptname.sh
回答by Donald Shahini
Firstly you have to make it executable using: chmod +x name_of_your_file_script
.
首先,您必须使用以下命令使其可执行:chmod +x name_of_your_file_script
.
After you made it executable, you can run it using ./same_name_of_your_file_script
使其可执行后,您可以使用 ./same_name_of_your_file_script
回答by gaurav07
Change your directory to where script is locatedby using cdcommand
使用cd命令将目录更改为脚本所在的位置
Then type
然后输入
bash program-name.sh
回答by Michael Treanor
This is an old thread, but I happened across it and I'm surprised nobody has put up a complete answer yet. So here goes...
这是一个旧线程,但我偶然发现了它,我很惊讶还没有人给出完整的答案。所以这里...
The Executing a Command Line Script Tutorial!
执行命令行脚本教程!
Q:How do I execute this in Terminal?
问:如何在终端中执行此操作?
Confusions and Conflicts:
困惑和冲突:
- You do notneed an 'extension' (like .sh or .py or anything else), but it helps to keep track of things. It won't hurt. If the script name contains an extension, however, you must use it.
- You do notneed to be in any certain directory at all for any reason.
- You do notneed to type out the name of the program that runs the file (BASH or Python or whatever) unless you want to. It won't hurt.
- You do notneed
sudo
to do any of this. This command is reserved for running commands as another user or a 'root' (administrator) user. Great post here.
- 你不会需要一个“扩展”(如.SH或的.py或其他任何东西),但它可以帮助跟踪的东西。不会痛的。但是,如果脚本名称包含扩展名,则必须使用它。
- 你不是需要在任何一个目录都以任何理由。
- 你不是需要键入的是运行,除非你想要的文件(BASH或Python或其他)的程序的名称。不会痛的。
- 你不会需要
sudo
做任何这一点。此命令保留用于以另一个用户或“root”(管理员)用户身份运行命令。很棒的帖子在这里。
(A person who is just learning how to execute scripts should not be using this command unless there is a real need, like installing a new program. A good place to put your scripts is in your ~/bin folder. You can get there by typing cd ~/bin
or cd $HOME/bin
from the terminal prompt. You will have full permissions in that folder.)
(一个刚刚学习如何执行脚本的人不应该使用这个命令,除非有真正的需要,比如安装一个新程序。放置脚本的好地方是你的 ~/bin 文件夹。你可以通过在终端提示符下键入cd ~/bin
或cd $HOME/bin
。您将在该文件夹中拥有完全权限。)
To "execute this script" from the terminal on a Unix/Linux type system, you have to do three things:
要从 Unix/Linux 类型系统的终端“执行此脚本”,您必须做三件事:
Tell the system the location of the script. (pick one)
- Type the full path with the script name (e.g.
/path/to/script.sh
). You can verify the full path by typingpwd
orecho $PWD
in the terminal. - Execute from the same directory and use
./
for the path (e.g../script.sh
). Easy. - Place the script in a directory that is on the system
PATH
and just type the name (e.g.script.sh
). You can verify the systemPATH
by typingecho $PATH
orecho -e ${PATH//:/\\n}
if you want a neater list.
- Type the full path with the script name (e.g.
Tell the system that the script has permission to execute. (pick one)
- Set the "execute bit" by typing
chmod +x /path/to/script.sh
in the terminal. - You can also use
chmod 755 /path/to/script.sh
if you prefer numbers. There is a great discussion with a cool chart here.
- Set the "execute bit" by typing
Tell the system the type of script. (pick one)
- Type the name of the program before the script. (e.g.
BASH /path/to/script.sh
orPHP /path/to/script.php
) If the script has an extension, such as .php or .py, it is part of the script name and you must include it. - Use a shebang, which I see you have (
#!/bin/bash
) in your example. If you have that as the first line of your script, the system will use that program to execute the script. No need for typing programs or using extensions. - Use a "portable" shebang. You can also have the system choose the version of the program that is first in the
PATH
by using#!/usr/bin/env
followed by the program name (e.g.#!/usr/bin/env bash
or#!/usr/bin/env python3
). There are pros and cons as thoroughly discussed here.
- Type the name of the program before the script. (e.g.
告诉系统脚本的位置。(选一个)
- 键入带有脚本名称的完整路径(例如
/path/to/script.sh
)。您可以通过键入pwd
或echo $PWD
在终端中验证完整路径。 - 从同一目录执行并
./
用于路径(例如./script.sh
)。简单。 - 将脚本放在系统上的目录中,
PATH
然后键入名称(例如script.sh
)。您可以PATH
通过键入echo $PATH
或echo -e ${PATH//:/\\n}
想要更简洁的列表来验证系统。
- 键入带有脚本名称的完整路径(例如
告诉系统脚本有执行权限。(选一个)
- 通过
chmod +x /path/to/script.sh
在终端中键入来设置“执行位” 。 chmod 755 /path/to/script.sh
如果您更喜欢数字,也可以使用。有一个很棒的讨论,这里有一个很酷的图表。
- 通过
告诉系统脚本的类型。(选一个)
- 在脚本之前键入程序的名称。(例如
BASH /path/to/script.sh
或PHP /path/to/script.php
)如果脚本具有扩展名,例如 .php 或 .py,则它是脚本名称的一部分,您必须包含它。 - 使用shebang,我看到你
#!/bin/bash
在你的例子中有 ( ) 。如果您将其作为脚本的第一行,系统将使用该程序来执行脚本。无需键入程序或使用扩展。 - 使用“便携式” shebang。您还可以让系统
PATH
通过 using#!/usr/bin/env
后跟程序名称(例如#!/usr/bin/env bash
或#!/usr/bin/env python3
)来选择程序的第一个版本。此处详细讨论了利弊。
- 在脚本之前键入程序的名称。(例如
回答by Eat at Joes
回答by Hassan Shamshir
If you are in a directory or folder where the script file is available then simply change the file permission in executable mode by doing
如果您位于脚本文件可用的目录或文件夹中,则只需通过执行以下操作更改可执行模式下的文件权限
chmod +x your_filename.sh
chmod +x your_filename.sh
After that you will run the script by using the following command.
之后,您将使用以下命令运行脚本。
$ sudo ./your_filename.sh
$ sudo ./your_filename.sh
Above the "." represent the current directory. Note! If you are not in the directory where the bash script file is present then you change the directory where the file is located by using
在“.”之上 代表当前目录。笔记!如果您不在 bash 脚本文件所在的目录中,则可以使用以下命令更改文件所在的目录
cd Directory_name/write the complete path
cd Directory_name/write the complete path
command. Otherwise your script can not run.
命令。否则你的脚本无法运行。