Using shell programming to implement DOS-style Linux command line _unix Linux

Source: Internet
Author: User

If you are an IT person accustomed to a Windows command prompt, you will feel overwhelmed when you first use the Linux command line. You are familiar with DOS commands that are basically non-existent in Linux. In front of you is a lot of orders to memorize.

One alternative is to use a powerful Linux shell command to write shell scripts that allow you to use DOS commands under Linux as well. Let me tell you what to do.

Shell Scripting Basics
From scheduled backups to simple commands, Linux shell scripts can perform a variety of functions. Almost all programs can be run with shell scripts. You can even include some simple conditional selections in your script. The basic format of the shell script is as follows:

#!/bin/sh

...

Your commands here

...

Note that the file starts with #!/bin/sh. This statement tells the operating system where the program is used to interpret the script. Most systems will have a/bin/sh directory, because the directory contains the standard shell program of the root user. In most systems you can also 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 a command from a script is very simple, much like running a command at a Windows DOS prompt. For example, you can copy a file by using the following statement:

#!/bin/sh

CP file1 File2

MV File2 File3

echo "Complete" > Complete.txt

Automating commands is useful for tasks that do not require human intervention, but is less useful for general users. To do this, the shell script allows the user to enter command-line arguments during execution and then runs the command with the input parameters. The input parameters in the script are expressed in $ $. If you have written a DOS batch file, you will find that there are similar functions in the batch file, except that it uses% 1,%2, etc. to represent the input parameters. Here's how to use command-line arguments, for example:



#!/bin/sh

CP $ $

The previous script accepts 2 command-line arguments, the first is the original file to be copied, and the second is the target file for the copy. The command format to run the script is:./myscript file1 File2,myscript represents the script file name. command-line options can also be passed in this way, such as:

#!/bin/sh

CP $ $

Typing the./copy–r sourcedir destdir Command executes the previous script to recursively copy all the files in the $ directory to the $ directory. Option-r enables the CP command to recursively copy all files.

Shell script with conditional selection
Simple shell scripts are generally competent for tasks that do not contain variables. However, in the implementation of a number of decision-making tasks, it is necessary to include the if/then of the conditional judgment. Shell scripting supports such operations, including comparing operations, determining whether a file exists, and so on. The basic if Condition command options are:

-eq-compares two arguments for equality (for example, if [2–eq 5])
-ne-compares two arguments not equal
-lt-parameter 1 is less than parameter 2
-le-parameter 1 is less than or equal to parameter 2
-gt-parameter 1 is greater than parameter 2 br>-ge-parameter 1 is greater than or equal to parameter 2
-f-Check that a file exists (for example, if [-F "filename"])
-D check that the directory exists
almost all judgments can be implemented with these comparison operators. The common-f command option in a script checks to see if it exists before executing a file.

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.