Tutorial on Parameter Substitution at Runtime

Today, we are presenting a very simple & useful thing i.e. Parameter Substitution in Unix at run-time.We must know that mainly there are four types of parameter substitutions which are commonly used in Unix Scripts.The Bourne shell has a handy set of operators for testing and setting shell variables.These are presented below with some quick examples.This will run in almost all the shells but as we all know exceptions are always there , You can use parameter substitution operators in any command line. You’ll see them used with the colon ( : ) operator , checking or setting default valuesLet us know see what are the types of parameter substations

1.${parameter:-value}
This Type of parameter substitution is used if the value is defined already for the parameter,So now it will use that value otherwise use the above one.Means if Variable is undefined or assigned a null string it must get the default value :For Example lets have a scenario where we have ${EDITOR:-/bin/vi}

In this case it will use the default EDITOR value if it defined otherwise it will use vi.We can use this feature in a program which prompts for a file name and use default value if the user simply press enter

echo “Enter file Name : \c”
read filename
f_name=${filename:-emp.lst}

2.${parameter:+ value}
This one will substitute the value if parameter is defined otherwise it will substitute nothing. (just opposite of – option).It simply override the value of parameter, if parameter is null, it will never override.

OPT_MODE=P
echo “Mode : ${ OPT_MODE:+T}”
output : Mode : T

As it was defined it will override its value.This feature can be used to set a variable to the output of a command and echo a message if the variable is no null.

found_file=`ls *log`
echo ${found:+”There are log”}

3.${parameter:=value}
It works similarly for (- option) except that it goes further and makes the assignment to the variable that is evaluated.With = option , we can use parameter substitution with a command without making intermediate assignment.

echo “enter file name : \c”
read filename
grep $pattern ${filename:=emp.lst} # filename is now assigned
X=1; while [ $x –le 10 ] can be combined with while [ ${x:=1} –le 10 ]

4.${parameter : ?value}
If the parameter is not assigned and null , it echoes value and kill the shell.This is quite useful in terminating script if user fails to respond properly to shell directive.

echo “Enter the filename: \c”
read filename
grep ${filename:? “No file entered”}

If no file name is entered then message will be displayed and script is also aborted the use of explicit exit command.
NOTE : If you omit the colon ( : ) from the expressions , the shell doesn’t check for an empty parameter. In other words, the substitution will happen whenever the parameter is set. (That’s how some early Bourne shells work: they don’t understand a colon in parameter substitution.).The first substitution ( ${Value=default} ) will leave $Value empty because the variable has been set. The second substitution will set $Value to default because the variable has been set but is empty. The third substitution will leave $Value2set to stuff :

+Value=
Value2=stuff
: ${Value=default}
: ${Value:=default}
: ${Value2:=default}

Tutorial on Time Stamps in Unix

Unix Tips By Rahul Miglani
Unix Tips By Rahul Miglani

As a beginner we must know that , we have 3 different time stamps in Unix. Each file has three time stamps, which record the last time that certain operations were performed on the file. You can search for files whose time stamps are within a certain age range or compare them to other time stamps.

1: Modify Time:

This is last time when the actual contents of the file were modified.

We can get the list of files modified n days using find command.

find . –type f –mtime +n -exec ls –l {} \;

Where:

-n : files modified between today to n-1 days.

n : files that was modified on that particular n day.

+n : files modified from n+1 days back .

Needless to say that we get modified time of file using ls –lt command 

Another example would be : Finding 15 Days old files

find *.* -mtime -15

You can play with these files now : Deleting these 15 Days old files

find *.* -mtime -15 -exec rm -rf {}\;

2: Change time:

Change time is something like altering the label of the package (file) whereas modify time is altering the content of the file. So in Unix we can say, change time is last time change in inode of the file and inode changes when we update the file, change permission, rename file, change in owner etc.

So whenever mtime change, ctime also does (change in content of file also update inode) but ctime change some extra time as explained above.

We use –ctime in find to get list of files changed in particular days back. ls –lc command is used to get the change time of a particular file.

Here file was zipped (renamed), means inode information of the file has been modified.

username@servername:username> ls -lt TestScript.sh.gz
-rwxrwxr-x 1 username aimsys 581 Dec 16 14:18 TestScript.sh.gz
username@servername:username> ls -lc TestScript.sh.gz
-rwxrwxr-x 1 username aimsys 581 Jan 25 07:29 TestScript.sh.gz

Here ls –lc gives the time when the file was zipped.

Another example would be the use of find command with ctime

find *.* -ctime -15

or may be you want to delete these files.

find *.* -ctime -15 -exec rm -rf {}\;

3: Access Time:

It’s a time the file was last accessed means the file was read. Last time we read that file.

username@servername:username> ls -lu TestScript.sh.gz
-rwxrwxr-x 1 username aimsys 581 Jan 25 07:43 TestScript.sh.gz

Example: Finding files which were accessed with in 15 days.

find *.* -atime -15

You can play with these files now : Deleting these 15 Days old files which were accessed (Not Modified)

find *.* -atime -15 -exec rm -rf {}\;

NOTE: With find you can choose to type -print or not with the find command, because the basic principle of find is obviously to print what ever it is doing so you may choose to type -print .To know more about the find command you can type man find on unix, to know what has been written about the find in Unix.

Summary :

Access Time (atime ) : This is the time that the file was last accessed, read or written to.

Modify Time (mtime ) :This is the last time the actual contents of the file were last modified.

Change Time (ctime ):This is the time that the inode information (permissions, name, etc., the metadata, as it were) was last modified.

Enjoying Playing with the times in Unix.