LiveFire Labs - Online UNIX Training - Company Logo


Online UNIX Training with Hands-on Internet Lab


"Eliminate the expense and inconvenience of classroom training without eliminating the classroom experience."

 


Home
Internet Lab
Console Access
Sample Course

Student Login


LiveFire Labs' UNIX Tip, Trick, or Shell Script of the Week - View


Questions? Call
1.888.843.1637 or send us email

September 29, 2003 - Useful Shell Scripting Variables - Part I - PWD

There are a number of shell variables, both built-in and user-defined, that are helpful and at times essential for building both basic and complex shell scripts.  This series of tips will present some of the more commonly used members from this group, explain what they contain, and provide examples of how they may be used when writing scripts.

The built-in variable PWD stores the present working directory, which is also referred to as the current working directory.  Each time the cd (change directory) command is issued, ksh updates the value stored in this variable to reflect the new directory location.  In the following example, the starting directory was /tmp, and then was changed to /usr/bin:

$ print $PWD
/tmp
$ cd /usr/bin
$ print $PWD
/usr/bin
$

One use for PWD in scripts is to validate the directory the user is in, when he or she invokes your shell script, against an acceptable list of directories.  The directory may need to be an exact match to a directory from the list, or can also be a subdirectory of one of the directories.  If the latter, some additional logic would need to be included in the script to perform an adequate comparison of the two values.

This short shell script demonstrates how PWD can be used to check for an exact directory match:

#!/bin/ksh
#
# Filename: dir_chk
# Description: checks if user's PWD is /tmp
#

if [ $PWD = /tmp ]
then
  print "PWD is /tmp"
else
  print "PWD is NOT /tmp"
fi

exit

The if-then-else statement in this script may be compressed into a single line of code and still produce the same results:

[ $PWD = /tmp ] && print "PWD is /tmp" || print "PWD is NOT /tmp"


NOTE: If you do not understand the use of && and || in this example, see our tip on Combining UNIX Commands using && and ||

Running the script produces the following results:

$ pwd
/home/livefire
$ /home/livefire/bin/dir_chk
PWD is NOT /tmp
$ cd /tmp
$ pwd
/tmp
$ /home/livefire/bin/dir_chk
PWD is /tmp
$


Learn more...
  

If you are new to the UNIX or Linux operating system and would like to learn more, you may want to consider registering for LiveFire Labs' UNIX and Linux Operating System Fundamentals online training course.

If you already have a solid grasp of the fundamentals but would like to learn more about the Korn shell and basic and advanced shell scripting, taking our Korn Shell Scripting course will be beneficial to you.

Our innovative hands-on training model allows you to learn UNIX by completing hands-on exercises on real servers in our Internet Lab.


More Tips...

· 
Popular UNIX Tips from the Past

spacer Box Border
 

Receive the UNIX Tip, Trick, or Shell Script of the Week by Email


First Name:


Email Address:






   1.888.843.1637

Home - Contact us - Company info - Privacy Statement   

 
©2002-2003 LiveFire Labs.  All rights reserved.
Linux® is a registered trademark of Linus Torvalds, author and developer of this public domain operating system.
UNIX® is a registered trademark of The Open Group in the United States and other countries.