Little White's way to the shell
System version Red Hat Enterprise Linux Server Release 7.0
[Basic articles] Understanding the shell and basic operators 1. What is a shell script:
The shell is a program written in C that is a bridge for users to use Linux. The shell is both a command language and a programming language.
A Shell is an application that provides an interface through which users access the service of the operating system kernel.
2. View the existing interpreter of this machine:
Cat /etc/shells
3. Modify the user's shell interpreter:
Useradd -s /bin/ksh zhangsan [Specify the login shell when creating user Zhang]
Usermod -s /bin/ksh zhangsan [Modify the login shell of the existing user Zhang San]
4. View history commands:
History
Vim /etc/profile [Enter configuration file]
HISTSIZE=1000 [Can modify the default history number]
History -c [clear history command]
5. Application of historical commands:
! [Serial number] [Command to execute the serial number again]
! Cat [Re-execute the command that started with cat recently]
6. Redirect standard input / output / error output
> Redirect normal output from command execution to file
>>: only collect the correct output of the previous command
2>: only collect the error output of the previous command
&>: Correct and error output of previous commands
>&2: Turn the correct output into the wrong output
Operator:
1.expr [Note: There are spaces between numbers]
[root@server0 ~]# expr 10 % 3 [To find the remainder of 10 and 3]
1
2.$[]
[root@server0 ~]# echo $[5*3] [See 5 times 3]
15
[root@server0 ~]# x=5 [Assign x and y respectively and find the product]
[root@server0 ~]# y=10
[root@server0 ~]# echo $[x*y]
50
3.let [Do not display results]
[root@server0 ~]# x=6
[root@server0 ~]# y=6
[root@server0 ~]# let i=x+y
[root@server0 ~]# echo $i
12
Shorthand operation:
[root@server0 ~]# x=5
[root@server0 ~]# let x++ [add 1 to x value]
[root@server0 ~]# echo $x
6
[root@server0 ~]# x=5
[root@server0 ~]# let x+=5 [add 5 for x value]
[root@server0 ~]# echo $x
10
Let x*=5 [for x value morning multiply 5]
Let x/=5 [divide the value of x by 5]
Let x%=5 [for x value]
4.bc [Calculator]
5+5
10
5. Take the small tree point after the x bit
Scale=x
Take two decimal places
[root@server0 ~]# echo "scale=2;2/10" | bc
.20
6. Compare: test or [parameter = parameter] [must have spaces in parentheses]
[root@server0 ~]# [ a = a ]
[root@server0 ~]# echo $? [Judge whether to execute]
0
7. Judging null -z
[root@server0 ~]# [ -z $p ]
[root@server0 ~]# echo $?
0
[root@server0 ~]# p=5
[root@server0 ~]# [ -z $p ]
[root@server0 ~]# echo $?
1
1. One line to play multiple commands
a && b Execute b only when a is successful
a || b Execute b only if a fails
a ; b execute a, execute b
Example:
Create user login for more than two email alerts
#!/bin/bash
Num=`who | wc -l`
[ $num -gt 2 ] && mail -s error root < /root/mail
Edit the message content vim /root/mail
2. Determine the status of the file/directory
-e determines whether the object exists, and the existence is true
-d determines whether the object is a directory, and the existence is true
-f determines whether the object is a file, and the existence is true
-r read
-w write
-o execute this
Note: root is true for all objects except "script"
[Moring Morning Light]#chmod -x who.sh
[Moring Morning Light]#ll who.sh
-rw-r--r--. 1 root root 81 April 24 11:52 who.sh
[Moring Morning Light]#[ -o who.sh ]
[Moring Morning Light]#echo $?
1
[Judgement] && command Execute command
[Judgement] || Command Execute command without it
! Negate
Single branch if can only judge the pair
Double branch if to judge right and wrong
Multi-branch if multiple judgment
First knowledge of the shell operator and judgment