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!
Subscription
Categories
- HowTos (612)
- Linux Devices (40)
- Linux Diggs (620)
- Linux News (1541)
- Linux Videos (24)
Recent Posts
Blog Archive
-
▼
2007
(340)
-
▼
September
(58)
- Dear Linux SysAd Blog Readers
- Proactive monitoring from linux terminal
- deleting new lines and return line from text file
- Google chat setup using PSI howto
- PSI messenger - a truly promising open messaging a...
- Google chat setup using GAIM Pidgin howto
- Linux command line shell variables defined
- ls - displaying directory contents in many ways
- Control of alternative linux executables
- Searching using whereis linux command
- Witchy which linux command
- NeroLinux - diehard Nero burning software
- Google Sky - Explore and Rediscover the Sky
- Celestia - 3D Earth and Sky visualization
- Earth3D - real-time 3D Earth visualization
- GcStar - managing personal collection items
- DStat - resource statistics linux tool
- Bandwidth Monitor-NG - terminal-based interface ba...
- KNemo - KDE network interface monitoring tool
- EtherApe - graphical network activity monitoring tool
- Beauty of Math using Linux
- print leading/trailing lines before/after a matchi...
- Nagios Monitoring - install and generic setup howto
- ChRT- change real-time attribute process scheduling
- squeezed out multiple commented lines
- UNIX to DOS text file format converter
- totally squeezed out multiple blank lines
- squeezed multiple blank lines into single line
- fmt - simple optimal text formatter
- Linux backup powered by RDiff-Backup
- Linux Ping command explained
- read and display text file from terminal
- URLView - URL and email extractor
- TFTP server - setup and install howto
- NMap - Linux port scanning
- removing garbage characters from screen terminal
- invert string match using grep
- RDesktop - remote desktop howto
- Graveman on Linux - burn baby burn burn
- HTOP - interactive process viewer alternative
- Caching DNS server install howto
- BitTorrent - downloading large files made easy
- PHPAlbum - web photo album install howto
- MRTG graph creation with Cisco routers
- Tree view of directories and file listings from co...
- MAC address packet filtering using IPTables
- GTK-based GNOME Linux Tools
- Linux backups powered by RSnapShot
- MRTG tutorial, install and howtos
- FindSMB - view shared folders from network
- Squid - upgrade and install howto
- prompt and press a key between script lines
- Devede - DVD/VCD video authoring and creation tool
- display file and file system status
- determine file type
- GNOME GUI task scheduler install howto
- Linux backups powered by Rsync
- KPackage - GUI package administration and manageme...
-
▼
September
(58)
Friday, September 21, 2007
Linux command line shell variables defined
Subscribe to:
Post Comments (Atom)
ILoveTux - howtos and news | About | Contact | TOS | Policy
0 comments:
Post a Comment