If you are an IT support expert and you like Windows Command operations very much, when you first use Linux Command lines, you may soon find yourself confused. The DOS commands you have been familiar with for a long time do not exist in Linux. So you will find yourself facing a terrible task: Re-learning and getting familiar with a new set of commands.
As another option, you can use the inherent flexibility of Linux Command Shell to create scripts to help you simulate doscommands in Linux. The procedure is as follows.
Basics of shell Scripting
Linux Shell scripting is a method for automatically completing multiple types of tasks, from night backup to simple command line applications. Almost anyProgramCan be completed through shell script. You can even perform simple condition checks within the script. The basic format of shell script is as follows:
#! /Bin/sh
...
Here is your command
...
Note that the file is #! /Bin/sh. This directs the operating system to the program that explains the script. Most systems include/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 script 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 a script is very simple. It is like running dos in windows. For example, you can copy an object like this:
#! /Bin/sh
CP file1 file2
MV file2 file3
Echo "complete"> complete.txt
The ability to complete a command without interaction is very useful for automatically running tasks, but not so helpful for users. Shell also provides a way to input data to a running script. This allows the script to obtain data input from the user and then use the data in the program running. The argument in the command line refers to $1 to $9. If you have created a batch file in DOS, you may use % 1 and % 2 to handle the same thing. The following is an example of using the command line argument:
#! /Bin/sh
CP $1 $2
The above script uses two command lines argument and uses one of them as the source of the copy, and the other as the destination of the copy. When running the above script, you need to enter such as./myscript file1 file2. Here myscript refers to the name of the above script. The command line option can also be passed in this way, for example:
#! /Bin/sh
CP $1 $2 $3
To recursively copy all the files in the $2 Directory to $3, you can use the above script:/copy sourcedir destdir. Option $1 plus-R can tell the system to perform recursive file copying.
Conditional shell Scripting
Simple shell scripting is suitable for handling straightforward tasks without variables. For those jobs that require a certain degree of decision-making, the IF/then condition assumption is necessary. Shell scripting supports many options, from comparison operator operations to file retrieval. The basic options for determining the if condition include:
-EQ: Check whether two values are equal (for example, if [2 EQ 5])
-Ne: Check whether two values are not equal.
-Lt check whether value 1 is smaller than value 2
-Le: Check whether value 1 is less than or equal to value 2.
-GT: Check whether value 1 is greater than value 2
-Check if value 1 is greater than or equal to value 2 of Ge
-F: Check whether a file exists (for example, [-F "file name"])
-D. check whether a directory exists.
Almost all major programs can be compared. The most frequently used is-F. We use it to check its existence before using a file.
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 in Linux. It is very easy to create a simulated ing for your commonly used DOS commands. For example, you can map the Linux CP command to the Windows Copy command as follows:
#! /Bin/sh
If [-F "/usr/bin/mcopy"]
Then
Mcopy $1 $2
Else
CP $1 $2
Fi
This script uses mcopy (if it exists) because the command accepts the Windows path, for example, a:/file.txt. This command is used in most mainstream Linux mtool packages. Once a script is successfully created, use the CHMOD + x yourscriptname command to make it an executable file.
There are many ways to debug your script, but the simplest way is to insert a simple echo statement in your script. The following is an example:
#! /Bin/sh
Echo "marker 1"
If [-F "/usr/bin/mcopy"]
Then
Echo "marker 2"
Mcopy $1 $2
Else
Echo "marker 3"
CP $1 $2
Fi
Echo "Marker 4"
Use a simple statement to help you understand the script and help you track where it goes wrong.
Obtain scripts
With these BASIC script knowledge, you can easily convert most common Windows command lines into available Linux scripts. If you want to map a specific command line option to Linux man pages, you can find a proper method.