How to write a simple script in Linux Shell

In the Linux terminal and a basic understanding of its mix of Shell (the default is bash ) at the base, we can write a shell script program in the terminal using vi / vim editor

Shell command interpreter interpreted both as a tool, but also a scripting language, under the command of a variety of shell and some specific grammatical structure can be composed of a Shell script, shell script is a plain text file, but in no action is required prior to execution. We can let us write some scripts easier to manipulate files, as well as do some shell testing.

# Learn Benpian knowledge required to understand the use of simple vi, chmod, and vi editor

1: Open a terminal, enter the directory to save the program, command line vi test.sh (suffix Shell scripts for .sh)

2: In the Shell program, the characters after the # as a comment, so we need to write some comments in the program is necessary, it does allow you to program up a notch, and pay attention to is the need in the first line of the program Affirming #! / Bin / bash (csh, zsh ..), use #! We are used to indicate the program is executed with bash (where used)

3: Here wrote a simple script as follows:

Part of the program

4: After writing can be run directly, / represents the current directory, enter ./test.sh execution, you will find no authority (permission denied), then we grant a right to own, you can run, but since this is a script. plain text files, prior to performing relative to the execution of other programs less compile this step, it will seem a little slow.

Shell script of a small success.

If you want to further enhance the content of the script, a lot we can learn some command of Shell, Shell language and some grammatical style of the program uses only a few simple commands.

Guess you like

Origin www.linuxidc.com/Linux/2019-10/161164.htm