The shell of linux study notes (1)

1. What is a shell?

From the programmer's point of view, the Shell itself is a program written in C language, and from the user's point of view, the Shell is the bridge between the user and the Linux operating system. Users can either enter commands to execute, or use Shell script programming to complete more complex operations. In today's increasingly perfect Linux GUI, Shell programming still plays an important role in system management and other fields. In-depth understanding and proficient mastering of Shell programming is one of the compulsory homework for every Linux user.

 Second, the shell operating environment and writing tools

 Shell programming is the same as java and php programming, as long as there is a text editor that can write code and a script interpreter that can interpret and execute
There are many types of Linux shells, the common ones are: Bourne Shell (/usr/bin/sh or /bin/sh), Bourne Again Shell (/bin/bash), C Shell (/usr/bin/csh), K Shell ( /usr/bin/ksh), Shell for Root (/sbin/sh), etc. Different shell languages ​​have different syntax, so they cannot be used interchangeably. Each shell has its own characteristics, and basically, it is enough to master any of them. In this article, we focus on Bash, also known as Bourne Again Shell. Bash is widely used in daily work due to its ease of use and free of charge; at the same time, Bash is also the default shell of most Linux systems. In general, people do not distinguish between Bourne Shell and Bourne Again Shell, so, in the text below, we can see #!/bin/sh, which can also be changed to #!/bin/bash.

 3. Basic syntax of shell script

wrote
The format of using a text editor such as vi to write a shell script is fixed, as follows:
#!/bin/sh
#comments
Your commands go here
The symbol #! in the first line tells the system that the program specified by the following path is to interpret the script file shell program. If the first line does not have this sentence, an error will occur when executing the script file. The subsequent part is the main program. Shell scripts, like high-level languages, also have variable assignments and control statements. Except for the first line, lines beginning with # are comment lines until the end of the line. If a line is not completed, you can add "" at the end of the line, this symbol indicates that the next line and this line will be merged into the same line.
After editing, save the script as filename.sh, the file name suffix sh indicates that this is a Bash script file.
To execute the script, first change the attribute of the script file to executable:
chmod +x filename.sh
The common method of executing the script is:
1) As an executable program
./filename.sh
Note, it must be written as ./filename.sh , Instead of filename.sh, the same is true for running other binary programs. Write filename.sh directly. The Linux system will go to the PATH to find out if there is a filename.sh, but only /bin, /sbin, /usr/bin, /usr /sbin is in the PATH, your current directory is usually not in the PATH, so if you write filename.sh, you will not be able to find the command. Use ./filename.sh to tell the system to find it in the current directory.
2) As interpreter parameter
/bin/sh filename.sh

 Fourth, the first helloworld.sh

#!/bin/bash
echo 'hello world'
[imix@localhost shell]$ chmod +x helloworld.sh
[imix@localhost shell]$ ./helloworld.sh
hello world
[imix@localhost shell]$ /bin/sh helloworld.sh
hello world

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326863557&siteId=291194637