Today, programmers who don't have Linux do not mean that they are programmers, and not shell programming can say that they are Linux. It sounds like shell programming is a dick, but don't worry, shell programming is really simple.
Background
- What is Shell programming
The explanation on the tall, often let a person can't touch the mind. In a nutshell, Shell programming is the logical processing of a bunch of Linux commands.
- Why would shell programming
As a simple example, we did the Javaweb development, and in the past, if we were to package the program locally and then deploy it to a remote server (leaving the current CI, the original method), our previous practice would typically go through several steps:
Fetch the latest code (GIT pull)
Compile Package
Uploading and deploying to a remote server
Every time you pack, you have to go through this phase, which is inefficient and irritable. At this point, we can write a shell script, and then each time only need to run the shell script, you can implement the package deployment of the series of actions, completely free hands, how good
Entry
#!/bin/bash# First shell applet echo Helloworld!
Above, our first shell applet was completed, and the result is of course the output of our familiar Hello world.
The first line indicates that we chose to use the bash shell.
The # symbol in the shell indicates a comment. The first line of the shell is special, and typically starts with #! to specify the shell type to be used. In Linux, in addition to the bash Shell , there are many versions of the shell, such as zsh, Dash , etc... But the bash shell is still the most we use.
Specific can be consulted: http://www.cnblogs.com/qlqwjy/p/7575609.html
The second line starts with the # sign, indicating that the bank is a comment and will not run when it runs.
The echo in the third row is the output command in Linux, and the line is obviously the output Hello world!
- Run the first shell program
The first mode of operation:
Create a new file (hello_world.sh), and then copy the above code to this file, and then you need to assign the executable permission to the file.
VI helloword.sh ... Copy code ... : Wq Save Exit
to give a shell file permission to run:
chmod +x helloworld.sh
Final execution:
./helloworld.sh
Attention:
In Linux, the suffix is almost arbitrary or has no suffix, and the shell is generally saved as Xxx.shell to make it look more intuitive.
If the hello_world.sh is executed directly, it will be looked up by default from the $PATH environment variable, which is not found because we are configuring this file in an environment variable. So, we used the "." This symbol indicates a search from the current directory.
The second mode of operation:
In addition to the above execution methods, we can also specify the shell directly to run:
/bin/sh helloworld.sh
Here we specify/bin/sh to execute, when the/bin/bash specified in helloworld.sh will not take effect.
Does programming have no variables? Right?
Shell programming is divided into two variables, the first is our own defined variables (custom variables), the second is the Linux defined environment variables (environment variables, such as: $PATH, $HOME, etc..., such variables we can use directly).
#!/bin/bash# Use environment variable echo $PATH # custom variable Hellohello="helloWorld"Echo $hello
The above demonstrates the use of custom variables and system environment variables, as simple as using the $ symbol plus the variable name. Remember: define the variable without the $ sign, and use the variable to add $ to the line .
In line 5th, we use double quotes when customizing variables, and in shell programming, if a variable has spaces or quotes, it must be quoted, otherwise it can be omitted .
It is also important to note that when defining variables, the"=" must not have spaces around .
Assigning the results of a Linux command execution to a variable
#!/bin/Bashpath=$ (pwd) files= ' ls-al ' Echo Current path: $pathecho files: $files
The above 2 and 3rd lines demonstrate two ways to save the results of a Linux command execution to a variable.
The 2nd line assigns the PWD execution result (currently located in the directory) to the PATH variable.
Line 3rd ls-al Command Execution results (list all files and folders in the current directory) assign to variables
Note: The third line of the symbol is not a single quotation mark, the "~" button on the keyboard
Well, here we have a preliminary understanding of shell programming, as well as the use of variables. In this, it seems that shell programming is not very simple. Yes, it's really just one thing.
Summarize:
Create a sh file vi hello.sh
Complete with VIM Editor: Wq Save exit
To give operational permissions chmod +x hello.sh
Run the sh file./hello.sh ( must Add./)
Shell Programming (i)