shell脚本
In the command line, a shell script is an executable file that contains a set of instructions that the shell will execute. Its main purpose is to reduce a set of instructions (or commands) in just one file. Also it can handle some logic because it’s a programming language.
在命令行中,shell脚本是一个可执行文件,其中包含该Shell将执行的一组指令。 它的主要目的是减少一个文件中的一组指令(或命令)。 由于它是一种编程语言,因此它还可以处理一些逻辑。
如何建立 (How to create it)
Create the file:
创建文件:
$ touch myscript.sh
Add a shebang at the start of the file. The Shebang line is responsible for letting the command interpreter know which interpreter the shell script will be run with:
在文件的开头添加一个Shebang 。 Shebang行负责让命令解释器知道将运行shell脚本的解释器:
$ echo "#!/bin/bash" > myscript.sh
# or
$ your-desired-editor myscript.sh
# write at the first line #!/bin/bash
Add some commands:
添加一些命令:
$ echo "echo Hello World!" >> myscript.sh
Give the file execution mode:
给出文件执行方式:
$ chmod +x myscript.sh
Execute it!
执行它!
$ ./myscript.sh
Hello World!
More info about shell-scripting can be found here
有关shell脚本的更多信息,请参见此处。
有关Shell脚本和Linux的更多信息: (More info on shell scripting and Linux:)
A beginner's guide to surviving in the Linux shell
在Linux Shell中生存的初学者指南
Basic Linux commands to know
基本的Linux命令要知道
Shell scripting tricks
Shell脚本技巧
翻译自: https://www.freecodecamp.org/news/shell-scripting/
shell脚本