In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
1. Preface 1.1 Why do you learn shell programming
Shell script language is an important tool to realize Linux/UNIX system management and automatic operation and maintenance. The bottom layer of Linux/UNIX system and the core of basic application software mostly involve the content of Shell script. Every qualified Linux system administrator or operation and maintenance engineer needs to be proficient in writing Shell script language and be able to read the Shell script content that comes with the system and all kinds of software. Only in this way can we improve the work efficiency of operation and maintenance personnel, adapt to the increasingly complex working environment, and reduce unnecessary repetitive work, so as to lay a better foundation for personal workplace development.
1.2 basic knowledge needed to learn Shell programming well
Proficient in using vim editor, familiar with SSH terminal
Have a certain foundation of Linux commands, need to master at least 80 common Linux commands and be proficient in using it
To be proficient in Linux regular expressions and three Musketeers commands (grep,sed,awk)
1.3 how to learn Shel programming well
Learn the core of Shel programming well: practice more-- > think more-- > practice again-- > think again, and stick to this cycle!
Novice taboo: don't borrowlism, you can imitate it, but you have to chew it up and eat it, otherwise it will cause diarrhea.
Motto: if you think you will, you may not be right. What you think is right is not necessarily right.
Everyone should do it frequently and write the study notes and code by themselves. Cultivate your interest and sense of achievement through each small goal
2. Introduction to Shell script 2.1.What is Shell
Shell is a command interpreter, which is on the outermost layer of the operating system and is responsible for talking directly to the user.
Interpret the user's input to the operating system, process the output of various operating systems, and return the output screen to the user.
This kind of conversation can be:
1) Interactive mode: enter commands from the keyboard and get an immediate response from shell through the / bin/bash interpreter
2) non-interactive way: script
The following figure is the command interpreter shell.
Shell means shell in English, and the command interpreter Shell wraps the core of the system like a shell.
There are two ways for Shell to execute commands:
Built-in commands:
Such as cd,pwd,exit and echo commands mentioned above.
When the user logs in to the system, the shell and built-in commands are loaded into memory and run all the time
General commands: such as ls, program files on disk-> call-in-> execute commands
2.2 what is a Shell script
When a linux command or statement is not executed under the command line (strictly speaking, the command line is also shell)
Instead, when executed through a program file, the program is called a Shell script or Shell program
Users can type a series of commands and statement combinations into Shell scripts; these commands
Variables and process control statements are organically combined to form a powerful Shell script.
First of all, I will lead you to write a script to empty the / var/log/messages log.
We need to figure out a few questions first:
1) where is the log file?
/ var/log/messages
2) what command can be used to empty the file?
Redirect
3) write a simple shell script
#! / bin/bash
Cd / var/log/
> messages
4) how to execute the script?
# sh / server/scripts/log.sh
Have you considered:
Is there a script in a unified directory?
/ server/scripts directory
Permissions: which user to use to execute the file
Users need to be judged.
What to do and what to do when emptying the wrong files?
Error tip: do you know if there is any success?
Versatility of scripts
Summary:
Shell is the command interpreter. = > translator
The Shell script = = > command is placed in the script
3. Establishment and execution of Shell script 3.1. Establishment of Shell script
It is recommended to use the vim editor to edit the script. You can make individual names in advance.
# echo "alias vi=vim" > > the first line at the beginning of / etc/profile# source / etc/profile3.1.1 script
The first line of the canonical Shell script indicates which program (interpreter) executes the contents of the script
In linux bash programming, it is generally:
#! / bin/bash or #! / bin/sh
The opening "#!" Also known as magic numbers, when executing Shell scripts
The kernel will be based on "#!" To determine which program interprets the contents of the script
Note: this line must be on the first line at the top of each script, or comment the script line if it is not the first line
3.1.2 the difference between sh and bash # ll / bin/shlrwxrwxrwx. 1 root root 4 Dec 23 20:25 / bin/sh-> bash# sh is a soft link for bash, recommended for #! / bin/bash
You can take a look at the script that comes with the system.
Head-1 / etc/init.d/*
3.1.3 script comments
In the Shell script, the content followed by # represents a comment. The comment section will not be executed, only for people to see.
Comments can be on your own line, or you can follow the command and walk with the command. Get into the habit of writing notes to facilitate yourself and others
It is best not to use Chinese annotations, because garbled codes will occur in systems with different character sets.
3.2The execution of Shell script 3.2.1 several ways of Shell script execution
1) bash scripts-name or sh script-name (recommended)
This method is often used when the script itself does not have executable permissions.
2) path / script-name or. / scripts-name (full path or current path execution script)
This method first needs to give executable permission to the script file.
3) source scripts-name or. Scripts-name # Note "." A period with a space after it
Source or. While executing this script, you can load functions and variables from the script into the current shell
3.3 specifications and habits for Shell script development
1) specify the script interpreter at the beginning
2) add information such as version copyright at the beginning, and the ~ / .vimrc file can be automatically added.
3) the script should not be annotated in Chinese, but in English as much as possible
4) the script has a .sh extension
5) put it in a unified directory
6) excellent code writing habits
A, write the content in pairs at one time to prevent omission, such as [],', ", etc.
B, [] to have spaces at both ends, enter [] backspace first, enter 2 spaces, and then backspace
C, the process control statement is written at once, and then the content is added
If conditional then content fi
D, to make the code readable by indenting
E, the quotation marks in the script are all quotation marks in English, and other characters are also in English status.
Good habits can help us avoid a lot of unnecessary trouble and improve our work efficiency.
4. Shell environment variable 4.1 what is a variable
A variable is to replace more and more complex content with a fixed string (or a combination of characters, numbers, etc.).
This content may also contain variables and paths, strings and other content. Variables are defined as stored in memory
X=1y=24.2 variable type
Variables fall into two categories:
1) Environment variable (also known as global variable)
Can be used in creating their Shell and derived sub-shell
Environment variables can be divided into custom environment variables and bash built-in environment variables.
2) Local variable (ordinary variable):
Can only be used in the shell function or shell script that created them, remember the previous $user? What we create are generally ordinary variables.
4.2.1 Environment variabl
Environment variables are used to define the running environment of Shell to ensure the correct execution of Shell commands. Shell uses environment variables to determine login user name, command path, terminal type, login directory, etc. All environment variables are global variables and can be used in all child processes
Including editors, shell scripts and all kinds of applications. Except for crond scheduling tasks, you also need to redefine environment variables
Environment variables can be set on the command line, but their values are also lost when the user exits, so it is best to set them in the user's home directory.
Defined in the .bash _ profile file or in the global configuration / etc/bashrc,/etc/profile file or / etc/profile.d/ directory
Put environment variables in the profile file, and the values of these variables will be initialized each time the user logs in
Typically, all environment variables are uppercase. Environment variables should be exported with the export command before they are applied to the user process
For example: export chensiqi=1
Some environment variables, such as HOME,PATH,SHELL,UID,USER, have been / bin/login before the user logs in.
The program is set. Environment variables are usually defined and saved in the .bash _ profile or / etc/profile file in the user's home directory.
4.2.1 Local variables
Define local variables
Local variables are used in scripts for the user's current shell lifetime. For example, the local variable test takes a value of test18
This value is meaningful only in the user's current shell lifetime. If you start another process or exit in shell, the local variable test value will not be valid
Normal string variable definition
Variable name = value variable name = 'value' variable name = "value"
Requirements of variable name and variable content in shell
It is usually made up of letters, numbers, underscores and begins with a letter.
The contents of a variable can be printed in single or double quotes, or without quotation marks
Although variables can begin with an underscore, variables like this are special and are used by the system itself. We use it as little as possible.
Summary:
1) the symbols on both sides of the ls of CMD=ls are above the keyboard tab key, not single quotation marks
2) add $before the variable name to get the value of the variable, and use the echo or printf command to display the value of the variable
$An and $(A) are written differently, but the effect is the same. It is recommended to write it later.
3) ${WEEK} DAY if the variable and other characters form a new variable, you must add braces {} to the variable.
4) get into the habit of enclosing all string variables in double quotes to reduce weird errors encountered in programming. "$A" and "${A}"
4.3 Summary of the definition of variable name and variable content
Variable names can only consist of letters, numbers, underscores, and begin with a letter
The canonical definition of variable name writing: knowing the meaning by name.
A _ 1 / dev/null 2 > & 1
If [! $?-eq 0]; then
Echo "$user created faile!"
Echo "scripts begin to rollback!"
For i in ${User} {01.. 10}
Do
Userdel-r $I > / dev/null 2 > & 1
[$?-eq 0] | | exit 1
Done
Echo > $Path/user_passwd
Exit 1
Else
PassWD= `echo $RANDOM | md5sum | cut-C1-8`
[- d $Path] | | mkdir $Path
Echo $passWD | passwd-- stdin $user
Echo "$user:$passWD" > > $Path/user_passwd
Fi
Done
11.4.6 get the directory name under the current directory as a printout of the variable list # cat / servermax scriptsUniverse Test.shallowBash BinhPath = `pwd`echo $Pathfor filename in `ls`do [- d ${Path} / ${filename}] & & echo $filenamedone11.4.7 ninety-nine multiplication table # cat / servermax scriptsUniverse test.shedding cards spanwise Bin / bashfor
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.