The use of DAT, calculator, built-in variables in shell scripts

Source: Internet
Author: User



What is a shell script. First, it is a script and cannot be used as a formal programming language. Because it's running in the shell of Linux, it's called a shell script. To be blunt, a shell script is a collection of commands. For example, I want to do this:



1) enter into the/tmp/directory;



2) List all filenames in the current directory;



3) Copy all current files to the/root/directory;



4) Delete all files in the current directory.



A simple 4 step in the shell window requires you to knock 4 commands, press 4 times to enter. Isn't that a lot of trouble? So it's a matter of recording all the operations into a document and then calling the commands in the document so that one step can be done. In fact, this document is a shell script, but this shell script has its special format.



The shell script can help us to manage the server very conveniently, because we can specify a task schedule to execute a shell script to implement the requirements we want. For example, with the convenience of e-mail, we can deploy monitoring shell script on our Linux server, such as network card traffic is abnormal or the server Web server stop can send a message to the administrator, and send to the administrator an alarm text message so that we can know the server problem in a timely manner.



Any custom script recommendations are placed in the/usr/local/sbin/directory, which is designed to better manage the document, and then the administrator who takes over later knows where the custom script is placed for easy maintenance.


Basic structure of shell scripts and how to execute them


First shell script

[[email protected] ~] # cd / usr / local / sbin /
[[email protected] sbin] # vim first.sh
#! / bin / bash

## This is my first shell script.
## 2017.09.02.

date
echo "Hello world!"
Shell scripts are usually suffixed with the name .sh. This is not to say that the script cannot be executed without .sh. So, in the future, if you find a file with a .sh extension, it may be a shell script. The first line in the shell script
Start with "#! / Bin / bash", which means that the file uses bash syntax. If you do not set this line, although your shell script can also be executed, but this does not meet the specifications. # Indicates a comment, followed by some relevant comment content of the script, author and creation date or version, etc. Of course these notes are not necessary, but it is recommended to write them. . Because with the gradual transition of working hours, you will also write more and more shell scripts. If one day you look back at a script you have written, you may forget what the script is used for and when written. So it is necessary to write notes. In addition, the system administrator is not the only one. If other administrators look at your script, he is not very depressed. Now it's time to run this script:
[[email protected] sbin] # sh first.sh
Saturday, September 2, 2017 18:58:02 CST
Hello world!
In fact, there is another way to execute shell scripts:

In fact, there is another way to execute shell scripts:

[[email protected] sbin] # ./first.sh
-bash: ./first.sh: insufficient permissions
[[email protected] sbin] # chmod + x first.sh
[[email protected] sbin] # ./first.sh
Saturday, September 02, 2017 18:58:51 CST
Hello world!
To use this method to run a shell script, the premise is that the script itself has execute permissions, so you need to add a ‘x’ permission to the script. In addition, when using the sh command to execute a shell script, you can add the -x option to view the execution process of this script, which is helpful for us to debug what is wrong with this script:

[[email protected] sbin] # sh -x first.sh
+ date
Saturday, September 02, 2017 20:00:11 CST
+ echo ‘Hello world!’
Hello world!
Command: date, usage in shell
[[email protected] sbin] # date + "% Y-% m-% d% H:% M:% S"
2017-09-2 19:41:01
The most common uses of date in scripts:

data +% Y print year in four-digit format

date +% y prints the year in two-digit format

date +% m month

date +% d date

date +% H hours

date +% M minutes

date +% S seconds

date +% w weekday, if the result shows 0, it means Sunday

Sometimes the date one day ago is used in the script:

[[email protected] sbin] # date -d "-1 day" +% d
1
Or an hour ago:

[[email protected] sbin] # date -d "-1 hour" +% H
18
Even 1 minute ago:

[[email protected] sbin] # date -d "-1 min" +% M
50
Variables in shell scripts
If you write a shell script up to 1000 lines long, and a certain command or path appears hundreds of times in the script. Suddenly you think the path is wrong and want to change it, wouldn't it have to be changed hundreds of times? You can use the batch replace command, but it is also very troublesome, and the script looks a lot bloated. The role of variables is to solve this problem.

[[email protected] sbin] # cat variable.sh
#! / bin / bash

## In this script we will use variables.
## Writen by Aming 2017-09-2.

d = `date +% H:% M:% S`
echo "The script begin at $ d."
echo "Now we ‘ll sleep 2 seconds."
sleep 2
d1 = `date +% H:% M:% S`
echo "The script end at $ d1."
‘D’ and ‘d1’ appear as variables in the script. The format of the variable is defined as variable name = value of the variable. When referring to the variable in the script, you need to add the ‘$’ symbol. Let ’s see the script execution result:
[[email protected] sbin] # sh variable.sh
The script begin at 20:16:57.
Now we ‘ll sleep 2 seconds.
The script end at 20:16:59.
Below we use the shell to calculate the sum of two numbers:

[[email protected] sbin] # cat sum.sh
#! / bin / bash

## For get the sum of tow numbers.
## 2017.09.02.

a = 1
b = 2
sum = $ [$ a + $ b]

echo "$ a + $ b = $ sum"
Mathematical calculations should be enclosed in [] and a ‘$’ script should be carried out. The result of the script is:

[[email protected] sbin] # sh sum.sh
1 + 2 = 3
Shell scripts can also interact with users:

[[email protected] sbin] # cat read.sh
#! / bin / bash

## Using ‘read’ in shell script.
## 2017.09.02.

read -p "Please input a number:" x
read -p "Please input another number:" y
sum = $ [$ x + $ y]
echo "The sum of the two numbers is: $ sum"
The read command is used in such a place to interact with the user, using the string entered by the user as a variable value. The script execution process is as follows:

[[email protected] sbin] # sh read.sh
Please input a number: 2
Please input another number: 10
The sum of the two numbers is: 12
Sometimes we will use this command /etc/init.d/iptables restart. The /etc/init.d/iptables file in front of it is actually a shell script, why can it be followed by a "restart"? This involves a shell script Default variables. In fact, the shell script can follow the parameters when it is executed, and it can also follow multiple.

[[email protected] sbin] # cat option.sh
#! / bin / bash

sum = $ [$ 1 + $ 2]
echo "sum = $ sum"
The execution result is:

[[email protected] sbin] # sh -x option.sh 1 2
+ sum = 3
+ echo sum = 3
sum = 3
$ 1 and $ 2, which are actually the default variables of the shell script, where the value of $ 1 is the 1 entered during execution, and the value of $ 2 is the $ 2 entered during execution, of course, the default variable of a shell script is no limit Yes, there is another $ 0, but it represents the name of the script itself. May wish to modify the script

[[email protected] sbin] # cat option.sh
#! / bin / bash

echo "$ 1 $ 2 $ 0"
Results of the:

[[email protected] sbin] # sh option.sh 1 2
1 2 option.sh

Usage of dat, calculator, and built-in 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.