Shell Programming (i)

Source: Internet
Author: User
Tags time and date

[TOC]

Shell programming One, 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;

Explains the importance of the shell script in the work, the shell script is a collection of commands, is an important part of automated operations

Second, shell script structure and execution

Follow the steps below to learn how to write a shell script in one step

2.1 Creating a Shell Script lab environment
[[email protected] ~]# mkdir shell[[email protected] ~]# cd shell/[[email protected] shell]# ls[[email protected] shell]# vi 01.sh

Shell scripts are usually. SH is the suffix name, but not to say no. SH's script can't be executed, just a habit for everyone, so if you send a new file with a. sh suffix, it might be a shell script.

In this case, the script file 01.sh, after entering the editing state, the first line to start with #!/bin/bash, indicating that the file is using the bash syntax. And/bin/bash is the parser command path for bash.

2.2 Execute the script #sh. 01.sh
[[email protected] shell]# sh 01.sh123 22:43:14 up 25 min,  1 user,  load average: 0.00, 0.01, 0.01USER     TTY      FROM             [email protected]   IDLE   JCPU   PCPU WHATroot     pts/0    192.168.72.1     22:26    2.00s  0.08s  0.00s w01.sh

This line of #!/bin/bash is deleted, and can be executed after running the command, but this does not conform to the specification.

2.3 Another way to execute the script #./01.sh

Use this method to run the shell script is the premise that the script itself has execute permissions, need to give the script x permission

[[email protected] shell]# chmod +x 01.sh[[email protected] shell]# ./01.sh123 23:18:34 up  1:01,  1 user,  load average: 0.00, 0.01, 0.03USER     TTY      FROM             [email protected]   IDLE   JCPU   PCPU WHATroot     pts/0    192.168.72.1     22:26    2.00s  0.11s  0.01s w01.sh
2.4 View interpreting Paths/bin/bash and/bin/sh

[[email protected] shell]#  bash 01.sh123 23:37:15 up  1:19,  1 user,  load average: 0.00, 0.01, 0.03USER     TTY      FROM             [email protected]   IDLE   JCPU   PCPU WHATroot     pts/0    192.168.72.1     22:26    3.00s  0.20s  0.01s w01.sh

There are three ways to execute a shell script, but the success of the native validation does not mean that the other machine virtual machines are successful, and subsequent tests

2.5 Examples of how scripts are edited, comments, etc.
[[email protected] shell]# vi /etc/init.d/network#! /bin/bash## network       Bring up/down networking## chkconfig: 2345 10 90# description: Activates/Deactivates all network interfaces configured to #              start at boot time.#

2.6 sh-x View shell script execution process for easy follow-up debugging
[[email protected] shell]# sh -x 01.sh+ echo 123123+ w 23:40:21 up  1:22,  1 user,  load average: 0.00, 0.01, 0.03USER     TTY      FROM             [email protected]   IDLE   JCPU   PCPU WHATroot     pts/0    192.168.72.1     22:26    5.00s  0.22s  0.01s w+ ls01.sh[[email protected] shell]# bash -x 01.sh+ echo 123123+ w 23:40:29 up  1:23,  1 user,  load average: 0.00, 0.01, 0.03USER     TTY      FROM             [email protected]   IDLE   JCPU   PCPU WHATroot     pts/0    192.168.72.1     22:26    5.00s  0.21s  0.00s w+ ls01.sh
2.7 Sh-n detect if a shell script has syntax errors

Iii. use of the date command

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

[[email protected] ~]# Date//Current date April 17, 2018 Tuesday 22:06:22 cst[[email protected] ~]# cal//Current calendar April 2018 Day 1,234,561 2 3 4 5 6 7 8 9 ten-1415-2122-2829 30[[email protec Ted] ~]# date +%y//year 2018[[email protected] ~]# date +%y//year abbreviation 18[[email protected] ~]# date +%m//month 04[[email& Nbsp;protected] ~]# Date +%y%m%d//month day 20180417[[email protected] ~]# date +%f2018-04-17[[email protected] ~] # date +%h/hour 22[[email protected] ~]# date +%m//min 23[[email protected] ~]# Date +%s//Sec 10[[EMAIL PR Otected] ~]# Date +%s//timestamp, the number of seconds elapsed since January 1, 1970 (Midnight of Utc/gmt), regardless of leap seconds 1523975099[[email protected] ~]# date +%t// Full display of time 22:27:04[[email protected] ~]# date "+%y-%m-%d%h:%m:%s%w"//month day, cent seconds, week 2018-04-17 22:20:50 2[[email  Protected] ~]# date-d "-1 day" April 16, 2018 Monday 22:31:20 cst[[email protected] ~]# date-d "-1 day" +%f2018-04-16[[e Mail protected] ~]# date-d"-1 month" +%f2018-03-17[[email protected] ~]# date-d "-1 year" +%f2017-04-17[[email protected] ~]# date-d "-1 Hour "+%t21:44:30[[email protected] ~]# date +%s-d" 2018-04-17 22:46 "//date converted to timestamp 1523976360
Iv. variables in shell scripts

Shell Programming (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.