Shell Scripting Learning

Source: Internet
Author: User
Tags logical operators

Regular expressions
^ Start tag

$ end Tag
[A-z]
[A-z]
[0-9]
. Single arbitrary character
* Number of previous character repetitions >=0
+ Number of previous character repetitions >=1
? Number of previous character repetitions 0 or 1
{N,m} The number of times the preceding character repeats N to M

[A-z] [0-9] [0-9] B12 B1a

# grep ' ^r.*n$ '/etc/passwd

Sed ' match conditions/actions performed '/etc/passwd
Or
cat/etc/passwd | Sed ' match conditions/actions performed '

Replace sed ' s/old/new/g '

Ifconfig | grep ' inet addr: '
|grep-v ' 127.0.0.1 ' | Sed ' s/inet addr://g ' | Sed ' s/bcast.*//g '

Awk
# awk-f: ' {print '} '/etc/passwd take the first column with a colon delimiter

---------------------------------------------------------------------------------------
Environment variable System--Multiple user set commands to view environment variables
--User environment variables (/root/.bash_profile)
--System environment variables (/etc/profile)

Positional variables
-typically used in conjunction with scripts
--$0 Script Name
--$1, $, $, $4...$9 position parameters

Pre-defined variables
--$? Represents the program exit representative (typically 0 for execution success, not 0 for execution failure)
echo $?
--$# represents the number of arguments for the current shell
--$* (an entire string) [email protected] (each parameter counts as a string) represents all parameters
--$$ PID of the current process

Custom variables
--The syntax format is: Name=[value]
--Note:
Variable equals cannot have spaces on either side
Variable is case sensitive
--Use ($ variable name) to invoke the value of the variable after it is defined


Arithmetic operations
-- + - * / %
--$ ((expression))
--$[expression]
--expr expression
a=2 (duplicate assignment will overwrite)
B=3
# echo $ (($a + $b)) or $ (a+b)
# echo $[$a + $b]
# expr 5 + 2 Note there are spaces
# expr $a + $b


Built-in test judgment succeeded/established--failure/not established
--test testing An expression
--[test expression] the most common
--Note: there must be a space between the test expression and the brackets

Numerical comparison
-eq equals True
-ne is not equal to true
-GT is greater than true
-ge is true if it is greater than or equal
-lt is less than true
-le is less than or equal to true


Example 1:
[Email protected] ~]# echo $a $b
2 3
[Email protected] ~]# test $a-lt $b
[[email protected] ~]# echo $?
0 successes
[Email protected] ~]# test $a-gt $b
[[email protected] ~]# echo $?
1 failure

Example 2:
[Email protected] ~]# echo $a $b
2 3
[Email protected] ~]# [$a-lt $b]
[[email protected] ~]# echo $?
0
[Email protected] ~]# [$a-gt $b]
[[email protected] ~]# echo $?
1


&& Logic and
--CMD1 && cmd2cmd1 successfully executed CMD2
|| Logical OR
--CMD1 | | Cmd2cmd1 fails to execute cmd2.
; No logical relationship
--cmd1; CMD2 cmd1 after execution, execute CMD2

String test (strings are enclosed in double quotes)
= Equals is True
! = is not equal is true
-Z String string length 0 is True
-N string string length not 0 is true

File test
-e File name True if the file exists
-D file name determines whether the directory
-f filename If file exists and is normal file true
-r file name True if the file exists and is readable
-W file name True if the file exists and is writable
-X file name if file exists and executable is true
-S file name True if the file exists and has at least one character
Linux also offers non-(! ), or (-O), and (-a) three logical operators,
Used to connect test conditions in order of precedence:! Highest,-a second,-o lowest

Date (man + command, viewing Help)
# date ' +%y%m%d%h%m '
# date-d ' 7 day ago ' +%y%m%d%h%m '
# date-s "2016-04-09 15:30:13" Modify system time (temporary)

Take string ${variable name: offset:length}
[[email protected] ~]# a= "201604091527"
[Email protected] ~]# echo $a
201604091527
[[email protected] ~]# echo ${a:2:3} skip two fetch 3
160

Take string echo ${variable name%.*}
[Email protected] ~]# a= "123.log"

[[email protected] ~]# echo ${a%.*}
123

Find File
-type-name
-size (file size)-mtime (file modification time)
Script Case:

#!/bin/bash
# #号表示注释
# The Shell script executes from the top down
Date
grep ' Root '/etc/passwd

Execute script:
# chmod +x 1.sh
#/tmp/1.sh

. /tmp/1.sh Execute File/tmp/1.sh


Control statements
#!/bin/bash
For i in {1..50}
Do
Mkdir/usr/local/src/dir$i
Done

#!/bin/bash
For i in ' seq ' #反引号代表命令预先执行
Do
Mkdir/usr/local/src/dir$i
Done

#!/bin/bash
For ((i=1;i<=10;i++));d o
sum=$ ((sum+i))
Done
Echo $SUM

#!/bin/bash
I=1
While [$i-le 10]
Do
sum=$ ((sum+i))
I=$[I+1]
Done
Echo $SUM

#!/bin/bash
While Read-r line
Do
echo $line: HELLO
Done </etc/passwd

Username:hello
Root:hello
Adm:hello

#!/bin/bash
While Read-r line
Do
Echo ' echo $line | Awk-f: ' {print $} ': HELLO
Done </etc/passwd

#!/bin/bash
if [-d/tmp/123];then
Ls/tmp/123
Else
Mkdir/tmp/123
Fi



Top Free


#!/bin/bash
Case $ in
Top
Top
;;
Free
Free
;;
df
Df
;;
*)
echo "USAGE:$0{TOP|FREE|DF}"
Esac

If for and case--control statement

Shell functions: Some commands that are repeatedly called can usually be placed inside a function
#!/bin/bash
Sum () {
echo $ (($1+$2))
}
Sum 5 6

Scheduled Tasks

Usage: at [TIME] specifies the time to execute a specific command

Example:
At 4:17# specifies that the scheduled task be performed 4:17 A.M. the same day
at> cp/etc/passwd/tmp# Scheduled Task content
At> <EOT> #输入完成后, press Ctrl+d to end recurring scheduled Tasks crontab

1) Service Crond status
Chkconfig--list | grep Crond
Crontab-e
0 2 * * 3 cp/etc/passwd/tmp Weekly 32-point backup
5 1 10,25 * * rm-rf/tmp/* 1:5 A.M., 10th and 25th per month
*/10 * * * * ntpdate time server IP performs time synchronization every 10 minutes
0 1-6 * * * daily from 1 o'clock in the morning to 6.

2) Restart the Crond Services service Crond restart

Tail-f Dynamic View

crontab [u user] [-l|-r|-e]

-L: List execution tasks

-U: Specifies a user, without the-u option, for the current user

-e: Make a scheduled task

Shell Scripting Learning

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.