basic shell programming (practical) and common linux commands

Declaring variables
X = 5
echo $ X --- "Output 5
Z = X + 6 $
echo $ Z ---" 5 + 6 output
superimposed variables:
X = 123
Y = "$ X" 456
echo $ Y - -> output 123456

the SET can view all the variables of the system already in force include: system and that the definition of variables.

-u set
echo $ A -----> Output spaces or empty
if the input set in advance -u
enter echo $ a ----- "will complain when calling undeclared variables.

unset variable name deleted variable
unset name without $, because deletion is the value of a variable rather than a variable.

Environment variables:
environment variables and user-defined variables difference?
User-defined variables only take effect in the current Shell in
environment variables to take effect in all the current sub Shell Shell and the Shell's
environment variables are global variables
User-defined variables are local

input bash in the current shell into the sub-shell
can enter pstree View process tree: you can see two bash, indicating that there is currently a shell, the other is the son shell.
Exit from the current sub-shell to shell, enter exit

environment variable name and role of the system in force is fixed.

Set the environment variable:
export variable name value of the variable
or variable name variable value
export variable name

to see all the variables:
the SET: See all the variables
env: View environment variable
to delete an environment variable:
unset variable name

suggestion environment variable names written in capital, primarily and command distinction, because the linux commands are lowercase.

Common environment variables:
HOSTNAME: Host name
SHELL: current shell
TERM: interrupt context
HISTSIZE: history command bar number
SSH_CLIENT: The current operating environment is to use ssh connection, where the record client ip
SSH_TTY: the terminal ssh connection PTS / 1
the USER : user currently logged on

all linux script must # / bin / beginning bash!. Then go to define variables.

PATH environment variable:
PATH variable: Find route system command
echo $ PATH
# View the PATH environment variable
PATH = "$ PATH": / root / SH
# increase the value of the PATH variable

PS1 environment variables:
PS1 variables: Command Prompt setting
\ d : displays the date in the format "day of week"
\ H: display the full hostname. The default hostname "localhost.localdomain"
\ T: 24-hour time display, the format of "HH: the MM: the SS"
\ A: 24-hour time display, the format of "HH: the MM"
\ U: Display current user name
\ w: displays the full name of the current directory
\ W: displays the last directory of the current directory of
\ $: prompt. If the root user prompt is displayed, "#", will be displayed if an ordinary user prompt is "$"
for example: [root @ iZ2ze57xep3rm7w9t2l85cZ src] # PS1 = '[\ u @ \ A \ w] \ $'
execution : [root @ 09: 57 / usr / local / src] #
only currently in effect, restart recovery is: [root @ iZ2ze57xep3rm7w9t2l85cZ logs] #
execute: echo $ PS1
output: [\ u @ \ h \ W] \ $ ie the original PS1 is defined
ls \: wrap, default has not finished entering.

Language variables: current language query.
locale: View language.
Input: locale
output: [root @ 09: 57 / usr / local / src] # locale
LANG = en_US.UTF-8: US Department languages.
= The LC_CTYPE ". 8-the en_US.UTF"
the LC_NUMERIC = ". 8-the en_US.UTF"
the LC_TIME = ". 8-the en_US.UTF"
the LC_COLLATE = ". 8-the en_US.UTF"
the LC_MONETARY = ". 8-the en_US.UTF"
the LC_MESSAGES = "the en_US. . 8-UTF "
LC_PAPER =" the en_US.UTF-. 8 "
LC_NAME =" the en_US.UTF-. 8 "
LC_ADDRESS =" the en_US.UTF-. 8 "
LC_TELEPHONE =" the en_US.UTF-. 8 "
LC_MEASUREMENT =" the en_US.UTF-. 8 "
LC_IDENTIFICATION = "en_US.UTF-8"
LC_ALL =
statistical branch of the size of the command: df -h
output: [root @ iZ2ze57xep3rm7w9t2l85cZ src] df -h
Filesystem Size Used Avail the Use% Mounted ON: If the language is Chinese, this line will be displayed in Chinese.
/ dev / vda1 40G 4.8G 33G 13 % /
devtmpfs 486M 0 486M 0% / dev
tmpfs 496m 0 496m 0% / dev / SHM
tmpfs 496m 452K 496m. 1% / RUN
tmpfs 496m 0 496m 0% / SYS / FS / a cgroup
tmpfs 100M 0 100M 0% / RUN / User / 0

LANG: main language of the definition of system variables
Display the current language: echo $ LANG
see Linux supports all languages: locale -a | More
LC_ALL: define the overall language of variable

cat / etc / sysconfig / i18n: Linux everything is a file, put the document shall be permanently effective. The language here is the language of the current linux, and restart or the next boot configuration language is still here.

Location parameter variables:
$ n-: n-digital, $ 0 for the command itself, $ 1- $ 9 representing the first to ninth parameters than ten parameters need braces include, for example $ {10}
example. 1:
! # / Bin / bash
num1 = $ 1
num2 = $ 2
Shi sum = $ (($ num1 + $ num2)) # num1 value of $ 1, the value of num2 is $ 2
echo $ sum: the value of print variable sum of
execution to give permission: chmod 755 ** sh.
1. the owner of the file read-write executables 6: readable and writable
2. the owner of the file to other users belong to a group of user-readable executable
3. the other user groups readable executable
Execution:. ./** sh 10 11
Output: 21
or more for developers to use, not suitable for use by third parties.
$ *: This variable represents all of the command line parameters, $ * all the parameters as a whole
$ @: This variable also represents all of the command line parameters, but each parameter to $ @ distinguished between
$ #: This variable represents the command line parameters for all the number

for the X-in "$ *"
do
echo $ the X-
DONE

for the y-in "$ @"
do
echo $ the y-
DONE
[root @ iZ2ze57xep3rm7w9t2l85cZ local] # chmod 755 zhaopeng.sh
[root @ iZ2ze57xep3rm7w9t2l85cZ local] # ./zhaopeng.sh 1 2 3
1 2 3
1
2
3
assume $ * there are four numbers, then the four numbers as a whole, so only one cycle
and there will be $ @ 4 cycles.

Predefined variables:
$ ?: return to the state of the last command executed. If this variable is 0, the proof of a command executed correctly; if this variable is non-zero (which specific number, by the command to decide), it proves a command is not correct.
$$: the current process process ID (PID)
$ !: process ID of the last process running in the background (PID)

Accept keyboard input: read
read [options] [variable name]
Options:
-p "message": read while waiting for input, output message
-t seconds: read command has been waiting for user input, you can use this option to specify the wait time
-n number of characters: read command accepts only the specified number of characters, will perform
-s: hidden data input, input applies to confidential information
write a script read.sh:
# / bin / bash!
the Read -p "Please your name iNPUT: "30 -t name
echo $ name
output when executed: Please your name iNPUT: 123
123
If the wait 30 seconds have not entered, the system automatically end script commands.
-p the Read "Please the INPUT your passwd:" -s password
echo -e "\ the n-": newline
echo $ password
output when executed: do not display the password in plain text as you type.

read -p "please input your sex [ M / F]:" -n 1 sex: only inputs a character. M or N
echo -e "\ n-": newline
echo $ sex

judged by file type
-b file: determining whether the file exists, and whether the file is a block device (block device file is true)
-c file: determining whether the file exists, and whether the file is a character device (true is a character file)
-d file: determining whether the file exists, and whether the file is a directory (a directory is true) common
-e file: determining whether the file exists (exist true) common
-f file: determining whether the file exists, and whether a regular file (normal file is true) common
-L file: determining whether the file exists, and whether the file is a symbolic link (a symbolic link file is true)
-p file: determining whether there is a file changed, and whether the file is a pipe (pipe files is true)
-s file: determining whether the file exists, and if non-empty (non-null true )
-S file: determining whether the file exists, and whether the file is a socket (socket file is true)
[the root @ iZ2ze57xep3rm7w9t2l85cZ local] # [zhaopeng.sh -e]
[local iZ2ze57xep3rm7w9t2l85cZ the root @] # echo $ ?
0 is true, there is
[root @ iZ2ze57xep3rm7w9t2l85cZ local] # [-e zhaopeng.sh] || echo && echo yes NO
yes
[root @ iZ2ze57xep3rm7w9t2l85cZ local] # [-e zhaop eng1.sh] && echo yes || echo NO
NO
[root @ iZ2ze57xep3rm7w9t2l85cZ local] # [-f zhaopeng.sh] || echo && echo yes NO
yes
[root @ iZ2ze57xep3rm7w9t2l85cZ local] # [-d zhaopeng.sh] && echo yes || echo NO
NO
[root @ iZ2ze57xep3rm7w9t2l85cZ local] # [-d etc] || echo && echo yes NO
yes

judged in file permissions:
-r file: to determine whether the file exists, and if the file has read permissions (read access is true)
-w file: to determine whether the file exists, and if the file has write permissions (you have write access to true)
-x file: to determine whether the file exists, and if the file has execute permission (Executive authority is true)
-u file: to determine whether the file exists, and if the file has SUID privileges (SUID have permission to be true)
-g file: the judge if a file exists, and if the file has SGID permissions (SGID permissions have true)
-k file: to determine whether the file exists, and if the file has SBit privileges (permissions have SBit is true)
[root @ iZ2ze57xep3rm7w9t2l85cZ local] # [ zhaopeng.sh -w]
[root @ iZ2ze57xep3rm7w9t2l85cZ local] # echo $?
0
[root @ iZ2ze57xep3rm7w9t2l85cZ local] # [-r zhaopeng.sh]
[root @ iZ2ze57xep3rm7w9t2l85c Local the Z-] # echo $?
0
[root@iZ2ze57xep3rm7w9t2l85cZ local]#
[root@iZ2ze57xep3rm7w9t2l85cZ local]#
[root@iZ2ze57xep3rm7w9t2l85cZ local]# [ -x zhaopeng.sh ]
[root@iZ2ze57xep3rm7w9t2l85cZ local]# echo $?
0
[root@iZ2ze57xep3rm7w9t2l85cZ local]#
[root@iZ2ze57xep3rm7w9t2l85cZ local]#
[root@iZ2ze57xep3rm7w9t2l85cZ local]# [ -u zhaopeng.sh ]
[root@iZ2ze57xep3rm7w9t2l85cZ local]# echo $?
1
[root@iZ2ze57xep3rm7w9t2l85cZ local]#
[root@iZ2ze57xep3rm7w9t2l85cZ local]#
[root@iZ2ze57xep3rm7w9t2l85cZ local]# [ -g zhaopeng.sh ]
[root@iZ2ze57xep3rm7w9t2l85cZ local]# echo $?
1
[root@iZ2ze57xep3rm7w9t2l85cZ local]#
[root@iZ2ze57xep3rm7w9t2l85cZ local]#
[root@iZ2ze57xep3rm7w9t2l85cZ local]# [ -k zhaopeng.sh ]
[root@iZ2ze57xep3rm7w9t2l85cZ local]# echo $?
1
[root@iZ2ze57xep3rm7w9t2l85cZ local]#
[root@iZ2ze57xep3rm7w9t2l85cZ local]#
[root@iZ2ze57xep3rm7w9t2l85cZ local]#
[root@iZ2ze57xep3rm7w9t2l85cZ local]#
[root@iZ2ze57xep3rm7w9t2l85cZ local]# [ -w zhaopeng1.sh]
-bash: [: missing `]'
[root@iZ2ze57xep3rm7w9t2l85cZ local]# [ -w zhaopeng1.sh ]
[root@iZ2ze57xep3rm7w9t2l85cZ local]# echo $?

Compared between the two files
Document 1 -nt, File 2: 1 modified file is determined whether a new folder 2 (True if new)
file 1 -ot Document 2: 1 modified determines whether the file than the file 2 old (if the old true)
file 1 -ef file 2: consistent lnode No. 1 to determine whether the file and the file 2, can be understood as whether two files are the same file.
This judgment is used to determine a hard link is a good way

Comparison between two integers:
an integer of 1 -eq integer of 2: determining whether the integer 1 and an integer equal to 2 (equal to the true)
integer of 1 -ne integer of 2: 1 is determined whether the integer 2 are not equal and integer (true not equal)
integer 1 -gt integer of 2: 1 is greater than an integer determined integer (greater than is true)
an integer of 1 -LT-integer of 2: 1 is less than an integer determined integer (less than is true)
an integer of 1 -ge integer of 2: 1 is an integer greater than or equal Analyzing integer (greater than or equal to true)
integer of 1 -le integer of 2: 1 is less than determined integer equal to an integer (true or less)

determines the string:
the -Z string: determining whether the string is empty (empty returns true )
-n string: determining whether the string is non-empty (non-return true empty)
string string == 1 2: 1 is determined whether the character string, and the string 2 are equal (equal returns true)
! = string string 1 2: determining whether the string 1 and string 2 are not equal (not equal returns true)
[the root @ iZ2ze57xep3rm7w9t2l85cZ local] # = Fengjie name
[@ iZ2ze57xep3rm7w9t2l85cZ the root local] # = NAME2 JJJ
[@ iZ2ze57xep3rm7w9t2l85cZ the root local] # [ "$ name "==" $ name2 "] && echo yes NO echo ||
NO
[root @ iZ2ze57xep3rm7w9t2l85cZ local] # [ "$ name"! = "$ NAME2"] &&

Multiple conditional:
determining a 2 -a determination: logic, and determines a judgment 2 are true, the end result will be true
determination Analyzing 2 -o 1: or logic, and a determination has 2 determines a set up, the end result will be true
determination: non logic, determines that the original formula negated!

above [] is the test command, but are commonly used []

flow control statements:
single branch if condition
if [conditional formula]; then
program
fi
or:
IF [conditional formula]
the then
program
fi
single branch conditional statement few points to note
1, if the end of the statement use fi, and the end braces typically use different languages
2, [conditional formula] is determined using the test command , there must be a space between the brackets and the conditional formula
after 3, then program execution with back after qualified, can be placed in [], with ";" split. It can also wrap write, you do not ";" an

example of 1: determine whether the root user login
to view the current user: USER whoami under which the current user is the env
env | grep "USER": visiting with USER the row of data.
cut -d "=" -f 2: equal sign as the delimiter, the interception of the second character. -d used to define the separator, default tab key, which field indicates the need to obtain -f

[root @ iZ2ze57xep3rm7w9t2l85cZ local] More testzhao.sh #
! # / bin / bash
# find out the current user, the interception of the current user, the current user name given to the Test
the Test = $ (env | grep "the USER" | Cut -d "= "-f 2)
# value obtained test string matching root and, if equal, the output of root Shi yonghu dangqian
IF [" $ test "== root]
the then
echo" root dangqian yonghu Shi "
Fi
[iZ2ze57xep3rm7w9t2l85cZ local root @ ] # ./testzhao.sh
dangqian yonghu Shi the root

determines usage partition
# View system branches:
[iZ2ze57xep3rm7w9t2l85cZ the root @ ~] # DF -H
the Filesystem Size Used Avail the Use% Mounted ON
/ dev / 4.8G 33G 13 is Vdal 4OG% /
devtmpfs 0% 0 486M 486M / dev
tmpfs 496m 496m 0 0% / dev / SHM
tmpfs 496m 496m 484K. 1% / RUN
tmpfs 496m 496m 0 0% / SYS / FS / a cgroup
100M 0 0 100M% tmpfs / RUN / User / 0
# branch-View
[root @ iZ2ze57xep3rm7w9t2l85cZ ~] -H DF # | grep "Vdal"
/ dev / 4.8G 33G 13 is Vdal 4OG% /
# output of the fifth column
[root @ ~ iZ2ze57xep3rm7w9t2l85cZ] -H DF # | grep "Vdal" | awk '{}. 5 Print $'
13 is%
# while the output of the fifth column as a delimiter%, taken the first character
[root @ iZ2ze57xep3rm7w9t2l85cZ ~] # df -h | grep "Vdal" | awk '{}. 5 Print $' | Cut -d "%". 1 -f
13 is

! # / bin / the bash
# usage taken root branch to% split
rate = $ (df -h | grep "Vdal" | awk '{}. 5 Print $' | Cut -d "%" -f. 1)
# rate is less than the determination value 80 output genmulu. . . . Otherwise, output busuanda.
IF [ "$ Rate" -le 80]
the then
echo "genmulu Remedies] Guo DA"
the else
echo "




then
the condition is true, program execution
else
another program when the condition is not satisfied, the implementation of the
fi

Example: determines whether the input is not a directory
# / bin / bash!
The Read -p "Please enter a directory:" -t 30 dir
IF [-d "$ dir"] # information to determine whether the input is present and whether the file is a directory
the then
echo "you enter a directory"
the else
echo "nononono"
fi

determine apache service is running ps aux: View all running programs
! # / bin / bash
tomzhao = $ (PS -ef | grep Tomcat | grep -v grep)
IF [-n "$ tomzhao"]
the then echo "$ (DATE) Tomcat is running ......" >> /usr/local/zhaopeng.log
the else
/ usr / local / Tomcat / the Apache-tomcat- 8.5.45 / bin / startup.sh &> / dev / null
echo "$ (DATE) Tomcat has restarted ......" >> / usr / local / zhaopengerr.log
Fi

multi-branch if condition
if [Condition judging Formula 1]
the then
when the condition determination formula 1 is true, execute the program. 1
elif [conditional Formula 2]
the then
When the conditional formula 2 is established, the program execution 2
... more conditions omitted ...
the else
when all of the conditions are not established, and finally execute this program
fi

seq 1 10: used to generate all integers from one to a number between a number of additional
   embodiment a:
   # 10 SEQ. 1
   result 12,345,678,910

[root@iZ2ze57xep3rm7w9t2l85cZ local]# for i in $(seq 1 10)
> do
> echo i=$(seq 1 10)
> done
i=1 2 3 4 5 6 7 8 9 10
i=1 2 3 4 5 6 7 8 9 10
i=1 2 3 4 5 6 7 8 9 10
i=1 2 3 4 5 6 7 8 9 10
i=1 2 3 4 5 6 7 8 9 10
i=1 2 3 4 5 6 7 8 9 10
i=1 2 3 4 5 6 7 8 9 10
i=1 2 3 4 5 6 7 8 9 10
i=1 2 3 4 5 6 7 8 9 10
i=1 2 3 4 5 6 7 8 9 10

[root@iZ2ze57xep3rm7w9t2l85cZ local]# for i in $(seq 1 10)
> do
> echo $i
> done
1
2
3
4
5
6
7
8
9
10

Multi-branch case conditional statement:
case statement and if elif ... as are multi-branch conditional statement else statement, however, and if multi-branch conditional statement is different, case statement can only
judge a conditioned relationship, and can determine if statements the relationship between a variety of conditions.

case $ variable name in
"Value 1")
if the value of the variable equal to the value 1, the execution of the program 1
;;
"Value 2")
if the value of the variable equal to the value of 2, then the execution of the program 2
;;
... other branches omitted. ..
*)
If the variable value is not above, this procedure is performed
;;
Esac

example: yes or no input, the input is determined whether the yes or no input error or
read -p "please choose yes / no :" 30 CHO -t
Case $ CHO in
"yes")
echo "you choosed yes! you are right!"
;;
"NO")
echo "you choosed NO! you are right!"
;;
*)
echo "you are Wrong! ! "
;;
esac


Unzip multiple files, implemented for loop:
! # / Bin / bash
# into the corresponding directory to be operated
cd / usr / local / testdir
# View the number of compressed files ending in .tar.gz and recorded ll.log
ls .tar.gz *> ll.log
# View compressed files ll.log the number and names assigned to the circulation and i
for i in $ (CAT ll.log)
do
# perform decompression operations (background, do not print the log information and
tar-zxf $ i &> / dev / null
DONE
# delete the temporary log file ll.log
RM -rf /usr/local/testdir/ll.log

while loop:
while loop is variable cycle, also known as a conditional loop. As long as the conditional expression is established, the cycle will always continue, to know the conditions predicate does not hold, the cycle will stop. This cycle for a fixed and not the same.

the while [conditional formula]
do
procedure
done

Magical ctrl keys:
ctrl + A: Back to the current input / beginning of the line will be inserted into the character, do not hold down the arrow keys.
ctrl + e: combinations on the opposite, back end of the line.
ctrl + l: Empty current terminal interface, the effect is equivalent to the clear command.
ctrl + u: Clear the current input line of all inputs. Suppose you enter a aa bb, press this key combination, aa bb was deleted.
ctrl + y: is to paste back ctrl + u delete the string.
ctrl + r: command history search. After pressing ctrl + r, it contains the commands you enter will search string.
ctrl + c: terminate the current terminal program running.
ctrl + d: push current terminal.
ctrl + z: the terminal is currently running programs in the background operation.

Guess you like

Origin www.cnblogs.com/zp123456/p/12161614.html