Shell scripting introduction, structure and execution, variables, and date command usage

Source: Internet
Author: User

First, Shell script introduction

The shell is a scripting language, and the shell is a collection of system commands
1, can use logic judgment, cycle and other grammar;
It's a bit like C language, but it's fundamentally different.
2, can be customized functions;
The function is defined to reduce our duplication of code.
3, it can realize automatic operation and maintenance, can be executed in batch at the same time, can greatly increase our operation and maintenance efficiency;
To learn how to write a good shell will continue to practice.

II. Structure and implementation

1. Need to be added at the beginning

#!/bin/bash

Specifies that the BASH interpreter be used to interpret the line at the beginning of the #. The script name ends with a. SH and is used to differentiate between a shell script.
2. There are two ways to execute a shell script:

[[email protected] ~]# chmod a+x 1.sh #给脚本执行权限[[email protected] ~]# ./1.sh        #执行权限后用./直接执行[[email protected] ~]# bash 1.sh  #直接用bash执行12233...
[[email protected] ~]# bash -n 1.sh    # -n 查看脚本是不是语法错误[[email protected] ~]# bash -x 1.sh    #-x 查看脚本的执行过程+ echo 12233...12233...+ echo 111[[email protected] ~]#

Third, the use of the data command
1. Directly hit the data command it displays the date and time of the system.
The 2?data command is very useful in the shell, and it can be used to mark time.
Tagging method:

[[email protected] ~]# date +%y #年2018 [[email protected] ~]# date +%m #月07 [[email protected] ~]# date + %d #日13 [[email protected] ~]# date +%m #分钟58 [[email protected] ~]# date +%y #两位数的年18 [[email protected     ] ~]# date +%d #带/month Day 07/14/18[[email protected] ~]# date +%y%m%d 20180714[[email protected] ~]# date +%F #带横线的年月日2018 -07-14[[email protected] ~]# date +%h #小时00 [[email protected] ~]# date +%s #秒38 [[email  Protected] ~]# Date +%s #时间戳, from 1970 0:0 0 seconds past how many banknotes [[email protected] ~]# date +%t #当前时间00:04:24[[email protecte D] ~]# date +%h%m%s #时分秒000554 [[[email protected] ~]# date +%h:%m:%s00:06:02[[email protected] ~]# cal #显示最近一 Monthly calendar July 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 27 2829 30 31[[email protected] ~]# date-d "-1 day" +%f #前一天2018 -07-13[[email protected] ~]# date-d "+1 Day" +%f #后 One day 2018-07-15[[email p rotected] ~]# date-d "+1 year" +%f #后一年2019 -07-14[[email protected] ~]# date-d "-1 year" +%f #前年2017-07-14 [[email protected] ~]# date-d "-1 month" +%f #上个月2018 -06-14[[email protected] ~]# date-d "+1 month" +%f # Next month 2018-08-14[[email protected] ~]# date-d "+1 Hour" +%t #后一个小时01:16:58[[email protected] ~]# date-d "-1 Hour "+%t #前一个小时23:17:04[[email protected] ~]# date-d @16032303211 #时间戳转成具体时间2478年 January 16 Sunday 09:33:31 cst[ [email protected] ~]# date +%s-d "2018-09-09 00:00:00" #具体时间转换成时间戳1536422400 [[email protected] ~]#
Iv. variables on the shell script

1. You should use a variable instead when you use a string in the script more frequently and the string length is long.
2. Variables that are commonly used when using conditional statements:
If [$a-gt 1];then ...; Fi
3. When referencing the result of a command, replace it with a variable:
n= ' WC-| 1.txt '
4, write and user interaction scripts, variables are also necessary
Read-p "Input a number:" N; echo $n #如果没写这个n and can be used directly with $reply
5, built-in variable $, $ $, $ ... $ $ represents the script itself, the first parameter, the $ A second .... $ #表示参数个数
6. Mathematical Operation a=1;b=2; c=$ (($a + $b)) or $[$a + $b]

Shell scripting introduction, structure and execution, variables, and date command usage

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.