Initial __linux of Linux script writing

Source: Internet
Author: User
Tags file copy new set

If you are an IT support expert, you like the command of Windows very much, when you first use the Linux command line, you may soon find yourself confused. The DOS commands you've been familiar with for a long time don't exist in Linux. So you will find yourself faced with a daunting task: to learn and become familiar with a new set of commands.

As another option, you can use the inherent flexibility of the Linux command shell to create scripts that will help you simulate DOS commands in a Linux environment. The specific approach is as follows.

the foundation of Shell scripting

The Linux shell scripting is a way to automate multiple types of tasks, from nightly backups to simple command-line applications. Almost any program can be done through shell script. You can even do a simple conditional check inside a script. The basic format for Shell script is as follows:

#!/bin/sh
...
This is your order.
...

Note that the file starts with #!/bin/sh. This points the operating system to the program that interprets the script. Most systems have/bing/sh, because this is the standard shell used by the root user. You can use/bing/bash in most systems.

It is important to understand the scripting differences between each shell. Some shells, such as bash, support more commands than the standard shell. For most Linux versions, SH is actually bash.

Running commands from one script is very simple. It's like running DOS on a Windows system. For example, you can copy files like this:

#!/bin/sh
CP file1 File2
MV File2 File3
echo "Complete" > Complete.txt

The ability to complete a command without interaction is useful for tasks that are run automatically, but it is not helpful for users. The shell also provides a way to enter data into a running script. This allows the script to get data input from the user and then use that data in the program's operation. Argument in the command line refer to $ $. If you've ever created a batch file in DOS, you might be able to handle the same thing with a similar% 1 or%2. The following is an example of using the command line argument:

#!/bin/sh
CP $ $

The above script uses two command-line argument, one as the source of the copy, and the second as the destination of the copy. When you run the above script, you need to enter such a. /myscript file1 file2, where MyScript refers to the name of the script above. command-line options can also be passed in this way, such as:

#!/bin/sh
CP $ $

To recursively copy all of the files in the $ directory to $ $, you can use the script above:/copy SourceDir destdir. Option Plus-R can tell the system to make a recursive file copy.

shell scripting with conditions

Simple shell scripting is ideal for handling straightforward, variable-task tasks. For those who need a certain degree of decision-making, the If/then condition hypothesis becomes necessary. Shell scripting supports a number of options, from the comparison operator to the presence of the retrieved file. The basic if condition judgment options include:

-eq Check that two values are equal (for example, if [2 eq 5])

-ne Check if two values are not equal

-LT Check value 1 is less than the value 2

-le Check value 1 is less than or equal to value 2

-GT Check value 1 is greater than value 2

-ge Check value 1 is greater than or equal to value 2

-F checks whether a file exists (for example, [-F "filename"])

-D Check if a directory exists

Comparison operations are available for almost all major programs. The most frequently used is-----F, which we use to check the existence of a file before using it.

To create a simple script to simulate Windows commands

Now that you understand the basics, you can create script commands so that Windows users can use the same commands within the Linux system. Creating a mock map for your commonly used DOS commands is a very simple thing to do. For example, mapping the Linux CP command to the Windows Copy command is done in this way:

#!/bin/sh
If [-F "/usr/bin/mcopy"]
Then
Mcopy $ $
Else
CP $ $
Fi

The script takes advantage of mcopy (if it exists) because the command accepts Windows paths, such as: A:/file.txt. This command is in the most mainstream Linux version of the Mtool package. Once a script is created successfully, remember to use the chmod +x yourscriptname command to make it an executable file.

There are many ways to debug your script, but the easiest way to do this is to insert a simple echo statement into your script. Here is an example:

#!/bin/sh
echo "Marker 1"
If [-F "/usr/bin/mcopy"]
Then
echo "Marker 2"
Mcopy $ $
Else
echo "Marker 3"
CP $ $
Fi
echo "Marker 4"

Use simple statements to help you understand the script and help you keep track of where it went wrong.

Get Script

With these basic scripting knowledge, you can easily convert most of the commonly used Windows command lines into Linux-usable scripts. If you have a specific command-line option that you want to map, look at the Linux man pages, which can help you find the right way to  

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.