Simple shell programming in Linux

Source: Internet
Author: User
Simple shell programming in Linux 
Transfer from: dynamic network Production Guide www.knowsky.com

 
Basics of shell script writing

From scheduled backup to simple command execution, Linux shell scripts can execute various functions. Almost all programs can run with shell scripts. The script can even contain some simple conditions. The basic format of shell scripts is as follows:

#! /Bin/sh

...

Your commands here

...

Pay attention to #! Start with/bin/sh. This statement tells the operating system the location of the script program. Most systems have the/bin/sh directory because it contains the standard shell program of the root user. In most systems, you can specify the/bin/bash directory. The scripts for each shell are different. Some shells, such as Bash, support more commands than the standard shell. In most Linux versions, SH is actually bash. Running commands from a script is very simple, like running commands at a Windows DOS prompt. For example, you can use the following statement to copy an object:

#! /Bin/sh

CP file1 file2

MV file2 file3

Echo "complete"> complete.txt

Automatic Command Execution is useful for tasks that do not require manual intervention, but not for general users. Therefore, the shell script allows you to enter command line parameters during execution, and then run the command using the input parameters. The input parameters in the script are represented by $1 to $9. If you have written a DOS batch file, you will find similar features in the batch file, but it uses % 1, % 2, and so on to represent the input parameters. The following example shows how to use command line parameters:

 
#! /Bin/sh

CP $1 $2 // input two parameters

The preceding script accepts two command line parameters. The first is the original file to be copied, and the second is the target file to be copied. The command format for running the script is./myscript file1 file2, and myscript represents the script file name. The command line option can also be passed in this way, for example:

#! /Bin/sh

CP $1 $2 $3

Run the command in the form of./copy-r sourcedir destdir to execute the preceding script, and recursively copy all the files in the $2 Directory to the $3 directory. When option $1 is-R, the CP command can recursively copy all files.

Shell script with conditional Selection
Simple shell scripts for tasks without variables are generally competent. However, when executing some decision-making tasks, it is necessary to include the IF/then condition judgment. Shell script programming supports such operations, including comparison operations and determining whether a file exists. The basic if condition command options include:

-Eq-compare whether two parameters are equal (for example, if [2-EQ 5])
-Ne-compare whether two parameters are not equal
-Lt-parameter 1: whether it is smaller than parameter 2
-Le-parameter 1: whether it is less than or equal to parameter 2
-GT-whether parameter 1 is greater than parameter 2
-Whether Ge-parameter 1 is greater than or equal to parameter 2
-F-check whether a file exists (for example, if [-F "FILENAME"])
-D-check whether the directory exists
Almost all judgments can be implemented using these comparison operators. In the script, the common-F Command Option checks whether a file exists before executing it.

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.