shell training day6 8.20

shell
see 100 linux shell questions.
Function of reducing code duplication.
shell is a collection of system commands

! The first line to # / bin / bash begin with
# / bin / bash!
Echo "123"
w
LS
Special circumstances: chkconfig and the description is not a comment.

sh -x 1.txt

sh -n 1.txt

View script execution bash -x 1.sh
see if the script syntax errors bash -n 1.sh

• date +% Y-% m-% d, date +% y-% m-% d date
• date +% H:% M :% S = date +% T time
• date +% s timestamp
• -d @ 1504620492 DATE
• DATE -d "+ 1day" after a day
• date -d "-1 day" the day before the
• date -d "-1 month" in January before the
• date -d "-1 min" a minute ago
• date +% w, date + % W week

[root@docker shell]# date +%F
2019-08-22

[root@docker shell]# date +%T
10:12:42

[root@docker shell]# date +%F-%T
2019-08-22-10:13:29
[root@docker shell]# date +%Y-%m-%d-%H:%M:%S
2019-08-22-10:14:03

[root @ docker shell] # date +% w - day of the week
. 4
[the root Docker the shell @] + # DATE% W is - how many weeks of the year
33

[root@docker shell]# cal
August 2019
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

The day before
[the root @ Docker the shell] -d # DATE "Day -1" + F.%
2019-08-21
month ago
[Docker the shell the root @] -d # DATE "-1 month The"% F. +
2019-07- 22
a historical
[Docker the shell the root @] -d # DATE "year -1" + F.%
2018-08-22

[root @ docker shell] # date +% s - timestamp
1,566,440,271
[Docker the shell the root @] -d @ DATE # 1,566,440,271 - converted to time stamp the date
Thu Aug 22 10:17:51 CST 2019

Specific time stamp in terms of
[the root Docker the shell @] + # DATE% S -d "2018-09-09 06:30:22"
1536445822

[root@docker shell]# date "+%Y-%m-%d %H:%M:%S"
2019-08-22 14:21:16

Variables
• When a script used more frequently and a string variable instead should be used when a long string length
• When using a conditional statement, is often used a variable IF [$ A -gt. 1]; the then ...; Fi
• references when the results of a command to replace the variable with n = wc -l 1.txt
• when writing the script and user interaction, the variable is also essential read -p "Input a number:" n; echo $ n n If you did not write this, can be used directly $ the REPLY
• built-in variable $ 0, $ 1, $ 2 ... $ 0 for the script itself, $ 1 the first parameter, $ 2 second .... $ # represents the number of parameters
• mathematical operations a = 1; b = 2; c = $ (($ a + $ b) ) or $ [$ a + $ b]

Logical judgment
• Format 1: if condition; then statements; Fi
• Format 2: if condition; then statement; else statement; Fi
• Format. 3: IF ...; then ...; elif ...; then ...; else ...; Fi
• logic determination expression: if [$ a -gt $ b ]; if [$ a -lt 5]; if [$ b -eq 10] et -gt (>); -lt (< ); -ge (> =); -le (<=); -; (! =) eq (==) -ne Note everywhere spaces
• || && may be used in conjunction with a plurality of conditions
• if [$ a -gt 5] && [$ a -lt 10]; the then
• IF [$ B -gt. 5] || [-LT-B $. 3]; the then

[root@docker shell]# if [ $a -gt 3 ]; then echo ok; fi
ok

[root@docker shell]# cat if1.sh
#!/bin/bash
a=5
if [ $a -gt 3 ]
then
echo ok
fi

[root@docker shell]# cat if2.sh
#!/bin/bash
a=2
if [ $a -gt 3 ]
then
echo ok
else
echo "not ok"
fi

[root@docker shell]# cat if3.sh
#!/bin/bash
a=6
if [ $a -gt 3 ]
then
echo ">3"
elif [ $a -lt 6 ]
then
echo "<6"
else
echo "not ok"
fi

Support>, but this is generally used -gt
A = 2; IF (($ A>. 1)); the then echo OK; Fi
OK

Guess you like

Origin blog.51cto.com/testdb/2432140