Shell script introduction, structure, execution, date usage

Shell script introduction

Shell is a program written in C language, which is a bridge for users to use Linux. Shell is not only a command language, but also a programming language.

  • shell is a scripting language;
  • It can use logic judgment, circulation and other syntax;
  • Customizable function;
  • shell is a collection of system commands;
  • shell script can realize automatic operation and maintenance, which can greatly increase our operation and maintenance efficiency;

Shell script structure and execution

structure

"ා / bin/bash" is required at the beginning;

The lines beginning with #, in the content of the script, serve as the explanation;

Notes when writing script: author, time, function and other information, convenient to view later;

The name of the script ends with ". sh", which is used to distinguish this is a shell script;

Execution method

1. As an executable program: add the execution permission "chmod a+x test.sh" to the script, and then directly execute the script ". / test.sh";
2. As interpreter parameter: execute sh test.sh with SH;

sh parameter

-x: sh -x test.sh view script execution

-n: sh -n test.sh syntax error detected

date command usage

Command description:

The date command displays or sets the system time and date.

Command options:

-d: Displays the date and time specified by the string (the string must be preceded by double quotes)
-s: Set the time and date according to the string (double quotation marks must be added before and after the string)

Command example

[root@dl-001 shell]# date       //View current time
2018year 01month 20Saturday, Sunday 10:10:25 CST
[root@dl-001 shell]# cal            //View system date
      //January 2018     
//Day one two three four five six
    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

[root@dl-001 shell]# date +%Y           //View current year (four digit display)
2018
[root@dl-001 shell]# date +%y           //View current year (simplified display)
18
[root@dl-001 shell]# date "+%Y-%m-%d %H:%M:%S %w"   //View current month day hour minute second week
2018-01-20 10:11:15 6
[root@dl-001 shell]# date +%F       //View the current full date
2018-01-20
[root@dl-001 shell]# date +%W           //View the current week of the year
03
[root@dl-001 shell]# date +%T           //View the current time
10:11:39
[root@dl-001 shell]# date +%s           //View timestamps (shows the number of seconds that have passed since 00:00:00, January 1, 1970)
1516414309

//Conversion of timestamps
[root@dl-001 shell]# date +%s -d  "2018-01-20 22:00:00"
1516456800
[root@dl-001 shell]# date -d @1516456800
2018year 01month 20Saturday, Sunday 22:00:00 CST

//Print specified date and time
[root@dl-001 shell]# date -d "-2 day" +%d
18
[root@dl-001 shell]# date -d "-1 year -3 month -1 day" +%Y-%m-%d
2016-10-19

//Set the current time manually
[root@dl-001 shell]# date -s "2017-5-20 12:00:00"
2017year 05month 20Saturday, Sunday 12:00:00 CST
[root@dl-001 shell]# date
2017year 05month 20Saturday, Sunday 12:00:03 CST


//Synchronize network time
[root@dl-001 shell]# yum install -y ntp
[root@dl-001 shell]# ntpdate ntp.ubuntu.com
20 Jan 10:47:42 ntpdate[8615]: step time server 91.189.89.198 offset 21163558.201100 sec
[root@dl-001 shell]# date
2018year 01month 20Saturday, Sunday 10:47:45 CST

Tags: shell Linux Programming network

Posted on Tue, 05 May 2020 11:18:08 -0400 by UmneyDurak