Part Three Shell programming 3 (Shell Scripting 1)

Source: Internet
Author: User

Do monitoring and backup up to

1. What is a shell script?
It's a scripting language, not a programming language.
You can use some logical judgments, loops, and other syntax
You can customize the sub-function
is a collection of system commands
Shell scripts enable automated operations, greatly increasing our productivity

first shell script:
mkdir shell
" Span style= "font-size:15px;" >CD shell/
vim 1.sh
"
' #!/bin/bash
#This is a test shell Script
'
echo" 123456. "
"Touch aming.111

2. Shell script structure and execution methods
Start line Specify Bash path: #! /bin/bash
Lines that begin with # are interpreted as explanatory notes
The name of the script ends with. SH and is used to differentiate between this is a shell script
There are two ways of execution: chmod +x 1.sh;./1.sh
Bash 1.sh If you don't have Execute permissions
can also sh 1.sh can view ls-l/bin/sh

Bash-x 1.sh To view the script execution process


Cal output Calendar.
CAL-Y output year-round calendar

lang=en

3. Learn the usage of the date command
Date +%y-%m-%d, date +%y-%m-%d, date +%f Month Day
Date +%h:%m:%s = Date +%t time


Date +%s Timestamp
Date-d "+1day" a day later
Date-d " -1day" a day ago
Date-d " -1day" +%f what was the date a day ago?
Date-d " -1month" month ago

Date-d " -1hour" an hour ago
Date-d " -1hour" +%t an hour before the time is how much, the following several like

Date-d " -1min" a minute ago
Date-d " -1sec" a second ago
Date +%w, week date +%w week of the year, 52 weeks in a year

The time to run this script to generate a log, saved in the TMP directory
Vim 2.sh
#! /bin/bash
d= ' Date +%t '
Logname= $d. Log
touch/tmp/$logname

This is the log for the TMP.

BC is calculator
One is the direct input transport
An echo "365/7" |BC

4. Variables in shell scripts
You should use a variable instead when you use a string more frequently in your script and the string length is long
Variables are essential when using conditional statements
When referencing the result of a command, replace it with a variable
Variables are also necessary when writing scripts for user interaction
Built-in variables, $ ...
Mathematical Operation a=1;b=2; c=$ (($a + $b)) or $[$a + $b]

echo "SCALE=2;5/2" |BC output is 2.50

Vim 3.sh
#! /bin/bash
Read-p "Pleaswe input number" n
Echo $n

========
Read-t 3-p "Pleaswe input number" N T is the time-out
===============

Vim 4.sh
#! /bin/bash
Echo
echo $
Echo

Input parameters of the time of the output is itself 4.sh

Vim 5.sh
#!/bin/bash
n1=0
While :;: denotes true meaning, or it can be 1
Do
read-p "Please inpty a number:" N
m= ' echo $n |sed ' s/[0-9]//g ' m is to remove all the numbers and decide if it's empty .
l= ' echo $m |wc-c ' If it's a pure number, it's definitely empty, l if it doesn't range.
if [$l! = "1"]; then 1 is not empty and must contain numbers or pure letters
echo "The charater you input was not a number."
n1= ' echo ' $n "|BC ' l if not equal to 1, record him once.
Else
echo "Your number is $n" if the pure digit description is empty on the output, then exit
Exit 0
fi
if [$n 1 = = "5"];then exit if the input is not a number more than 5 times
Exit 1
fi
Done

--------------
A=
echo $a |wc-c C is a couple of strings
The output is 1 for the null character, because he has a line break

echo "12345a" |sed ' s/[0-9]//g ' here output is a

echo "12345" |sed ' s/[0-9]//g ' Here the output is empty

5. Shell

format 2:if condition; then statement; Else statement; Fi
format 3:if ...; Elif ...; Then ...; else ...; Fi
logical judgment expression: if [$a-gt $b]; if [$a-lt 5]; if [$b-eq 10]

-GT (>); -lt (<); -ge (>=); -le (<=);-eq (= =); -ne (! =) Note that there are spaces everywhere
can use && | | Combine multiple conditions
N=5
If [$n-gt 2] && [$n-lt 10]; then echo OK; else Echo Nook;fi
=
If [$n-gt 2-a $n-lt 1]; then echo OK; else Echo Nook;fi

-A is with the meaning
=========================
If [$n-gt 2] | | [$n-lt 1]; then echo OK; else Echo Nook;fi
=
If [$n-gt 2-o $n-lt 1]; then echo OK; else Echo Nook;fi

-O is or means

=========================
Format 1 and Format 2
Vim 6.sh
#! /bin/bash
n=10
If [$n-GT 5]
Then
echo "Greater than 5"
Else
echo "Not greater than 5"

Fi

======================
Format 3
Vim 6.sh
#! /bin/bash
N=3
If [$n-GT 5]
Then
echo "Greater than 5"
elif [$n-lt 2]
Then
echo "Less than 2"
Else
echo "Greater than 2"

Fi


6. If determine file, directory properties
[-F file] Determines if it is a normal file, and there is
[-D file] Determines if it is a directory and exists
[-e File] Determines whether the files or directories exist ======
[-R File] to determine if the document is readable
[-W file] Determines whether the file is writable
[-X file] Determines whether the file is executable

echo $RANDOM generate a random number
Echo $RANDOM $random
echo $RANDOM $random$random |cut-c 1-10 as long as the first 10 bits
Or
mkpasswd
Mkpasswd-l 20
Mkpasswd-l 20-s 0 Special symbols
Mkpasswd-l 20-s 0-c 0-c 0

Part Three Shell programming 3 (Shell Scripting 1)

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.