Introduction to shell scripting for Linux, shell script structure and execution, date command usage, variables in shell scripts

Source: Internet
Author: User

Shell Script Introduction
    • Shell is a scripting language
    • You can use logical judgments, loops, and other syntax
    • Functions can be custom-defined
    • The shell is a collection of system commands
    • Shell scripts enable automated operations, which can greatly increase our operational efficiency
Shell script structure and execution
    • 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 execution, plus execute permission chmod +x 1.sh;1. ./1.sh2. /root/shell/01.sh (absolute path execution)
    • Execute Script Bash 1.sh
    • View the script execution process bash-x 1.sh
    • See if the script is syntactically incorrect bash-n 1.sh

Practice a script to do the testing:

[[email protected] ~]# mkdir shell[[email protected] ~]# cd shell/[[email protected] shell]# ls[[email& Nbsp;protected] shell]# vi 01.sh[[email protected] shell]# bash 01.sh 123 10:57:41 up 9 min, 1 user, load average: 0.00, 0.13, 0.13USER TTY from [email protected] IDLE jcpu PCPU whatroot pts/0 172.1 6.111.1 10:49 5.00s 0.03s 0.01s w01.sh[[email protected] shell]# cat 01.sh #!/bin/bash//First line this if you do not write on this side, Line script when added/bin/bsah can echo "123" wls[[email protected] shell]# chmod a+x 01.sh [[email protected] shell]#./01.sh   123 10:58:12 up 9 min, 1 user, load average:0.00, 0.11, 0.12USER TTY from [email protected] IDLE jcpu PCPU whatroot pts/0 172.16.111.1 10:49 4.00s 0.02s 0.00s/bin/bash./01.sh01.sh[[email&nbsp ;p rotected] shell]# ls-l/bin/bash-rwxr-xr-x. 1 root root 960392 August 3 2016/bin/bash[[email protected] shell]# ls-l/bin/shlrwxrwxrwx. 1 root RooT 4 October 05:04/bin/sh-bash[[email protected] shell]#/bin/bash 01.sh 123 11:02:12 up min, 1 user, load     average:0.00, 0.05, 0.10USER TTY from [email protected] IDLE jcpu PCPU whatroot pts/0 172.16.111.1 10:49 4.00s 0.03s 0.00s w01.sh# View script execution process [[email protected] shell]# bash-x 01.sh + echo 1231 23+ W 11:12:04 up min, 1 user, load average:0.00, 0.03, 0.07USER TTY from [email protected ] IDLE jcpu PCPU whatroot pts/0 172.16.111.1 10:49 4.00s 0.04s 0.01s w+ ls01.sh# Check for script syntax error [[EMAIL&NBSP ;p rotected] shell]# bash-n 01.sh [[email protected] shell]#/root/shell/01.sh 123 11:23:45 up min, 1 user, lo Ad average:0.05, 0.05, 0.06USER TTY from [email protected] IDLE jcpu PCPU whatroot PT s/0 172.16.111.1 10:49 1.00s 0.05s 0.00s/bin/bash/root/shell/01.sh01.sh
Use of the date command
    • Basic usage List
 date??+%Y-%m-%d, date +%y-%m-%d 年月日 date??+%H:%M:%S = date +%T 时间 date +%s??时间戳 date -d @1504620492 date -d "+1day"  一天后 date -d "-1 day"  一天前 date -d "-1 month" 一月前 date -d "-1 min"  一分钟前 date +%w, date +%W 星期
    • Usage examples
[[email protected]]# date2018 February 03 Saturday 13:41:51 cst# Display the date command into English [[email protected]]# Lang=en[[email  protected]]# Datesat Feb 3 13:45:14 CST 2018# year [[email protected] ~]# date +%y2018# Two-bit year [[email protected ] ~]# date +%y18# month [[email protected] ~]# date +%m02# minutes [[email protected] ~]# date +%m47# dates [[email  Protected] ~]# Date +%d03[[email protected] ~]# date +%d02/03/18[[email protected] ~]# date +%y%m%d20180203[[ Email protected] ~]# date +%f2018-02-03# hour [[email protected] ~]# date +%h13# second [[email protected] ~]# Date +%s41# timestamp (distance 19700101 past number of seconds) [[email protected] ~]# date +%s1517636984[[email protected] ~]# date-d @ 1517637599Sat Feb 3 13:59:59 CST 2018[[email protected] ~]# date +%s-d "2018-02-03 13:59:59" 1517637599# time [[EMAIL&NB Sp;protected] ~]# date +%t13:52:05[[email protected] ~]# date +%h:%m:%s13:53:17# English month [[email protected] ~]# Date +%hfeb# Day of the week [[email protected] ~]# date +%w6# the week of the year [EMail protected] ~]# date +%w05# display calendar [[email protected] ~]# cal February 2018 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 1011 1718 (2425) 28[[email protected] ~]# date-d "+1 da Y "Sun Feb 4 14:02:19 CST 2018[[email protected] ~]# date-d"-1 day "Fri Feb 2 13:57:49 CST 2018[[email protect Ed] ~]# date-d "-1 day" +%f2018-02-02[[email protected "~]# date-d"-1 month "+%f2018-01-03[[email protected] ~]# date-d "-1 year" +%f2017-02-03[[email protected "~]# date-d"-1 Hour "+%t12:59:07
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]

Introduction to shell scripting for Linux, 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.