Friday, September 21, 2007

Linux command line shell variables defined

Reading daily doze of linux commands makes our memory to remember their command usage and command scopes. Doze of commands refreshes part of our linux brain to easily apply simple but useful linux commands when needed. Remembering those thousand of commands is not really an easy task specially most linux commands are not being used and applied on our daily linux work routines.

Here are a few doze of linux shell variables that might be helpful and useful to some bash shell scripting.

Usually, when creating shell scripts, we make use of user input variables or command line parameters that server as input arguments to shell commands or shell executable scripts. Using these variables provides us a way to determine shell program function execution and directly reference a submitted command line parameters.

Below are several linux bash variables that we can integrate when parsing user-input command line parameters.

$$

The above linux shell variable can be used to display and fetch current PID number of the current shell script.

How to know your current shell PID number from command line?

# echo $$
~~~~~~~~~~~~~~~
25341
~~~~~~~~~~~~~~~

Usage Application:
~~~~~~~~~~~~~~~~~~

On terminating and remembering the PID number of the current running shell program. A sample shell script usage of the above command would be

~~~~~~~~~~~~~~~~~~~~~
#/bin/bash
echo Current PID: $$
~~~~~~~~~~~~~~~~~~~~~

Next linux shell variable

When terminating a function, a proper way to determine if a function script had terminated properly is to determine its script exit code or status. This can be done by using the below linux shell variable

$?

Usage Application:
~~~~~~~~~~~~~~~~~~

When doing function or disk/file operations, or even executing another shell script, we can make use of this linux shell variable as shown below, which prints the exit code status of test_function
after finishing some stuff operations shown below.
~~~~~~~~~~~~~~~~~~~~~~~~~~~
#/bin/bash
function test_function {
# do some stuff
echo Function Exit Code $?
}
# call the function
test_function
~~~~~~~~~~~~~~~~~~~~~~~~~~~


Next linux variable

$0

This variable displays how the current shell program is being called. This could refer also the current path location where the program has been executed. We can also make use of this to test for any filename changes with the shell and/or any changes with the working folder of the file executable to verify if the current working folder or filename had been tampered or changed.

Usage Application:
~~~~~~~~~~~~~~~~~~

The below script checks if the script is being called from /tmp and the script name is test.sh

# cat /tmp/test.sh
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#/bin/bash
echo $0
if [ "$0" = "/tmp/test.sh" ]; then
echo "Good starting folder and still the same shell filename"
else
echo "Alert: Script Name and/or source folder changed!"
fi
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Next linux command variable would be

$1
$2


The above linux shell variable refers to arguments or parameters being passed to current shell script:

Usage Application:
~~~~~~~~~~~~~~~~~~

# cat test.sh
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#/bin/bash
echo Parameter No. 1: $1
echo Parameter No. 2: $2
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# ./test.sh firstname secondname
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Parameter No. 1: firstname
Parameter No. 2: secondname
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The above variable applies also to Nth variable.


The next linux shell variable:

$*

The above refers to all parameters or arguments passed with the current shell script

Usage Application:
~~~~~~~~~~~~~~~~~~

# cat test.sh
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#/bin/bash
echo Parameters Passed: $*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# ./test.sh Linux SysAd Blog
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Parameters Passed: Linux SysAd Blog
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Passing variables makes it easy for varying input arguments for a shell script. This approach flexes input entries for specific shell script. It can also be used to trigger shell functions and shell program directions as well.

Now, the below command refers to the total number of command line parameters or argument passed together with the shell script

$#

Usage Application:
~~~~~~~~~~~~~~~~~~

# cat test.sh
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#/bin/bash
echo Number of Parameters supplied: $#
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# ./test.sh I have only 5 parameters.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Number of Parameters supplied: 5
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Another shell variable is

**

This shell variable when used displays all files and directory folders located from where the shell script was called.

Usage Application:
~~~~~~~~~~~~~~~~~~

# cat test.sh
~~~~~~~~~~~~~~~~~~~~~~
#/bin/bash
echo **
~~~~~~~~~~~~~~~~~~~~~~

Looks useless but serves its purpose when used well.

Here's a tip:

Pressing ESC+* from command line list down all available binaries from current PATH variable of current box which the current user can make use of.


Final Note:
~~~~~~~~~~~

These linux shell variable can be handy when creating varying input arguments with your shell scripts. Making use of these variables creates convenience on passing parameters to a shell script.

Redirecting shell program direction when using these variables would then be easier assuming a single shell script that does two (2) different functions. Simply pass a trigger word that would redirect the shell program to call for a particular shell function defined inside that script like so:

# ./test.sh trigger2

That does the linux shell task as expected.

Happy weekend!

0 comments:

Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy