Windows commands on Linux command-line

This is an article on Windows commands on Linux command-line in Linux.

If you're an IT support professional who's comfortable with working at the command prompt in Windows, you might quickly find yourself lost when you first open a command line in Linux. Most of the familiar DOS commands you grew up with don't exist in Linux. So you find yourself facing the daunting task of learning and remembering a whole new set of commands.

As an alternative, you can take advantage of the flexibility inherent in the Linux command shell and create scripts that allow you to emulate DOS commands in a Linux environment. Here's how.

Basics of shell scripting
Linux shell scripting is a method for automating many types of tasks, ranging from nightly backups to simple command line utilities. Almost any program can be run with a shell script. You can even perform simple conditions checking inside of the script. The basic format of a shell script is:

Code:

#!/bin/sh
...
Your commands here
...

Notice that the file starts with #!/bin/sh. This directs the operating system to the program to use for interpreting your script. Most systems will have /bin/sh, because this is the standard shell used by root users. You can also specify /bin/bash on most systems.

It's important to note the differences between scripts in each shell. Certain shells, such as bash, support many more commands than a standard shell. For most Linux distributions, sh is actually bash.

Running commands from a script is quite simple. It's just like running commands from a regular DOS prompt in Windows. For example, you can copy files by typing:

Being able to run commands without any interaction can be useful for automated tasks but is far less useful for a user. The shell also provides a way to input data into a running script with command line arguments. This allows for scripts that take input from a user and use that data to run the programs within. Arguments from the command line are referred to by $1 through $9. If you've ever created batch files in DOS, you're probably familiar with doing the same thing using %1, %2, etc. An example of command line argument usage is:

Code:

#!/bin/sh
cp $1 $2

The script above takes two command line arguments and uses the first as the source to copy and the second as the target to copy to. To run the above script, you would type something like ./myscript file1 file2, where myscript is the name of the above script. Command line options can also be passed with this method, such as:

Code:

#!/bin/sh
cp $1 $2 $3

To recursively copy all files within the $2 directory to $3 you could call the above script this way: ./copy –r sourcedir destdir. Option $1 would be the -r causing the cp command to recursively copy all files.

Shell scripting with conditions

Simple shell scripts are great for doing straightforward tasks that never have variations. For tasks that require some level of decision making, if/then conditions must be brought in. Shell scripting supports a number of options, ranging from comparison operators to checking for file existence. Basic if conditions-checking options include:

* -eq—Checks to see if arguments are equal (for example, if [ 2 –eq 5 ] )
* -ne—Checks for inequality of arguments
* -lt—Checks if argument 1 is less than argument 2
* -le—Checks if argument 1 is less than or equal to argument 2
* -gt—Checks if argument 1 is greater than argument 2
* -ge—Checks if argument 1 is greater than or equal to argument 2
* -f—Checks if a file exists (for example, if [ -f "filename" ] )
* -d—Checks if a directory exists

Almost any major procedure can be performed using these comparison operators. The main option used in the scripts is -f for checking for file existence before attempting to execute it in the current case.

Now that you know the basics, you can create scripting commands so that Windows users can type the same commands on their Linux systems. It's a simple task to create mappings that mimic your most commonly used DOS utilities. For example, mapping the Linux cp command to the Windows copy command would look something like this:

The script takes advantage of mcopy if it exists because this command will accept Windows paths, such as a:\file.txt. This utility can be found in the mtools package included with most major Linux distributions. Once a script has been created, make sure to make it executable by performing the chmod +x YourScriptName command on the file.

There are many methods for debugging your script, but one of the easiest is to insert simple echo statements in your script. Here's an example:

Using simple statements allows you to see what path the script is taking in the if statement and to track if and where it breaks.

Get scripting

With this basic scripting knowledge you should be able to easily mimic almost every major Windows command line program. If there are certain command line options you wish to mimic, check the Linux main pages to determine the appropriate switches for use in your script.