If you are an IT support expert, you like the command operation of Windows, when you use Linux command line, you may soon find yourself confused. The DOS command you are familiar with will not exist in Linux for a long time. So you will find that you will face a terrible task: re-learn and become familiar with a new set of orders.
As another selection, you can use the inherent flexibility of Linux Command shell, create scripts to help you simulate the DOS command in the Linux environment. The specific practices are as follows.
SHELL SCRIPTING foundation
Linux's shell scripting is an automatic way to complete multiple types of tasks, backup from night to simple command line applications. Almost any program can be done by Shell Script. You can even complete simple conditions inside the script. The basic format of Shell Script is as follows:
#! / bin / sh ... here is your order ...
Note that the file is started in #! / Bin / sh. This points the operating system to the program explaining the script. Most of the systems have / bing / sh, because this is the standard shell used by root users. You can use / bing / bash in most systems.
It is important to understand the difference between the script between each shell. Some shells, such as Bash, support more commands than the standard shell. For most Linux versions, the SH is actually BASH.
Run command from a script is very simple. It is like running DOS in a Windows system. For example, you can copy the file like this:
#! / bin / shcp file1 file2mv file2 file3echo "complete"> Complete.txt
It is very useful to complete a command without interaction, but it is very useful for the user, but it is not so helpful for users. The Shell also provides a way to enter data from a running script. This allows scripts to get data input from users and then use them in program operation. The argument in the command line refers to $ 1 to $ 9. If you have created a batch file in DOS, you might have the same thing for using similar% 1,% 2. Below is an example of using the command line argument:
#! / bin / SHCP $ 1 $ 2
The above script uses two command line Argument and one of the sources of the copy, while the second as a copy of the destination. When running on this script, you need to enter such as ./myScript file1 file2, MyScript here is the name of the above script. Command line options can also be passed in this way, such as:
#! / bin / SHCP $ 1 $ 2 $ 3
Copy all the files in the $ 2 directory to $ 3 in order to recursively, you can use the above script: / copy sourcedir destdir. Option $ 1 Plus -R can tell the system to perform recutable file copies.
Shell scripting with conditions
Simple shell scripting is very suitable for processing straightforward tasks without variables. For those who need a certain degree decision-making, IF / THEN condition assumes become a must. Shell scripting supports many options that operate from the comparator to the existence of the retrieval file. Basic IF Condition Judgment Options include:
-EQ checks if a two values are equal (for example, if [2 Eq 5])
-ne checks if the two values are not equal
-lt check value 1 is less than value 2
-LE check value 1 is less than or equal to 2
-gt check value 1 is greater than value 2
-ge check value 1 is greater than equals value 2
-f checks if a file exists (for example, [- f "file name"])
-d checks if a directory exists
Almost all major programs can use comparison operations. The most frequently used -f, we use it before using a file, check its existence. Create a simple script to simulate the windows command
Now, you know the foundation, you can create a scripting command so that Windows users can use the same commands within the Linux system. Creating an analog mapping for your commonly used DOS command is a very simple thing. For example, the Linux CP command is mapped to the Windows Copy command is this:
#! / bin / shif [-f "/ usr / bin / mcopy"] Thenmcopy $ 1 $ 2ELSECP $ 1 $ 2FI
This script uses Mcopy (if it exists) because the command accepts the Windows path, such as: A: /FILE.TXT. This command is in the most mainstream Linux version of the MTOOL package. Once a script is successfully created, remember to use the chmod x YourScriptName command to make it executable.
There are many ways to debug your script, but the easiest way is to insert a simple Echo statement in your script. Below is an example:
#! / bin / shecho "Marker 1" IF [-f "/ usr / bin / mcopy"] Thenecho "Marker 2" McOpy $ 1 $ 2ELSEECHO "Marker 3" CP $ 1 $ 2FIECHO "Marker 4"
Use simple statements to help you understand this script and help you track it there.
Get a script
With these basic scripting knowledge, you can easily convert most commonly used Windows command lines to Linux available scripts. If you have a specific command line option, you want to map, see Linux Man Pages, which helps you find your right way