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:

  • -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.

Do you need help with Linux? Gain advice from Builder AU forums

Related links

Comments

1

KIRAN - 13/11/07

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

Log in


Sign up | Forgot your password?

What's on?

  • Optus Deal

    Broadband + home phone + PlayStation®3 in a single package price!