LinuxShell-- understanding Shell Script

LinuxShell-- understanding Shell Script

Abstract: This paper introduces some basic knowledge of Shell scripts.

What is Shell Script

shell script using a shell program functions written, the procedure is to use a plain text file, and some shell syntax instructions written on the inside, then regular expression, and the Pipe command data stream redirection and other functions, to achieve the want processing purposes.

Like dos shell script .bat early years, the simplest function is to aggregate many instructions to write together, allowing users to easily perform multiple operations can be a command, shell script is provided arrays, loops, conditions and logical judgment and other important functions, allowing users to write programs directly to the shell.

basic structure

#!/bin/bash

The role of this sentence is marked with the following wrote the script I used is BASH grammar, writing is based on the BASH Shell scripts should begin with this.

The default in Linux Shell environment is BASH, so in this one without Linux which can also run. If the script is not in the default environment BASH environment to run, or not pure BASH scripting language, but other languages ​​embedded, then the script can not be properly performed. So Shell script must be "#! / Bin / bash" at the beginning.

Note

Shell script, in addition to "#! / Bin / bash" this line, the other lines as long as the "#" at the beginning is a comment.

The main program

Linux commands can be executed directly in the script.

Execute scripts

Given execute permission, run directly

This method is the most common Shell script method, but also the most straightforward. That is, after giving execute permission, run directly. Of course, you can use absolute paths running, you can also use relative paths.

If the PATH shell.sh placed in the specified directory, so that you can be like the Linux system commands, do not specify the path directly run.

Given execute permissions:

1 [root@localhost sh]# chmod 755 shell.sh

Use absolute paths of execution:

1 [root@localhost sh]# /root/sh/shell.sh

Using a relative path to execute:

1 [root@localhost sh]# ./shell.sh

Run the script by calling bash command

Meaning of this method is the direct use bash or sh to explain the contents of the script, so the script can be run properly. Using this method to run the script, do not even have a script file "execute" permission, as long as a "read" the right to run.

Because the / bin / sh is actually the / bin / bash of a link, use the  SH shell. SH  is to tell the system you want directly to bash the functionality to perform shell.sh.

Performed using the bash command:

1 [root@localhost sh]# bash shell.sh
2 [root@localhost sh]# sh shell.sh

Performed using the source command

source command or "." command can be read into the script and execute the script, that is loaded and executed in the current environment Shell commands and statements related to a script file, instead of generating a sub Shell to execute commands in the file.

This command is mainly used to make the environment variables are re-configured profiles forced to take effect.

Performed using the source command:

1 [root@localhost sh]# . shell.sh
2 [root@localhost sh]# source shell.sh

Guess you like

Origin www.cnblogs.com/shamao/p/11209452.html