Shell Script Basics (i)

Source: Internet
Author: User
Tags time and date

First, Shell script introduction

The shell is a program written in C that is a bridge for users to use Linux. The shell is both a command language and a programming language.

  • The shell is a scripting language;
  • Can use logic judgment, loop and other syntax;
  • can be customized functions;
  • The shell is a collection of system commands;
  • Shell script can realize the automation operation and maintenance, can greatly increase our operation and maintenance efficiency;
Ii. shell script structure and execution 1, structure
  • The beginning requires "#!/bin/bash";
  • The line in the script content that begins with # is interpreted as a description;
  • Writing script notes: Author, time, function and other information, convenient to view later;
  • The script's name ends with ". Sh", which distinguishes it from a shell script;
2, the way of implementation

1, as an executable program: Add execution permissions to the script chmod a+x test.sh , and then execute the script directly ./test.sh ;
2, as the Interpreter parameter: using SH execution # sh test.sh ;

3. Parameters

-X: sh -x test.sh View the script execution process
-N: sh -n test.sh detecting syntax errors

Iii. use of the date command

The date command is used to display or set the system time and date.
Command options:

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

Command instance:

[[email protected] ~]# Date//Display current DateTime April 17, 2018 Tuesday 19:54:50 cst[[email protected] ~]# cal  Current calendar April 2018 Day 1,234,561 2 3 4 5 6 7 8 9 10 11 12 13 1415 16 17 18 19 20 2122 23 24 25 26 2829 30[[email protected] ~]# date +%y//Display year 2018[[email protected] ~]# date +%y//year abbreviated 18 [[email protected] ~]# date "+%y-%m-%d%h:%m:%s%w"//month day, time division seconds, week 2018-04-17 19:58:30 2[[email prote CTED] ~]# Date +%f//Full date of another display 2018-04-17[[email protected] ~]# date +%w//week of the year [[Email pro Tected] ~]# Date +%t//Current time 20:01:19[[email protected] ~]# date "+%f%T"//datetime 2018-04-17 20:01: 45[[email protected] ~]# Date +%s//timestamp (shows the number of seconds from January 1, 1970 00:00:00 to the current experience) 1523966522[[email protected] ~ ]# date-d @1523966522//timestamp converted to specific date April 17, 2018 Tuesday 20:02:02 cst[[email protected] ~]# date +%s-d "201       8-04-17 20:04:59 "  Date converted to timestamp 1523966699[[email protected] ~]# date-d "-1 Year" +%y//previous 2017[[email protected] ~]# date-d "- 1 month "+%m//Previous month 03[[email protected] ~]# date-d"-1 day "+%d//The day before 16[[email protected] ~]# date-d" 2 Year 2month 2 Day "+%y-%m-%d//Current date next 2 years next February next 2 days 2020-06-19
Iv. variables in the shell
  • When a string is used in a script, multiple times and the length of the string is long, you should use a variable instead
  • When using conditional statements, use the variable if [$a-gt 1];then ...; Fi
  • Replace N=wc-l 1.txt with a variable when referencing the result of a command
  • When writing and user interaction scripts, variables are also essential for read-p "Lnput a number:" N; echo $n If you do not write this n, you can use $reply directly
  • Built-in variable $0,$1,$2 ... $ A represents the script itself, the first parameter, the second ... $#: Indicates the number of arguments
  • Mathematical operations a=1;b=2;c=$ ($a + $b) or $[$a + $b]

Shell Script Basics (i)

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.