Wednesday, November 30, 2011

Passing the Parameters, Please!

When entering a command to run a shell, you can actually tell the program information on
the same line.  These parameters can be used within the script as variables.  For
example:

$> ./program.sh Jason 50 75

This tells the program to use Jason as the first parameter, 50 as the second, and 75 as
the third.  In program.sh, we can access the parameters like this:

echo "Name: $1"
echo "One: $2"
echo "Two: $3"

The $ sign followed by a number is special in unix scripting to use parameters.  The
code above would produce this:

Name: Jason
One: 50
Two: 75

The parameters can also be file names if you like.  Now you can enter quick custom
commands in one line.  Enjoy, technophiliacs!

No comments:

Post a Comment