In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you what the Shell script is, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Preface
Shell Script,Shell script is similar to batch processing under Windows and Dos, that is, a program file that is put into a file in advance with all kinds of commands to facilitate one-time execution, mainly for administrators to set up or manage. But it is more powerful than batch processing under Windows and more efficient than programs edited with other programming programs, after all, it uses commands under Linux/Unix. Shell script is a program written using the function of shell, this program is to use plain text files, write some shell syntax and instructions in it, and then use normal representation, pipeline commands and data stream redirection and other functions to achieve the desired processing purpose.
To put it more clearly, shell script is like .bat in the early dos era, the simplest function is to assemble many instructions together, so that users can easily execute multiple commands in one operation, while shell script provides important functions such as arrays, loops, conditions and logic judgment, so that users can write programs directly in shell without having to use syntax written by traditional programs such as the C programming language.
Conceptual distinction
What's the difference between Shell and Shell scripts? To be exact, Shell is a command-line interpreter, and its function is to follow a certain syntax to interpret the input commands and pass them to the system. It provides users with an interface system-level program that sends requests to Linux to run programs. Users can use Shell to start, suspend, stop or even write some programs. Shell itself is a program written in C language, and it is a bridge for users to use Linux. Shell is both a command language and a programming language (what you call shell scripts). As a command language, it interactively interprets and executes commands entered by users; as a programming language, it defines a variety of variables and parameters, and provides many control structures that are unique in high-level languages, including loops and branches. Although it is not part of the Linux system kernel, it invokes most of the functions of the system kernel to execute programs, create documents, and coordinate the running of each program in parallel.
1. The basic syntax of Shell scripts
1. Write a simple shell script
[root@xj ~] # vim example.sh
#! / bin/bash
# This is to show waht an example looks like.
Echo "our first example"
Echo "we are currently in the following directroy"
Pwd
Echo "This directory contains the following files"
Ls
[root@xj ~] # chmod + x example.sh / / give executable permissions to the script
[root@xj] #. / example.sh / / execute and run
2. Basic steps for creating and executing shell scripts
II. Variables of Shell script
1. What is a variable
Variables come from mathematics and can store calculation results or represent abstract concepts of values in computer language. Variables can be accessed through variable names. In imperative languages, variables are usually variable; but in pure functional languages (such as Haskell)
Variables can be immutable. In some languages, variables may be explicitly defined as abstractions that represent mutable states and have storage space (as in Java and Visual Basic), but others may use other concepts (such as C objects) to refer to this abstraction without strictly defining the exact extension of "variables."
2. Shell variable
Variables in Shell are a way to pass data. A variable is a symbolic name used to represent each value
There are two types of variables in Shell: temporary and permanent.
Extend:
Custom environment variabl
[root@xj ~] # mv example01.sh / etc/sysconfig/
[root@xj ~] # export PATH=/etc/sysconfig:$PATH
[root@xj ~] # echo $PATH
/ etc/sysconfig:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
Note: this method will become invalid once the terminal is closed. If you want the modification to take effect permanently, you can modify the .bashrc file in the user directory and add export PATH=/etc/sysconfig:$PATH to the last line of this file to save and exit.
[root@localhost ~] # source .bashrc
If you do not report an error after executing .bashrc, you will be successful!
User-defined variable: begins with a letter or underscore, consists of letters, numbers, or underscores, is case-sensitive and has different meanings of uppercase and lowercase letters. There is no limit to the length of the variable name.
When using the value of a variable, you should precede the variable name with a "$" symbol.
For example:
[root@xj ~] # 123=abc
Bash: 123=abc: command not found...
Variable assignment: variable = value, assignment number "=" without spaces on both sides, otherwise an error will be prompted.
[root@xj ~] # a = 1
Bash: a: command not found...
The correct way is as follows:
[root@xj ~] # name=rm
[root@xj ~] # NAME=mk
[root@xj ~] # echo $NAME
Mk
[root@xj ~] # echo $name
Rm
3. The use of variables
Example: assign the execution result of a command to a variable
[root@xj ~] # A = `date`
[root@xj ~] # echo $A
Tue Mar 8 21:12:45 CST 2016
[root@xj ~] # Bamboo $(date)
[root@xj ~] # echo $B
Tue Mar 8 21:13:33 CST 2016
The transitivity of the value of variables
[root@xj ~] # A = `date`
[root@xj ~] # Centra
[root@xj ~] # echo $C
Tue Mar 8 21:12:45 CST 2016
You can use variables and other characters to form a new string
[root@xj ~] # MYDIR=/home/rm/
[root@xj ~] # echo $MYDIR
/ home/rm/
[root@xj ~] # echo $MYDIRzhangsan
The output is empty
[root@xj ~] # echo ${MYDIR} zhangsan
/ home/rm/zhangsan
Assign multiple words to a variable:
[root@xj ~] # NAME= "mk docker"
[root@xj ~] # echo $NAME
Mk docker
[root@xj ~] # NAME= "rm $NAME"
[root@xj ~] # echo $NAME
Rm mk docker
[root@xj ~] # NAME='rm $NAME'
[root@xj ~] # echo $NAME
Rm $NAME
The difference between single quotation marks and double quotation marks: single quotation marks assign the contents between variables intact. Double quotation marks cancel the role of spaces and retain the meaning of special symbols.
Delete a variable:
[root@xj ~] # echo $NAME
Rm $NAME
[root@xj ~] # unset NAME
[root@xj ~] # echo $NAME
4. Location variables and special variables in Shell scripts
Position variable: when Shell interprets and executes a user's command, it takes the first word of the command line as the command name and the other words as parameters. Parameters that are determined by the position that appears on the command line are called position parameters.
Position variable: represented by $N
[root@xj ~] # vim b.sh
#! / bin/bash
Echo "first directory is $1"
Echo "second directory is $2"
[root@xj ~] # chmod + x b.sh
[root@xj] #. / b.sh / home/ / etc/
The first directory is / home/
The second directory is / etc/
$0 the file name of this program example.sh
The nth parameter value of this program, n. N.
Special variables:
Some variables are set at the beginning of the Script script and cannot be modified, but instead of calling it a read-only system variable, we call it a special variable. These variables are available as soon as the program is executed, and here are some isometric variables:
$* all parameters of this program
$# the number of parameters of this program
$PID of this program
$! Execute the PID of the previous daemon
$? The return value of executing the previous instruction
[root@xj ~] # vim z.sh
#! / bin/bash
Echo "$* represents all the parameters of this program"
Echo "$# indicates the number of parameters in this program"
Touch / root/a.txt
Echo "$$represents the PID of this program"
Touch / root/b.txt &
Echo "$! represents the PID of the previous background instruction"
Echo "$$represents the process PID of the program"
[root@xj ~] #. / z.sh aaa bbb ccc
Aaa bbb ccc represents all the parameters of this program
3 indicates the number of parameters in this program
39839 indicates the PID of this program
39841 represents the PID of the previous background instruction
39839 indicates the process PID of the program
Example: the use of variables in shell
[root@xj ~] # vim z1.sh
#! / bin/bash
Var1= "abcd efg"
Echo $var1
Var2=1234
Echo "The value of var2 is $var2"
Echo $HOME
Echo $PATH
Echo $PWD
[root@xj ~] # chmod + x z1.sh
[root@xj ~] #. / z1.sh
Abcd efg
The value of var2 is 1234
/ root
/ etc/sysconfig:/etc/sysconfig:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
/ root
III. Commands in shell
1. The use of Read command
Function: read data from the keyboard and assign it to variables
[root@xj ~] # read a b c
1 22 33
[root@xj ~] # echo $a $b $c
1 22 33
Read is used in combination with parameter-p-n-t
-p (prompt statement)
-n (number of characters)
-t (waiting time)
[root@xj ~] # read-p "Please enter your admission number:" ID
Please enter your admission card number: 20160308
[root@xj ~] # echo $ID
20160308
[root@xj ~] # vim read.sh
#! / bin/bash
Read-p "Please enter your name:" NAME
Read-p Please enter your age: AGE
Read-p "Please enter your gender:" SEX
Echo "your basic information is as follows:"
Echo "name: $NAME"
Echo "Age: $AGE"
Echo "gender: $SEX"
[root@xj ~] # chmod + x read.sh
[root@xj ~] #. / read.sh
Please enter your name: Sun WuKong
Please enter your age: 500
Please enter your gender: male
Your basic information is as follows:
Name: Sun WuKong
Age: 500
Gender: male
2. The use of expr command
Function: arithmetic operation of Shell variable:
§expr command: arithmetic operations on integer variables
Syntax: expr expression / / Note: space between operators
[root@xj ~] # expr 3 + 2
five
[root@xj] # expr 8-3
five
[root@xj ~] # expr 8 / 4
two
[root@xj ~] # expr 8\ * 4
thirty-two
[root@xj ~] # expr 8% 3
two
Note:% remainder operation
Use operators with variables
[root@xj ~] # var1=16
[root@xj ~] # var2=4
[root@xj ~] # expr $var1-$var2
twelve
3. The use of arithmetic operators in shell scripts
[root@xj ~] # vim expr.sh
#! / bin/bash
Read-p "Please input the value of a" a
Read-p "Please input the value of b" b
Read-p "Please input the value of c" c
Value1= `expr $a + $b + $c`
Echo "The value of value1 is $value1"
Value2= `expr $c / $b`
Echo "The value of value2 is $value2"
Value3= `expr $c\ * $b`
Echo "The value of value3 is $value3"
Value4= `expr $a + $c / $b`
Echo "The value of value4 is $value4"
[root@xj ~] #. / expr.sh
Please input the value of a 10
Please input the value of b 20
Please input the value of c 30
The value of value1 is 60
The value of value2 is 1
The value of value3 is 600
The value of value4 is 11
Complex operation
[root@xj ~] # expr $(expr $(expr 5 + 11) / $var3) + 4
twelve
The above is all the content of the article "what is the Shell script?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.