How to simulate DOS commands in a Linux environment

Source: Internet
Author: User
Keywords Script can check whether run
You can use the flexibility inherent in the command shell of Linux to create scripting languages that help you simulate DOS commands in a Linux environment.    The specific approach is as follows.





If you are an IT support expert, you like the command operation 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 help you simulate DOS commands in a Linux environment.    The specific approach is as follows.





Shell Scripting Foundation





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 started with #!/bin/sh. This points the operating system to the program that interprets the script. Most systems have/bing/sh, as this is the standard shell http://www.aliyun.com/zixun/aggregation/6579.html "> Users use."   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 run automatically, but not 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 do the same thing with a similar% 1 or%2. The following is an example of using the command line argument:





#!/bin/sh





CP $ $




The
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 as./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 $ $





You can use the script above to copy all the files from the $ directory to $ $/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 expresses condition hypothesis becomes necessary. Shell scripting supports a number of options, from the comparison operator to the presence of the retrieved file. Basic if condition judgment options include:





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





NE Check whether two values are unequal





-lt Check value 1 is less than the value 2





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





GT Check whether the value 1 is greater than the value 2





GE Check whether the value 1 is greater than or equal to the value 2





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





-D checks whether a directory exists





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





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 does this:





#!/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 most mainstream Linux versions 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 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"





uses simple statements to help you understand the script and help you track where it went wrong.





Get Script





  With these basic scripting knowledge, you can easily translate the most common 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 approach.

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.