Table of contents
Getting Started with Shell
Shell classification
For both Unix/Linux systems, the shell mainly consists of the following two types
Bourne shell
It also includes three types of sub-branches: Bourne shell (sh), Korn shell (ksh), and Bourne Again Shell (bash)
C shell
Including csh, tcsh two types
At present, the main study abroad is csh and bash
magic number
Create a sh file anywheres1.sh
write the following code
#! /bin/bash
echo tom
#!
It is called the magic number, and it indicates the shell interpreter used by the file
(for most linux systems, bash is currently used by default, but this line cannot be omitted)
Save the file, use the bash command to run it in the same directory, and find that the tom running code is outputbash s1.sh
This is the common sh beginning given in the book
#! /bin/sh
#! /bin/bash
#! /usr/bin/awk
#! /bin/sed
#! /usr/bin/tcl
#! /usr/bin/expect #<==expect解决交互式的语言开头解释器。
#! /usr/bin/perl #<==perl语言解释器。
#! /usr/bin/env python #<==python语言解释器。
note
Very simple, #
use
Shell Core and Practice
variable
Add the equal sign to the variable name to assign
Output variable value using dollar sign
#! /bin/bash
value="helloworld"
echo $value
In terminal mode, you can use the following three commands to obtain variables in the corresponding scope.
set
The command outputs all variables, including global variables and local variables;
env
the command only displays global variables;
declare
the command outputs all variables, functions, integers and exported variables
Common system environment variables
$HOME
: the directory that the user enters when logging in.
$UID
: UID (user ID) of the current user, equivalent to id -u.
$PWD
: The absolute pathname of the current working directory.
$SHELL
: Current SHELL.
$USER
:Current user.
quote output
a=123
Direct assignment without quotes, the value is parsed and output
a='123'
Single quotes, without any parsing, output whatever there is
a="123"
Double quotes, the variables and commands in the quotes will be parsed before outputting the content
special variable
$0
Get the full name of the file currently executing the script
$#
Get the number of parameters passed by the script
$*
and $@
both output the values of all incoming parameters, but will show different interpretation states according to whether the variable has single or double quotes
special state variable
$?
Get the return value of the execution status of the previous command
$$
Get the current shell script process number PID
$!
Get the process number of the previous background worker process
$_
Get the last parameter of the script that executed the command before
bash built-in variable commands
eval
Insert a new instruction at the current code execution position and execute it
aseval "echo $0"
exec
Execute the corresponding command without creating a new child process, and the process terminates after execution
.exec data
read
Read string information from standard input
exit
Exit the shell or go to the next digit
variable substring
That is, in a variable expression, use the corresponding variable substring symbol to achieve the effect of fast string operation
Instruction instance${#name}
special expansion variables
Use the special extended variable method to display the specified content at runtime for uninitialized or unassigned variables
In the following code, since the res variable is undefined, we :-
set
echo $test # 没有定义的变量直接调用就是0
res=${test:-UNDEFINED} # 定义未定义的变量的方法
echo $res # UNDEFINED
Shell variable numerical calculation practice
basic arithmetic operators
double parentheses
Double parentheses can be used as a simple expression, be sure to use the $ symbol when performing assignment or echo operations, otherwise an error will occur
echo $((100+200))
When the expression is executed on the command line, it is not necessary to add the symbol, just use the ( ( 6 symbol directly, and directly use the ((6%2)) form, but if output is required, it must be addedsymbol, directly use (( 6 symbols
“(())”
No spaces, one or more spaces between all the characters in will not affect the result
let
let is equivalent to double parentheses, you can write like this
let i=i+1
expr
Can be used for job hunting or expression calculation
The multiplication operation needs to be escaped with a backslash
expr 2 + 2 # 4
expr 2 \* 2 # 4
Wrap expr in backticks to make it appear as an expression
i=5
i=`expr $i + 6`
echo $i # 11
Use expr to do calculations, add an unknown variable and a known integer to see if the return value is 0, if it is 0, consider the variable to be added to be an integer, otherwise it is not an integer
You can use the length parameter to calculate the length of the corresponding string
str="asd"
expr length $str # 3
bc
bc is a calculator that comes with linux, we can also use it in the command line
echo 3+4|bc # 7
i=10
i=`echo $i+10|bc` # 20
awk
Suitable for decimal addition and subtraction
echo "7.7 8.2" |awk '{print ($1-$2)}' # -0.5
read
read can read user input
# -t 10 设置等待时间为10s
# -p "please input:"输入提示词
# num输入内容被赋予的变量
read -t 10 -p "please input:" num