Shell script structure, date command, variable

Source: Internet
Author: User
Tags time and date time and seconds

20.1 Shell Introduction

Shell is a scripting language
You can use logical judgments, loops, and other syntax
Customizable functions
The shell is a collection of system commands
Shell scripts enable automated operations, which can greatly increase our operational efficiency
20.2 shell script Structure and execution

Structure

"#!/bin/bash" is required at the beginning.
Lines in the script content that begin with # are interpreted as explanatory notes
Write a script note: Author, time, features and other information, easy to view later
The name of the script ends with ". Sh" and is used to differentiate between this is a shell script
Execution method

Add the Execute permission "chmod a+x test.sh" to the script and execute the script directly "./test.sh"
Bash Test.sh;sh test.sh
SH parameter

-x:sh-x test.sh Viewing the script execution process
-n:sh-n test.sh to see if there is a syntax error in the script
eg

[Email protected] sbin]# vim test.sh
#!/bin/bash
#This is a example of shell structure
#Written by Adai

Ehco "Hello World"
20.3 Date Command usage

The date command is used to display or set the system time and date.
Syntax: date [option] parameter

Options:
-D <string>: Displays the date and time specified by the string (double quotation marks must precede and before the string)
-s<string>: Set the time and date based on the string (double quotes must be added before and after the string)

Parameters:
<+ time and date format;: Specifies the format of the date and time display

Displays the current time zone:

[[Email protected] sbin]# Date
Sunday, September 03, 2017 19:36:32 CST
Common date formats

View System Calendars
[[email protected] sbin]# cal
September on 2017
Day 123456
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
Note: Cal=calendar (Calendar), "cal-y" can view the calendar for one year.

Date +%y (%Y): Displays the year in four-bit (two-bit) numeric format
[Email protected] sbin]# date +%y
2017
[Email protected] sbin]# date +%y
17
Date "+%y-%m-%d%h:%m:%s%w"
The above parameters represent: year, month, day, time, minute, second, week
[Email protected] sbin]# Date "+%y-%m-%d%h:%m:%s%w"
2017-09-03 19:54:57 0
NOTE: Double quotation marks are required if there is a special symbol in the middle of the above parameter combination.

Date +%f: Displays the full month day
[Email protected] sbin]# date +%f
2017-09-03
Date +%w: Shows the current time is the week ordinal of a year
[Email protected] sbin]# date +%w
35
Date +%t: Shows the current time
[Email protected] sbin]# date +%t
18:55:35
Date +%s: Timestamp, showing the number of seconds from January 1, 1970 00:00:00 to the current experience
[Email protected] sbin]# date +%s
1504440220

Time Stamp conversion:
[[Email protected] sbin]# date +%s-d "2017-09-01 12:00:00"
1504238400
[Email protected] sbin]# date-d @1504238400
Friday, September 01, 2017 12:00:00 CST

Print specified date & time

Sometimes you need to use the date or time before n days (hours, minutes, seconds).

Format: date-d "1 day" +%d

[Email protected] sbin]# date-d "-2 Day" +%d
01
[Email protected] sbin]# date-d "-1 year-2 month-1 Day" +%y-%m-%d
2016-07-02
Note: Specify a time or date, followed by the corresponding time format parameter (the same method is used for the same time and seconds).

Time setting

Manual setup Time: Date-s "x-x-x x:x:x:"
[[email protected] sbin]# date-s "2016-9-3 12:10:00"
Saturday, September 03, 2016 12:10:00 CST
Synchronize Network time: Ntpdate command
[[email protected] sbin]# Yum install-y NTP
#安装ntp的同时会同步安装ntpdate命令

[Email protected] sbin]# ntpdate ntp.ubuntu.com
3 Sep 20:28:54 ntpdate[3407]: Step time server 91.189.91.157 offset 31565561.035581 sec

[[Email protected] sbin]# Date
Sunday, September 03, 2017 20:29:02 CST
Description: Ntpdate followed by NTP time server address (domestic NTP time server address: http://www.cnblogs.com/JemBai/archive/2012/04/15/2450045.html).

20.4 variables in a shell script

When a string is used more frequently in a script and the character creation length is long, a variable should be used instead of the string.

Common variables

[Email protected] sbin]# vim variate.sh
#!/bin/bash
D=date +%F
echo "Today is $d"

[Email protected] sbin]# sh variate.sh
Today is 2017-09-03
Description: The script defines the variable d for the current day
Note: To use the inverse quotation marks when defining the command result as a variable in a shell script, call the variable's method: "$ variable name".

Built-in variables

$0,$1,$2,$3 ...

$ A: Represents the script itself
$: First parameter
$ A: The second parameter
$#: Indicates the number of parameters
Mathematical operations

[Email protected] sbin]# vim sum.sh
#!/bin/bash
A=1
b=2
sum=$[$a + $b]
echo "$a + $b = $sum"

[Email protected] sbin]# sh sum.sh
1+2=3
Note: The mathematical operation is to be enclosed in [] and preceded by the symbol $.

and user interaction modes

[Email protected] sbin]# vim sum.sh
#!/bin/bash
Read-p "Please input a number:" X
Read-p "Please input a number:" Y
sum=$[$x + $y]
echo "$x + $y = $sum"

[Email protected] sbin]# sh sum.sh
Please input a number:5
Please input a number:5
5+5=10
Note: The read command is used to interact with the user, which takes the user input string as the variable value, and you can use the-t option to specify the time to wait when the value is read (the script exits automatically after the time is exceeded).

Shell Script Preset variables

Sometimes with a command like/etc/init.d/iptables restart, the previous/etc/init.d/iptables file is actually a shell script, and the subsequent string restart is the default variable.

[Email protected] sbin]# vim option.sh
#!/bin/bash
SUM=$[$1+$2]
echo "Sum=$1+$2= $sum"
echo "Result of $"

[[Email protected] sbin]# SH option.sh 3 6
Sum=3+6=9
Result of option.sh
Description: The script's $ and $ are the shell's default variables, respectively, the first and second parameters of the script, the shell script preset variables are not limited, pay attention to the name of the bit script itself.

Shell script structure, date command, variable

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.