In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
Shell programming specification 1. Preface
this article will explain the programming specifications and variables of shell and verify them with examples, which is suitable for beginners to learn the basics of shell. In the process of learning, we need to do it ourselves, do it several times, understand its meaning while doing it, and then learn to sum up ourselves and gradually improve our ability. Believe in yourself, you can!
First of all, let's introduce the simple explanation of strong language and weak language type (it doesn't matter if we don't understand it at present):
Strong language types-- need to declare variable types-- weak language types-- No need to declare variable types HTML markup language tags
Shell script programming is a typical weak language type, in which variables are directly defined without declaration, and shell language does not have object-oriented thinking. Just need to save the commands to be executed in order to a text file, give the file executable permission, you can run, when slowly in-depth study of shell programming, can be combined with a variety of Shell control statements to complete more complex operations, combined with shell script programming, a large number of operations such as various services can be directly executed through a script to improve work efficiency.
Of course, if you want to learn shell well, you must be familiar with shell commands, especially some commonly used commands and some important commands such as grep, egrep, sed, awk and so on.
II. The concept of shell 2.1.What is shell?
shell-- in computer science, Shell is commonly known as shell (used to distinguish it from kernel), which refers to software (command parser) that "provides the user with an interface". It is similar to command.com under DOS and later cmd.exe. It receives user commands and then invokes the appropriate application.
2.2shell Application scenario repetitive Operation batch transaction processing Automation Operation and maintenance Service running status Monitoring the role of scheduled Task execution 2.3shell-- Command interpreter
Between the system kernel and the user, responsible for interpreting the command line, one line executes multiple unrelated commands using ";" to separate execution
III. Shell programming specification
The shell script in the Linux system is a special application that follows a standard script structure and can output friendly prompts that are easier to read. Corresponding to the script with more code and complex structure, you should add the necessary comment text. Examples are as follows:
[root@localhost shell] # cat demo.sh #! / bin/bash / / A special script declaration indicating that the statements after this line are interpreted by the / bin/bash program # this is a demo shell script / / except for the # key above, all the # keys indicate that the line change is a comment message cd / bootecho "currently located at:" pwdecho "where files starting with vml include:" ls-lh vml* "
, so how does the program execute? First of all, we can directly use the source or sh command to execute directly (note the difference! Otherwise, you need to add its execution permission, and then execute it
[root@localhost shell] # ls-l total dosage 16 RW / r / r 1 root root 134Nov 25 19:59 demo.sh / / No executable permissions-rwxr-xr-x. 1 root root 274 November 25 18:58 state.sh-rwxr-xr-x. 1 root root 208 November 25 19:17 sujiaju.sh-rwxr-xr-x. 1 root root 345 November 25 19:11 welcome.sh [root@localhost shell] # source demo.sh is currently located at: / boot where files that begin with vml include:-rwxr-xr-x. 1 root root 5.7m November 14 11:41 vmlinuz-0-rescue-a33ccb7775134b0e83e04555d56fedb7-rwxr-xr-x. 1 root root 5.7m August 23 2017 vmlinuz-3.10.0-693.el7.x86_64 [root@localhost boot] # cd-/ root/shell [root@localhost shell] # sh demo.sh is currently located at: / boot where files starting with vml include:-rwxr-xr-x. 1 root root 5.7m November 14 11:41 vmlinuz-0-rescue-a33ccb7775134b0e83e04555d56fedb7-rwxr-xr-x. 1 root root 5.7m August 23 2017 vmlinuz-3.10.0-693.el7.x86_64 [root@localhost shell] #
after practical operation, you can make the following summary: there is no permission to execute the shell script file, can be executed through the source or sh command, but the source command after the implementation of the path switch operation will be switched to the path, and the sh command will not, and the direct implementation of the file is not sufficient authority, the need to change permissions to execute.
IV. Piping and redirection
Most of the operations of shell are in the background and do not require user intervention, so it is very important to learn to extract and filter execution information. Here are two shell O operations: piping and redirection.
4.1 Pipeline operation
The results before are used as inputs to later commands or as processing objects, and multiple pipe symbols can be used in the same line of commands.
take a look at the following example:
[root@localhost ~] # grep-v "/ sbin/nologin$" / etc/passwd / / retrieve root:x:0:0:root:/root:/bin/bashsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltlokott:x:1000:1000:lokott:/home/lokott:/bin from users allowed to log in in the system / bash [root@localhost ~] # tail / etc/passwd | grep "/ sbin/nologin$" | the total number of users who are not allowed to log on on the last 10 lines of the statistics file passwd 4.2 redirect
When users process information through the operating system, there are the following types of interactive device files
Standard input: keyboard, file number 0 standard output: monitor, file number 1 standard error: monitor, file number 2
In actual Linux system maintenance, the direction of input and output can be changed instead of using the default standard input and output devices (keyboard and monitor). This operation is called "redirection".
1) redirect input
Redirecting input refers to changing the way to receive input in a command from the default keyboard to a specified file, rather than waiting for keyboard input. Redirect input uses "-" to indicate overwrite
"> >"-- means append
Example:
[root@localhost shell] # uname-p > kernel.txt [root@localhost shell] # cat kernel.txt x86 / 64 [root@localhost shell] # uname-p > > kernel.txt [root@localhost shell] # cat kernel.txt x86_64x86_64 [root@localhost shell] # uname-p > kernel.txt [root@localhost shell] # cat kernel.txt x86 / 64
(3) error redirection
Stores the error information to the specified file instead of displaying it directly on the screen. Error redirection uses the "2 >" operator.
Example:
[root@localhost shell] # tar jcf / nonedir/ex.bz2 / etc/ 2 > error.log [root@localhost shell] # cat error.log tar: delete the beginning "/" tar (child): / nonedir/ex.bz2: cannot open: there is no file or directory tar (child): Error is not recoverable: exiting now
Redirection and pipeline operation are very common functions in shell environment and need to be mastered.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.