68.shell Script Introduction, shell script structure and execution, date command usage, variables in shell scripts

Source: Internet
Author: User

First, what is the shell?
  • The shell is a scripting language aming_linux blog.lishiming.net
  • You can use logical judgments, loops, and other syntax
  • Functions can be custom-defined
  • The shell is a collection of system commands
  • Shell scripts can be automated operations, can greatly increase our operational efficiency two, shell script structure and execution methods
  • Need to add #!/bin/bash at the beginning
  • 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 methods of executing
  • chmod +x 1.sh;./1.sh
  • Bash 1.sh
  • View the script execution process bash-x 1.sh
  • View script syntax error bash-n 1.sh Third, usage of the date command
      date +%y Four-digit year date +%y two-digit year date +%m                 Month date +%m minute date +%d           05/30/18 format Displays the current day date +%d date (number of days) dates +%y-%m-%d Month date date +%y-%m-%d month Date +%h:%m:%s               Time Division seconds Date +%t time +%s date??          Timestamp date-d @1504620492date-d "+1day" one day after date-d "-1 day" date-d "-1 month ago date-d"-1 min "                 A minute ago date-d "+1hour" an hour later date-d "+1year" a year later date +%w week several date +%w Weeks of the week Cal display Calendar  




%H hours, 24-hour (00~23)
%I Hours, 12-hour (01~12)
%k hours, 24-hour (0~23)
%l hours, 12-hour (1~12)
%M min (00~59)
%p Show AM or PM
%r display time, 12-hour system (HH:MM:SS%p)
%s from January 1, 1970 00:00:00 to the number of seconds currently experienced
%s Display seconds (00~59)
%T display time, 24-hour system (HH:MM:SS)
%x shows the format of Time (%h:%m:%s)
%Z Display time zone, date field (CST)
%a week abbreviation (SUN~SAT)
Full name of%A week (Sunday~saturday)
%h,%b month abbreviation (JAN~DEC)
Full name of%B month (January~december)
%c Date and time (Tue Nov 20 14:12:58 2012)
%d the day of the one month (01~31)
%x,%d Date (MM/DD/YY)
%j the first day of the Year (001~366)
%m Month (01~12)
%w Day of the week one (0 for Sunday)
%W Week of the year (00~53, Monday for first day)
The last two digits of the%y year (1999 is 99)

Iv. 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
    • When using conditional statements, variables are often used?? If [$a-gt 1]; Then ...; Fi
    • When referencing the result of a command, replace it with a variable?? n=wc -l 1.txt
    • When writing scripts for user interaction, variables are also necessary?? Read-p "Input a number:" N; echo $n?? If you don't write this n, you can use $reply directly.
    • Built-in variables, $ $, $ ...? ? $ A represents the script itself, the first parameter, the second ...? ?? ? $ #表示参数个数
    • Mathematical Operation a=1;b=2; c=$ (($a + $b)) or $[$a + $b]

68.shell Script Introduction, shell script structure and execution, date command usage, variables in shell scripts

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.