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:
#!/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:
#!/bin/sh
cp file1 file2
mv file2 file3
echo "complete" > complete.txt
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:
#!/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:
#!/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:
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.
Creating simple scripts to emulate Windows commands
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:
#!/bin/sh
if [ -f "/usr/bin/mcopy" ]
then
mcopy $1 $2
else
cp $1 $2
fi
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:
#!/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"
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.
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 man pages to determine the appropriate switches for use in your script.
TechRepublic is the online community and information resource for all IT professionals, from support staff to executives. We offer in-depth technical articles written for IT professionals by IT professionals. In addition to articles on everything from Windows to e-mail to firewalls, we offer IT industry analysis, downloads, management tips, discussion forums, and e-newsletters.
©2004 TechRepublic, Inc.



2%
4%






Writing scripts to automate tasks is an extremely good idea. It reduces the probability of error especially as taks become more complex.
Writing scripts to emulate a DOS environment is an extremely bad idea. The problem is that you will tend to think of it as a DOS environment but it is not an dthe scripts will not emulate anything but the most basic behaviour. The example copy scripts for only take a fixed number of parameters and silently drop any others. If a DOS expert put a series of switchs on the command they will silently be ignored this could be very misleading. The approach of disguising the underlying system is dangerous, far better to learn the underlying system and use scripts to automate whatever you do repeatedly. The learning curve for this is actually quite small and once done productivity and quality is much higher than under DOS/windows.