Shell Training Day6 8.20

shell
See 100 linux shell questions.
Function to reduce duplication of code.
Shell is a collection of system commands

The first line starts with #!/bin/bash
#!/bin/bash
echo “123”
w
ls
Special case: chkconfig and description are not comments.

sh -x 1.txt

sh -n 1.txt

Check the script execution process bash -x 1.sh
Check whether the script is grammatical Error bash -n 1.sh

?date +%Y-%m-%d, date +%y-%m-%d year month day
? date +%H:%M :%S = date +%T time
? date +%s timestamp
? date -d @1504620492
? date -d “+1day” one day later
? date -d “- 1 day” one day ago
? date -d “-1 month” one month ago
? date -d “-1 min” one minute ago
? date +%w, date +%W week< /p>

[[emailprotected] shell]# date +%F
2019-08-22

[[emailprotected] shell]# date +%T
10: 12:42

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

[[emailprotected] shell]# date + %w –day of the week
4
[[emailprotected] shell]# date +%W –how many weeks of this year
33

[[emailprotected] 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

One day ago
[[email protected] shell]# date -d “-1 day” +%F
2019-08 -21
One month ago
[[email protected] sh ell]# date -d “-1 month” +%F
2019-07-22
One year ago
[[emailprotected] shell]# date -d “-1 year” +%F
2018-08-22

[[emailprotected] shell]# date +%s –time stamp
1566440271
[[emailprotected] shell]# date -d @ 1566440271 –Convert timestamp to date
Thu Aug 22 10:17:51 CST 2019

Convert specific time to timestamp
[[emailprotected] shell]# date +%s -d “2018-09-09 06:30:22”
1536445822

[[email protected] shell]# date “+%Y-%m-%d %H:%M: %S”
2019-08-22 14:21:16

Variable
? When a string is used frequently in the script and the string length is very long, you should use a variable instead
? When using conditional statements, variables are often used if [$a -gt 1 ]; then …; fi
? When referencing the result of a command, use variables instead of n=wc -l 1.txt
? When writing scripts that interact with users, variables are also essential. read -p “Input a number: “n; echo $n If you don’t write this n, you can use $REPLY directly
? Built-in variables $0, $1, $2… $0 means the script itself, $1 the first parameter, $2 the second… $# means the number of parameters
? Mathematical operations a=1;b=2 ; c=$(($a+$b)) or $[$a+$b]

Logical judgment
? Format 1: if condition; then statement; fi
? Format 2: if condition; then statement; else statement; fi
? Format 3: if …; then… ;elif …; then …; else …; fi
? logical judgment expression: if [$a -gt $b ]; if [$a -lt 5 ]; if [$b -eq 10] etc. -gt (>); -lt(<); -ge(>=); -le(<=);-eq(==); -ne(!=) Note that there are spaces everywhere
? You can use && || to combine multiple conditions
? if [$a -gt 5] && [$a -lt 10 ]; then
? if [$b -gt 5] || [$b -lt 3 ]; then

[[email protected] shell]# if [$a -gt 3 ]; then echo ok; fi
ok

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

[[email Protected] shell]# cat if2.sh
#!/bin/bash
a=2
if [$a -gt 3 ]
then
echo ok
else
echo “not ok”
fi

[[email protected] 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 generally use -gt a=2;if (($a>1)); then echo ok; fiok

Leave a Comment

Your email address will not be published.