[HPADM] RE: Compounded variable...

From: Mahajan Sachin (Sachin.Mahajan@o2.com)
Date: Mon Nov 11 2002 - 10:00:01 EST


Hi Admins...

thank you for your quick responses....
There were quite a few methods suggested.....have attached the respective
emails here...

the main contributors were:
Butcher, William (Bill.Butcher)
Thierry ITTY
Bill Hassell
Bob Vance
Rita Workman
Steve
TOTSCH,DAVID (HP-USA,ex3)

Cheers,
Sachin.

=========================================================
This electronic message contains information from the mmO2 plc Group
which may be privileged or confidential. The information is intended to be
for the use of the individual(s) or entity named above. If you are not the
intended recipient be aware that any disclosure, copying, distribution or
use of the contents of this information is prohibited. If you have received
this electronic message in error, please notify us by telephone or email
(to the numbers or address above) immediately.
=========================================================


attached mail follows:


Sachin,

 

You're going to need to use the 'eval' function to use the counter variable
within a loop in your script. See the attached notes and examples from
early last year and late 2000 for more information.

 

Good luck,

 

- Bill

 

 

William H. Butcher III

ChevronTexaco Pipeline Company

2811 Hayes Road, Suite 3313F

Houston, Texas 77082

Office: (281)596-2847, Cellular: (281)543-9651

E-mail: <mailto:> bill.butcher@chevrontexaco.com

-----Original Message-----
From: Mahajan Sachin [mailto:Sachin.Mahajan@o2.com]
Sent: Friday, November 08, 2002 6:14 AM
To: HP UX NETHERLANDS (E-mail)
Subject: [HPADM] Compound variable

 

Hi Admins,

I am trying to write a script with compounded variables....ie my variable
name is also a variabl...but it does'nt seem to work.

Could someone point out the mistake or give me an alternative way of doing
it ?

for i in Appln
do

        echo
        echo
        echo "Please enter the directory where you want $i to be installed"
        read "$i"
        echo $($i)
        echo $Appln

done

 

echo $Appln gives me the right input variable
whereas echo $($i) does'nt !!!

This is perplexing....

Thanks in advance...
Sachin.

=========================================================
This electronic message contains information from the mmO2 plc Group
which may be privileged or confidential. The information is intended to be
for the use of the individual(s) or entity named above. If you are not the
intended recipient be aware that any disclosure, copying, distribution or
use of the contents of this information is prohibited. If you have received
this electronic message in error, please notify us by telephone or email
(to the numbers or address above) immediately.
=========================================================


attached mail follows:


Raj,

You're going to need to use the 'eval' function to increment your array
subscripts. Use the following solution from last year for reference
(solution is at the bottom):

<<[HPADM] SUMMARY; Scripting Question>>

Enjoy,

- Bill

William H. Butcher III

Chevron Research and Technology Company

2811 Hayes Road, Suite 3313F

Houston, Texas 77082

Office: (281)596-2847, Cellular: (281)300-9112

E-mail: <mailto:> wbut@chevron.com

        -----Original Message-----

        From: Raj Mannar <mailto:[SMTP:rajmannar@ureach.com]>
[SMTP:rajmannar@ureach.com]

        Sent: Tuesday, February 13, 2001 9:21 AM

        To: <mailto:hpux-admin@dutchworks.nl> hpux-admin@dutchworks.nl

        Subject: [HPADM] Script question (revised)

        Hi,

           thanks for the folks who replied . I guess there has been

        some confusion over my question, since the suggestions i got

        was not exactly what i am looking for. So, let me rephrase

        the question.

        Consider you have a file with entries as follows:

        LV[0]="blahblah";FS[0]="fsdfsdf";FS_OPT[0]="dfdfds"

        LV[1]="dfsdfs";FS[1]="dsfsdfcv";FS_OPT[1]="-dsfs"

        and so on

        I would like to change this file in such a way that,

        only the array subscripts are incremented by 1 ,with

        the rest of the assignments being exactly the same.

        So, anything [0] should become [1]

             anything [1] should become [2]

        To summarise, in a file how do i change an

        entry [n] to [n+1] ??

        (and if possible, to make the script more flexible, this

        should be for any "n" i.e it should be able

        to increment the value of [n] at any given starting

        point of "n" . Say, push any value from 35 onwards. )

         

        thanks

        raj

         _________Reply Separator____________________

> Subject: [HPADM] Script question

> Author: Raj Mannar < <mailto:rajmannar@ureach.com>
rajmannar@ureach.com>

> Date: 2/12/2001 6:23 PM

>

> Hi,

>

> I have a file (package control script)

> as follows:

> ******

> LV[0]="/dev/vgname/ s1";

> FS[0]="/prod/data01";FS_MOUNT_OPT[0]="-o delaylog"

> LV[1]="/dev/vgname/ s1";

> FS[1]="/prod/data01";FS_MOUNT_OPT[1]="-o delaylog"

> ::

> ::

> LV[47]= xxx

> FS[47]="xxxx"; FS_MOUNT[47]="-o delaylog"

> ********

>

> I need to add an entry with the subscript [0] at

> the beginning and increment the current subscripts

> by 1 ; For eg:

> The existing LV[0]/FS[0]/FS_MOUNT_OPT[0] should

> become LV[1]/FS[1]/FS_MOUNT_OPT[1]

> and the existing [1] entries should become [2]

> and so on..

>

> I am trying to build a script to do this.

> WOuld appreciate any suggestions.

>

> Thanks and will summarize

> raj

>

>


attached mail follows:


Admins,
I am trying to write a script that "references" a variable. But I can't make
it work. You will see two functions, one that works and one that does not.
Any ideas on how to make the second one work?

****************************************************************************

#!/usr/bin/sh

Source_Dir="
/source1 /destination1 Files_1
/source2 /destination2 Files_2
/source3 /destination3 Files_3
"

Files_1="
file1a
file1b
file1c
"

Files_2="
file2a
file2b
file2c
"

Files_3="
file3a
file3b
file3c
"

DoSourceWorks()
{
echo "This works..."

set -- $Source_Dir
while [ $# -gt 0 ]
do

        SourceDir=$1
DestDir=$2
FileArray=$3

        case $FileArray in

        Files_1)

        for File in $Files_1
do

        echo "$SourceDir$DestDir/$File"

        done

        ;;

        Files_2)

        for File in $Files_2
do

        echo "$SourceDir$DestDir/$File"

        done
;;

        Files_3)

        for File in $Files_3
do

        echo "$SourceDir$DestDir/$File"

        done
;;

        esac

        shift 3

done
}

DoSourceFails()
{
echo "This fails..."

set -- $Source_Dir
while [ $# -gt 0 ]
do

        SourceDir=$1
DestDir=$2
FileArray=$3

        # I want a "pointer" to

        for File in $FileArray # a variable here...
do

        echo "$SourceDir$DestDir/$File"

        done

        shift 3

done
}

DoSourceWorks
DoSourceFails
exit 1

****************************************************************************

Best Regards,

Karl R. Seeger Desk (425) 392-5316 Ex 3724
System Administrator / Programmer Fax (425) 557-9026
Software Development, Zetec Inc. E-Mail
<mailto:Kseeger@zetec.com> Kseeger@zetec.com

ANSWER; Bill Butcher came up with this fix...

DoSourceFails()
{
echo "This works now!!!"

set -- $Source_Dir
while [ $# -gt 0 ]
do

        SourceDir=$1
DestDir=$2
FileArray=$3
Dollar='$'
Array=`eval echo $Dollar$FileArray`

        for File in $Array

        do

        echo "$SourceDir$DestDir/$File"

        done

        shift 3

done
}

Now it works great!


attached mail follows:


you can try something on eval

-----Original Message-----
From: hpux-admin-owner@DutchWorks.nl
[mailto:hpux-admin-owner@DutchWorks.nl]On Behalf Of Mahajan Sachin
Sent: Friday, November 08, 2002 5:44 PM
To: HP UX NETHERLANDS (E-mail)
Subject: [HPADM] Compound variable

Hi Admins,

I am trying to write a script with compounded variables....ie my variable
name is also a variabl...but it does'nt seem to work.

Could someone point out the mistake or give me an alternative way of doing
it ?

for i in Appln
do

        echo
        echo
        echo "Please enter the directory where you want $i to be installed"
        read "$i"
        echo $($i)
        echo $Appln

done

echo $Appln gives me the right input variable
whereas echo $($i) does'nt !!!

This is perplexing....

Thanks in advance...
Sachin.

=========================================================
This electronic message contains information from the mmO2 plc Group
which may be privileged or confidential. The information is intended to be
for the use of the individual(s) or entity named above. If you are not the
intended recipient be aware that any disclosure, copying, distribution or
use of the contents of this information is prohibited. If you have received
this electronic message in error, please notify us by telephone or email
(to the numbers or address above) immediately.
=========================================================


attached mail follows:


use :
eval "echo \$$i" instead of echo $($i)

A 12:13 08/11/2002 -0000, vous avez écrit :
>Hi Admins,
>
>I am trying to write a script with compounded variables....ie my variable
>name is also a variabl...but it does'nt seem to work.
>Could someone point out the mistake or give me an alternative way of doing
>it ?
>
>for i in Appln
>do
>
> echo
> echo
> echo "Please enter the directory where you want $i to be installed"
> read "$i"
> echo $($i)
> echo $Appln
>
>done
>
>
>echo $Appln gives me the right input variable
>whereas echo $($i) does'nt !!!
>
>This is perplexing....
>
>Thanks in advance...
>Sachin.
>
>
>=========================================================
>This electronic message contains information from the mmO2 plc Group
>which may be privileged or confidential. The information is intended to be
>for the use of the individual(s) or entity named above. If you are not the
>intended recipient be aware that any disclosure, copying, distribution or
>use of the contents of this information is prohibited. If you have received

>this electronic message in error, please notify us by telephone or email
>(to the numbers or address above) immediately.
>=========================================================
>
>
>Attachment Converted: "d:\app\net\eudora\attach\HPADMCom"
>
                        - * - * - * - * - * - * -
Bien sûr que je suis perfectionniste !
Mais ne pourrais-je pas l'être mieux ?
        Thierry ITTY
eMail : Thierry.Itty@Besancon.org FRANCE


attached mail follows:


Hi,

On Fri, 8 Nov 2002 12:13:50 -0000 Mahajan Sachin <Sachin.Mahajan@o2.com>
wrote:

> I am trying to write a script with compounded
> variables....ie my variable name is also a
> variabl...but it does'nt seem to work.
>
> Could someone point out the mistake or give me
> an alternative way of doing it ?
>
>
> for i in Appln
> do
>
>
>         echo
>         echo
>         echo
> "Please enter the directory where you want
> $i to be installed"
>         read "$i"

  This is OK.

>         echo $($i)

  This is incorrect, should be: eval echo $i

  eval expands everything, in this case, the name
  assigned to $i and then runs the resultant
  command.

--
Best regards,
Bill Hassell, pooderbill@mindspring.com

attached mail follows:


Try a variation of the following:
 
    eval echo \$$i
 
This evaluates the '$'s before parsing the line again, so the \$ is left as
a "$", while $i becomes "Appln", leaving the command to parse/execute as
 
    echo $Appln
 
.
 
==============================
Tks | mailto:BVance@sbm.com <mailto:BVance@sbm.com>
BV | mailto:BobVance@alumni.caltech.edu
<mailto:BobVance@alumni.caltech.edu>
Sr Technical Consultant Arrow, SBM Division
VOX: 770-623-3430
FAX: 801-327-6152
==============================
-----Original Message-----
From: hpux-admin-owner@DutchWorks.nl
[mailto:hpux-admin-owner@DutchWorks.nl]On Behalf Of Mahajan Sachin
Sent: Friday, November 08, 2002 7:14 AM
To: HP UX NETHERLANDS (E-mail)
Subject: [HPADM] Compound variable

Hi Admins,

I am trying to write a script with compounded variables....ie my variable
name is also a variabl...but it does'nt seem to work.

Could someone point out the mistake or give me an alternative way of doing
it ?

for i in Appln
do

        echo
        echo
        echo "Please enter the directory where you want $i to be installed"
        read "$i"
        echo $($i)
        echo $Appln

done

echo $Appln gives me the right input variable
whereas echo $($i) does'nt !!!

This is perplexing....

Thanks in advance...
Sachin.

============================This electronic message contains information from the mmO2 plc Group
which may be privileged or confidential. The information is intended to be
for the use of the individual(s) or entity named above. If you are not the
intended recipient be aware that any disclosure, copying, distribution or
use of the contents of this information is prohibited. If you have received
this electronic message in error, please notify us by telephone or email
(to the numbers or address above) immediately.
============================


attached mail follows:


Hi,

I am not sure what you are trying to accomplish with the variable Appln, but
I have fixed the error to line 7, echo $($i).

See script below. When trying to address the value of a variable I found it
best to surround it by {} brackets. I will look for the technical reasons
behind this and send it along if I find it. FYI I was testing in the korn
shell. Hope this helps.

# cat sds
for i in Appln
do
        echo
        echo
        echo "Please enter the directory where you want $i to be installed"
        read "$i"
        echo "This is the value of i : {$i}"
        echo "This is the value of Applin : $Appln"
done

root@primo [/tmp]
# ./sds

Please enter the directory where you want Appln to be installed
/tmp
This is the value of i : {Appln}
This is the value of Applin : /tmp

root@primo [/tmp]
#
Mahajan Sachin wrote:

Hi Admins,

I am trying to write a script with compounded variables....ie my variable
name is also a variabl...but it does'nt seem to work.

Could someone point out the mistake or give me an alternative way of doing
it ?

for i in Appln
do

        echo
        echo
        echo "Please enter the directory where you want $i to be installed"
        read "$i"
        echo $($i)
        echo $Appln

done

echo $Appln gives me the right input variable
whereas echo $($i) does'nt !!!

This is perplexing....

Thanks in advance...
Sachin.

=========================================================
This electronic message contains information from the mmO2 plc Group
which may be privileged or confidential. The information is intended to be
for the use of the individual(s) or entity named above. If you are not the
intended recipient be aware that any disclosure, copying, distribution or
use of the contents of this information is prohibited. If you have received
this electronic message in error, please notify us by telephone or email
(to the numbers or address above) immediately.
=========================================================


attached mail follows:


Here's an old very basic script example that maybe will help you:

#!/bin/sh
# basic script
###################
for i in alpin
   do
    print -n "Name of file"
    read file_name
    echo $file_name
   done

Here's another:

print -n "Name of file"
read file_name
print -n "Name of receiver"
read send_to
mail $send_to < $send_file

Hope these help
Rita

>>> Mahajan Sachin <Sachin.Mahajan@o2.com> 11/08/02 07:13AM >>>
Hi Admins,

I am trying to write a script with compounded variables....ie my
variable
name is also a variabl...but it does'nt seem to work.
Could someone point out the mistake or give me an alternative way of
doing
it ?

for i in Appln
do

        echo
        echo
        echo "Please enter the directory where you want $i to be
installed"
        read "$i"
        echo $($i)
        echo $Appln

done

echo $Appln gives me the right input variable
whereas echo $($i) does'nt !!!

This is perplexing....

Thanks in advance...
Sachin.

=========================================================
This electronic message contains information from the mmO2 plc Group
which may be privileged or confidential. The information is intended to
be
for the use of the individual(s) or entity named above. If you are not
the
intended recipient be aware that any disclosure, copying, distribution
or
use of the contents of this information is prohibited. If you have
received
this electronic message in error, please notify us by telephone or
email
(to the numbers or address above) immediately.
=========================================================


attached mail follows:


I have found the following web site, there are many others that talk about
scripting. The variable section is pretty good, take a look. It you like I
can help you further with your script, however I need more information as I
am not clear what you trying to accomplish.

Hope this helps.

http://www.ug.cs.usyd.edu.au/~cs2/soft2004/s1_2002/tutes/week5/scripting.htm
l
<http://www.ug.cs.usyd.edu.au/~cs2/soft2004/s1_2002/tutes/week5/scripting.ht
ml>

Unix Scripting Notes

1. Introduction
<http://www.ug.cs.usyd.edu.au/%7Ecsled/biwf/scripttut/notes.html#Introductio
n>
2. Hello
<http://www.ug.cs.usyd.edu.au/%7Ecsled/biwf/scripttut/notes.html#HelloWorld>
World - Writing to standard output, Hash bang, Comments
3. Using
<http://www.ug.cs.usyd.edu.au/%7Ecsled/biwf/scripttut/notes.html#UsingUnixCo
mmands> Unix Commands - Simple Redirects, Pipes, Multiline scripts
4. Using
<http://www.ug.cs.usyd.edu.au/%7Ecsled/biwf/scripttut/notes.html#UsingVariab
les> Variables - Defining variables, Accessing variables, String
replacement in commands
5. Using
<http://www.ug.cs.usyd.edu.au/%7Ecsled/biwf/scripttut/notes.html#UsingBackti
cks> Backticks - Assigning output to a variable
6. Special
<http://www.ug.cs.usyd.edu.au/%7Ecsled/biwf/scripttut/notes.html#SpecialVari
ables> Varaibles - Command line arguments, Exit status, Process ID
7. The
<http://www.ug.cs.usyd.edu.au/%7Ecsled/biwf/scripttut/notes.html#TheIfStatem
ent> if Statement - Using an if statement, test and square brackets
8. Numbers
<http://www.ug.cs.usyd.edu.au/%7Ecsled/biwf/scripttut/notes.html#NumbersInSh
ellScripts> In Shell Scripts - The 'expr' command, Use of backticks,
Globbing and the using the escape character
9. The
<http://www.ug.cs.usyd.edu.au/%7Ecsled/biwf/scripttut/notes.html#TheWhileLoo
p> while Loop - Syntax of a while loop, Using a while loop
10. The
<http://www.ug.cs.usyd.edu.au/%7Ecsled/biwf/scripttut/notes.html#TheForLoop>
for Loop - Syntax of a for loop, Lists
11. The
<http://www.ug.cs.usyd.edu.au/%7Ecsled/biwf/scripttut/notes.html#TheCaseStat
ement> case Statement - Syntax for case statement, Magic 8-Ball, Wildcards
in case statements
12. Unix
<http://www.ug.cs.usyd.edu.au/%7Ecsled/biwf/scripttut/notes.html#UnixRedirec
tsInDetail> Redirects In Detail - Redirect input from scripts, Redirect one
stream to another
13. Conditional
<http://www.ug.cs.usyd.edu.au/%7Ecsled/biwf/scripttut/notes.html#Conditional
Execution> Execution - && and ||
14. Variables
<http://www.ug.cs.usyd.edu.au/%7Ecsled/biwf/scripttut/notes.html#VariablesIn
Detail> In Detail - Quoting (' and "), Exporting variables, Variable
Constructs
15. Useful
<http://www.ug.cs.usyd.edu.au/%7Ecsled/biwf/scripttut/notes.html#UsefulComma
nds> Commands - basename, dirname, exit, wait, read, trap, head, tail,
grep, shift

  _____

Introduction
Introduction

A shell script, in its simplest form, is a series of shell commands put
together in a text file, which can then be executed in order. The commands
used in the scripting language are the same ones the user types in when they
are using Unix through a command line. To those of you who are used to
writing code in languages like C and Java, shell scripting can seem strange
because it doesn't need to be compiled before it is run and because of it's
relative lack of structure, but it can prove to be a surprisingly powerful
tool.

Shell scripting is useful in situations where you want to get a program up
and running without wasting too much time. Often a shell script will only
have a fraction of the number of lines of an equivalent C or Java program,
and hence, it only takes a fraction of the time to write. In this tutorial,
we will look at how to use the Bourne shell, sh, but there are many other
shell scripting languages available, such as ksh, csh and bash.

In this tutorial, any code will be written in a box, and comments will be
coloured green, as shown below:
  

#!/bin/sh
ls # Comment

  _____

HelloWorld
Hello World

In keeping with Computer Science tradition, the first Unix script we will
look at is the "Hello World" example. This is a very simple example, but it
is worth seeing because it demonstrates a number of interesting aspects of
Unix scripting.
  

#!/bin/sh
printf "Hello World\n" # Writes string to standard output

The program above is a valid shell script, you can run it by typing it into
a text file, making the text file executable, and then running it just like
any other program. Note that you do not need to compile it, like you would
for C, or explicitly run an interpreter, like with Java, it runs as is.

When you look at the program, you will see three distinct parts to it. We
will look at each of these seperately.

* #!/bin/sh

        This line should be the first line of each and every shell script
you write. "#!" (pronounced "Hash bang") tells the computer that what comes
after it is the path of the interpreter needed to execute the script. In
this case (and with all of the scripts presented here), the interpreter is
/bin/sh, the Bourne Shell. If you are going to write a program in a
different language, you change the name of the interpreter, for example,
Python scripts start with #!/local/usr/bin/python.
  

* printf "Hello World\n"

        This line writes Hello World to the standard output. The printf
command operates in a similiar way to the printf() function in C, and can be
used to write formatted output to the standard output. There is a similiar
command, echo, which also writes text to the standard output, but this isn't
as powerful as printf.
  

* # Writes a string to standard output

        In sh, anything between # and the end of the line is considered to
be a comment, and is ignored by the interpreter.

  _____

UsingUnixCommands
Using Unix Commands

One of the features of Unix scripting is that any command you can use in
Unix is valid code in a shell script. All of the commands you have learnt in
Unix can be used in a shell script, for example:
  

#!/bin/sh
ls -alp | more # Displays the contents of the current directory, and
displays them one page at a time

Try writing this code into a file and executing it. Compare the output from
this program with the output produced when you type ls -alp | more into the
command line. You should see that they produce the same output.

One thing to note in the above example is the use of the pipe (|). All of
the usual Unix redirects are available for use in a Shell script. As an
example of how these might be used, here is a program which writes out the
login names of everyone connected to our machine in lexiograpic order.
  

#!/bin/sh
who > /tmp/tempfile # Writes the names of who is logged in to the file
/tmp/tempfile
sort /tmp/tempfile # Sorts the file and writes it to standard output
rm /tmp/tempfile # Deletes the temporary file

This program runs the 'who' command, which displays a list of everyone
logged in, and redirects its output to a temporary file. Note that the
temporary file is put in the /tmp directory, it is common practice to put
any temporary files in this directory when writing a Unix script. The next
line writes a sorted version of this file to standard output, and the final
line removes the temporary file which was created by 'who'. We could write
this without needing to create a temporary file by piping the output from
who into sort, as shown below.
  

#!/bin/sh
who | sort # Displays logged in users in lexiographic order

If you want a command to continue onto the next line, you can put a '\' at
the end of the line. You can use this as many times as you want, and it is
possible to have commands which run over many lines.
  

#!/bin/sh
ls \
-alp

One final thing to remember is if you don't want error messages from the
commands to appear on the screen, you should redirect the error stream to
null by writing 2>&-. This will stop error messages from the command from
reaching the terminal. Take, for example, this script which removes any
temporary files which may have been created a text editor. Temporary
filenames generally end with a ~, so the script will delete anything ending
in ~, but if there are no temporary files, we don't want to display an error
message, we should just quit normally.
  

#!/bin/sh
rm *~ 2>&- # Error messages are sent to null, ie. they are not displayed

Try running this script with and without the redirect, and notice the
difference when there is nothing to remove.

We will take a closer look at redirecting input and output later on.

  _____

UsingVariables
Using Variables

I'm sure that by now you are wondering about how you can declare a variable
in shell script. Creating a variable in a shell script is very easy, much
easier than in C or Java, all you need to do is write VARIABLENAME=VALUE
(Note that there are no spaces on each side of the equals sign. If you put
in a space, strange things will happen). To access a variable, put a dollar
sign, $, before the variable name, eg. $VARIABLENAME.

Variables in shell scripts operate in a very different way to variables in
other computer languages. Some of the things you must remember are:

1. In shell scripts, all variables can be thought of as strings. Even
if you say that a variable contains a number, eg. VAR=5, you can still treat
it like a string.
2. You do not need to declare a variable. The variable is made as soon
as you start using it.
3. If you have a line in a shell script where you access a variable,
the line is executed with the variable name replaced by its value. This
aspect may seem confusing, so it is best to look at some examples to get a
better idea of how they work.

Here we have the example from the previous page, altered to include a
variable.
  

#!/bin/sh

TMPFILE=/tmp/tempfile

who > $TMPFILE # Writes the names of who is logged in to the file
/tmp/tempfile
sort $TMPFILE # Sorts the file and writes it to standard output
rm $TMPFILE # Deletes the temporary file

When the program gets to a line in this program with '$TMPFILE' in it, it
relpaces '$TMPFILE' with '/tmp/tempfile', and then executes the command.
Hopefully you can see that if we replace '$TMPFILE' with '/tmp/tempfile' on
each line, the same three commands are executed as were in the previous
page. One important thing to remember about variables is that they can be
used anywhere in a command. For example, a script which simple executes the
who command might look like this:
  

#!/bin/sh

COMMAND=who
$COMMAND

When accessing variables, you can optionally put the variable name in { },
for example, the code below runs the ls command, but does so in a strange
way. If you can't see how putting the variable name in braces would be
useful, don't worry, its purpose will become clear later in the tutorial.
  

#!/bin/sh

COMMAND=l
${COMMAND}s -alp

  _____

UsingBackticks
Using Backticks

One of the more interesting things you can do with variables is to assign
the output of a command to a variable. This is done by enclosing the
expression in backticks (`). The backtick is the button at the top left of
you keyboard, next to the number 1. When a script reaches a line containing
a command inside backticks, it executes this command, replaces that command
with its output, and then executes the line as per normal. As with most
aspects of Unix scripting, this is best explained with an example.
  

#!/bin/sh

OUTPUT=`echo HELLO`

printf "$OUTPUT\n"

This example executes in the following order:

1. When the script is on the first line, it encounters a command in
backticks. This is the first command that is exectued.
2. Then, the command `echo HELLO` is replaced by its output HELLO, so
now the line looks like this 'OUTPUT=HELLO'
3. The first line is now executed, so now the variable OUTPUT has the
value HELLO.
4. Now we are up to the second line. Before this line is executed,
$OUTPUT is replaced by the value of the variable OUTPUT, HELLO, so now it
look like 'printf "HELLO\n"'
5. This line is executed, writing HELLO to standard output.

This was a fairly long winded method of getting something written to the
screen, but it does demonstrate how backticks work. Later we will look at
more interesting and useful ways of using backticks.

  _____

SpecialVariables
Special Variables

When you start a Unix script, there are already a number of special
variables which you can use. The first ones we will look are the ones
relating to command line arguments. The important variables are:

* $# - This variable contains the number of command line arguments. If
someone runs your script with a line like 'my_script John Bill Fred', this
variable will equal 3
* $0 - This contains the name of the script. This is useful to know
because the script might be moved to a file with a different name.
* $1, $2, $3, ... - The command line arguments. If the script is
executed with a command line like 'my_script John Bill Fred', $1 will be
John, $2 Bill and $3 Fred.
* $@ - All of the command line arguments (excluding the name of the
script).

As an example, consider the script below. This script simply writes out some
of these variables to standard output.
  
#!/bin/sh
# This script requires at least two command line arguments to run properly

printf "The scripts name is $0\n"
printf "The first argument is $1\n"
printf "The second argument is $2\n"
printf "The script was executed with $# command line arguments\n"
printf "The full list of arguments is : $@\n"

Type in and execute this script, with different numbers of command line
options.

Another other important special variable is known as the Process ID (PID).
In Unix, each process, or running program, is allocated a unique PID between
1 and 32767. You can find out the value of the PID from the $$ variable.
  

#!/bin/sh
printf "The Process ID is $$\n"

Because the PID is unique, it is often used to help name temporary files.
Here we have the example given in the Using Variables tutorial, where we
used a temporary file.
  

#!/bin/sh

TMPFILE=/tmp/tempfile

who > $TMPFILE # Writes the names of who is logged in to the file
/tmp/tempfile
sort $TMPFILE # Sorts the file and writes it to standard output
rm $TMPFILE # Deletes the temporary file

The problem with this is that if two copies of this script are run at once,
they will both try to access the same temporary file, and we could get some
errors occuring. A way to avoid this problem would be to ensure that each
invocation of the script gave its temporary file a unique name. This can be
done using the Process ID as shown below:
  

#!/bin/sh

TMPFILE=/tmp/tempfile-$$ # This is a unique filename

who > $TMPFILE # Writes the names of who is logged in to the temporary
file
sort $TMPFILE # Sorts the file and writes it to standard output
rm $TMPFILE # Deletes the temporary file

The final variable which you should know about is $?. This contains the exit
status of the last command executed. When a program finishes, it passes a
number back to the shell telling it how it exited. Generally, if the program
finishes normally, the program will return a value of 0, and if the program
ended due to an error, it will return a non-zero value. This example below
uses cat to display the file $1, and then writes out the exit status of cat.

  

#!/bin/sh
# Requires at least 1 command line argument

cat $1
EXIT_STAT=$? # EXIT_STAT is the exit status of cat

printf "\n\ncat had an exit status of $EXIT_STAT\n"

Try running this script with different command line arguments. If the file
exists and cat can display it, you will find that the exit status is 0, but
if you give it a file it can't read, or one which doesn't exist, the exit
status will be something else.

  _____

TheIfStatement
The if Statement

Almost every programming language has a feature that allows you to execute
code based on the truth of a condition, and shell scripting is no different.
The if statement allows you to check if a given expression is true, and if
it is, execute a certain piece of code. The syntax for the if statement is
as follows:
  

if EXPRESSION ; then
  DO THIS
  ...
elif EXPRESSION ; then # else if is optional, and there can be more
than 1 of them
  DO THIS
  ...
elif ...

else # else is optional, only one per if statement
  DO THIS
  ...

fi # fi = end of the if statement (fi is if
backwards)

The first important part of the if statement is the expression. The
expression is any valid Unix command, and the expression is considered to be
true if and only if it has an exit status of zero. The if statement can be a
good way to check if a program has worked. An example of how this might work
is:
  

#!/bin/sh

if cat $1 2>&- ; then
  printf "\n\ncat finished successfully\n"
else
  printf "\n\ncat failed\n"
fi

This program will attempt to display the file given to it as an argument. If
it was displayed successfully, it will write a message to standard output,
and if it failed, a different message is written.

While it is nice to see if a program completed successfully, often when we
write an if statement we want perform some sort of check on the value of a
variable. The test command provides us with this ability. We can run the
test command by typing the word test but, to make the code a little more
readable, we can just put the thing we want to test in square brackets. In
other words, the following commands are equivalent:

* test 3 -ne 4
* [ 3 -ne 4 ]

The test command will return 0 if the test evaluated to true, and it will
return a non-zero value if the test evaluated to false, making it very
useful as the expression part of an if statement.

There are many different ways to use the test command (run man test to see
them all), but some of the more useful ones include:

* [ -r filename ] - Returns true if the file exists and is readable.
* [ -d filename ] - Returns true if the filename exists and is a
directory.
* [ string1 = string2 ] - Returns true if two strings are equal.
* [ string1 != string2 ] - Retruns true if the two strings are not
equal.
* [ string ] - Returns true if the string is not the null string.
* [ number1 -eq number2 ] - Retruns true if the two numbers are equal.

* [ number1 -ne number2 ] - Retruns true if the two numbers are not
equal.
* [ number1 -gt number2 ] - Returns true if the first number is
greater than the second (-lt for less than).
* [ number1 -ge number2 ] - Returns true if the frist number is
greater than or equal to the second number (-le for less than or equal).
* [ ! expression ] - True if the expression is false. This can be used
with a command like [ ! -r $1 ].
* [ expression1 -a expression2 ] - And operator, true only if
expression1 and expression2 are true.
* [ expression1 -o expression2 ] - Or operator, true if either
expression1 is true or expression2 is true.

The script below uses the test command to perform error checking on the
input to a program.
  
#!/bin/sh
# This script will display the file give as the first argument.

if [ $# -eq 0 ] ; then # If no command line arguments were
given...
  printf "Usage: $0 filename\n" # write the appropriate error message
to standard output
  exit 1 # and then exit with a non-zero exit
status

elif [ $# -gt 1 ] ; then
  printf "Too many arguments, only the first argument will be dispalayed\n"
fi

if [ -r $1 ] ; then # If the file to be displayed if
readable
  cat $1 # display the file
else
  printf "$0: cannot open $1\n" # Could not open file, error message
and exit
  exit 2
fi

  _____

NumbersInShellScripts
Numbers In Shell Scripts

At this stage, we have learnt how we can treat variables as strings, but
often we need to treat a variable like it is a number. The expr command
allows us to evaluate arbitrary mathematical expressions, and by using
backticks, we can store the result. For example:
  

#!/bin/sh

NUM=5

printf "$NUM + 1 = `expr $NUM + 1`\n" # Prints NUM + 1 to the screen

NUM=`expr $NUM \* 2` # NUM equals 2 times the old value
of NUM (must put a slash before the *)
printf "NUM is now $NUM\n"

Before we continue, you should make a note of the '\*' on the third line. A
star on its own in a command line is known as the 'glob' character, and if
this is in a command, it will be replaced by all of the filenames in the
directory. If you want to see how this is done, write a script which simply
writes out the number of arguments ($#) to the screen and run this script
with * as the argument. You will see that the number of arguments isn't
necessarily 1, it is the number of visible files in the directory. To stop
Unix from replacing the * with filenames, we put the escape character \
before it. You will also need to put a \ before question marks, open and
close brackets, less than and greater than's, dollar signs, etc. if you want
them to be treated as normal characters.

Now that that's out of the way, lets look at the above example line by line:

* On the first line, the variable NUM is assigned the value of five.
* The next thing executed is the command in backticks on the second
line. This calculates 5 + 1, so it outputs 6. When this command is finished,
the second line looks like printf "$NUM + 1 = 6\n"
* The $NUM variable is replaced by 5, and the second line is executed,
writing the text to the terminal.
* Now we reach the third line. Firstly, the command in backticks is
evaluated. This turns out to be 10. Now this line is NUM=10.
* The variable NUM now has the value 10
* Prints out the string on the final line.

You can also use the expr command to conduct comparisons between two
expressions. If the expression is true, it will output 0, else it will
output 1.
  

#!/bin/sh

# Compares (5 * 6) + 3 to 5 + (6 * 3)

if expr 5 \* 6 + 3 \> 5 + 6 \* 3 ; then
  printf "Expression evaluated to true\n"
else
  printf "Expression evaluated to false\n"
fi

  _____

TheWhileLoop
The while Loop

The only major topic we haven't yet covered in shell scripts is looping. The
first type we will look at is the while loop. Those of you who have
programmed in Java or C will already be familiar with this type of loop.
When a while loop is reached, the condition is tested. If the condition is
false, we continue running the script from below the end of the loop,
however if it evaluates to true, the code in the body of the loop is
executed. Once the body has been executed, we return to the top of the loop
and test the condition again. The while loop is useful in situations where
we must execute a piece of code a certain number of times. The syntax for
the while loop is as follows:
  

while EXPRESSION; do
  BODY OF LOOP
  .
  .
  .
done

Consider the following piece of code which writes out the numbers from 1 to
$1 to the terminal.
  

#!/bin/sh

COUNT=1

while [ $COUNT -le $1 ]; do
  printf "${COUNT}\n" # Write COUNT to standard output
  COUNT=`expr $COUNT + 1` # Increment COUNT
done

printf "FINISHED\n"

On the first iteration through the loop, COUNT has a value of 1. If COUNT is
not less than $1, the test will evaluate to false, the loop is finished and
the program continues from after the done, but if it was less than 1, we
execute the two commands in the body of the loop and try the test again.

  _____

TheForLoop
The for Loop

The other way to write a loop in a shell script is to use a for loop. The
for loop in a shell script allows you to loop over a list of items, and it
works in a completely different way to the for loop in languages like Java
and C. The syntax for the for loop is as follows:
  

for VARIABLE in LIST; do
  BODY OF LOOP
  .
  .
  .
done

When a for loop is executed, on the first iteration through the for loop,
VARIABLE is the first item in the list. On the next iteration, VARIABLE is
the second item on the list, on the third iteration it is the third item,
etc..., until the end of the list is reached. When the end of the list is
reached, the loop is finished and we continue executing from after the end
of the loop. One question you may have at this point is "How do I make a
list?". In shell scripts, any variable with spaces in it is a list, for
example, in the code fragment below, NAMES is a list with three variables,
where 'John' is the first element of NAMES, 'Bill' is the second element and
'Fred' is the third.
  

NAMES="John Bill Fred" # A list with three elements

A simple example of how a for loop might be used is shown below.
  

#!/bin/sh

KEYBOARD="Q W E R T Y U I O P A S D F G H J K L Z X C V B N M" # Letters
from a QWERTY keyboard

COUNT=1
for LETTER in $KEYBOARD; do
  printf "Letter number $COUNT is $LETTER\n"
  COUNT=`expr $COUNT + 1`
done

In this example, KEYBOARD is a list which contains all of the letters on
QWERTY keyboard. The for loop is used to loop over each item in this list,
and while we are inside the loop, the variable LETTER contains the current
list item.

The for loop can be a very powerful tool when used in conjunction with ls.
ls can provide you with a list of filenames matching a regular expression.
Some examples of how you might use this include:

* ls *.java - Lists all files in the current directory which end in
'.java' (excluding those which start with a '.').
* ls * - Lists all files in the current directory (excluding those
which start with a '.').
* ls .* - Lists all files in the current directory which start with a
dot. These are the so called hidden files.
* ls ??? - Lists all files with three letter names.

An example of how you might use this shown below. This script writes out the
name of all files in the current directory which end in .c (C source files),
.a (C Archive files) and .o (C Object files).
  

#!/bin/sh

printf "C files in the current directory are:\n"
COUNT=0

for FILENAME in `ls *.c *.a *.o 2>&-`; do # If ls gives an error message,
send it to null.
  printf "${FILENAME}\n"
  COUNT=`expr $COUNT + 1`
done

printf "\nCounted $COUNT C files\n"

  _____

TheCaseStatement
The case Statement

The final control structure available in a Unix script is the case
statement. This is similiar to the switch statement in Java and C, and it
allows you to choose between multiple paths depending upon the value of a
variable. The syntax for a case statement is:
  

case VARIABLE in
OPTION1)
  DO THIS
  ;;
OPTION2)
  DO THIS
  ;;
OPTION3)
.
.
.
esac # esac = case spelled backwards

When a case statement is executed, the variable is compared to each of the
options in order. If the variable matches one of the options, the code after
the option is executed, up until the two semicolons. Once the two semicolons
are reached, the case statement is completed and we continue from after
esac. If two options are found which match the variable, only the first of
these is executed. If none of the options match, no code is executed inside
the case statement.

Below is an example of how a case statement can be used. This script, like a
Magic 8-Ball, will produce an answer to a yes or no question. The random
number is generated from the Process ID.
  

#!/bin/sh

printf "Ask the Magic 8-Ball a yes or no question out loud...\n"
sleep 8 # Wait for 8 seconds

printf "Shaking the eight ball...\n"
sleep 2

case `expr $$ % 6` in # $$ is virtually a random number, $$ mod 6 is a
random number from 0 to 5 inclusive
0)
  printf "All signs point to yes\n"
  ;;
1)
  printf "Looks unlikely\n"
  ;;
2)
  printf "Vision is a little hazy, try again\n"
  ;;
3)
  printf "A definate maybe\n"
  ;;
4)
  printf "It looks doubtful\n"
  ;;
5)
  printf "I think so\n"
  ;;
esac

An interesting feature of case statements is that you can use wildcards like
? and * in the options. This next example is an edited version of the script
from 'The for Loop' which, as well as writing out all of the C files, writes
a comment next to them about what type of file they are.
  

#!/bin/sh

printf "C files in the current directory are:\n"
COUNT=0

for FILENAME in `ls *.c *.a *.o 2>&-`; do # If ls gives an error message,
send it to null.
  printf "${FILENAME} "

  case $FILENAME in
  *.c)
    printf "C Source code file\n"
    ;;
  *.o)
    printf "C Object file\n"
    ;;
  *.a)
    printf "C Library file\n"
    ;;
  esac

  COUNT=`expr $COUNT + 1`
done

printf "\nCounted $COUNT C files\n"

  _____

UnixRedirectsInDetail
Unix Redirects In Detail

We have already had a brief look at how we can use pipes, redirect output in
Unix scripts, and how we can supress error messages using a command like
2>&-. Now we will take a deeper look at how streams can be redirected.

In Unix, each process has 3 file descriptors. These are:

* Standard input (stdin) - File descriptor 0 - This file descriptor is
used to provide input to a program. By default, anything the user types into
the terminal sent to the program via this file descriptor.
* Standard output (stdout) - File descriptor 1 - This file is where a
program will place its output. Normally, anything sent to this file is
placed on the users terminal.
* Standard error (stderr) - File descriptor 2 - This is the file where
error messages are placed. Like standard output, this is sent to the
terminal by default.

While all of these files default to the user's terminal, it is possible to
redirect them to other places. Some ways of doing this include:

* > filename - Redirects the standard output to a file. Anything
output which would normally appear on the screen is written to the file
instead. This will overwrite what was previously in the file.
* 2> filename - Redirects standard error to a file, overwriting the
previous contents of the file.
* < filename - Sets standard input to come from a file rather than
from the terminal. This can be very useful when making automated testing
code, because you don't need to type all of the instructions in each time,
just write them to a file and use the file as input for the program
* >> filename - Redirects the standard output to be appended to the
end of the file. This will not overwrite what was previously in the file.
* << WORD - Standard input is set to come from the script itself,
until WORD is read on a line by itself.
* command1 | command2 - Standard output from command1 is sent to
standard input for command2. We have already seen examples of how this
works.

Below is an example whch demonstrates how redirecting input and output can
be used. This is a script which tells the user how many times it has been
executed by keeping a counter in a file.
  

#!/bin/sh

COUNTFILE=$HOME/.countfile # This file contains the number of times
the script has been executed.

if [ ! -r $COUNTFILE ] ; then # If the count file doesn't exist, the
script has never been run before,
  NUM_TIMES=1 # so this is the first execution of the
script.
else
  NUM_TIMES=`cat < $COUNTFILE` # cat < $COUNTFILE will write out the
contents of the file $COUNTFILE
  NUM_TIMES=`expr $NUM_TIMES + 1`
fi

printf "$NUM_TIMES\n" > $COUNTFILE # Updating $COUNTFILE

printf "This script has been executed $NUM_TIMES times\n"

The next example demonstrates how we can use << to redirect input to come
from the script itself. This seems quite strange because it has C code
written directly into a Unix script, but it is valid because the C code is
being used as input to another program.
  

#!/bin/sh

SRC=/tmp/temp-$$.c
EXE=/tmp/temp-$$

cat > $SRC << END_C_CODE # The script itself is used as input to cat,
until the line END_C_CODE is reached
int main()
{
    printf("Hello!!!");
    return 0;
}
END_C_CODE

gcc -o $EXE $SRC

$EXE

rm $EXE $SRC 2>&-

Some other useful redirection commands include:

* <&n - Sets file descriptor n to be used as standard input. The file
descriptor can be any number, it doesn't have to be 0, 1 or 2
* >&n - Redirects output to file descriptor n.
* <&- - Closes standard input.
* >&- - Closes standard output. Anything sent to standard output will
be ignored.
* n>&- - Closes file descriptor n. You have already seen this used
when we write 2>&- to close standard error.

  _____

ConditionalExecution
Conditional Execution

In shell scripts it is possible to use the logical operators && and || to
group together commands. These logical operators also allow us to execute
commands based on exit values much like an if statement. These logical
operarors work as follows:

* comamnd1 && command2 - Executes command1, and if this worked
correctly (exit status of 0), executes command2. The exit status of the
whole group of commands is the exit status of the last command executed. If
command1 failed, command2 isn't executed because it is not possible for the
whole expression to evaluate to true.
* command1 || command2 - Executes command1, and if this failed,
executes command2. The exit status of the whole group of commands is the
exit status of the last command executed.

One use for these commands is to link together conditions in an if
statement, for example:
  

#!/bin/sh

if [ 5 -eq $1 ] || [ $1 -gt 7 ] ; then # $1 must be either 5 or greater than
7.
  printf "Good choice.\n"
fi

But another way to use them is as a replacement for the if statement.
  

#!/bin/sh
# Tells the user if $1 is a readable file

[ -r $1 ] && printf "$1 is a readable file.\n" || printf "$1 is not a
readable file. That's a shame...\n"

If the file $1 is readable, then [ -r $1 ] returns true. This means that the
part after the && must be executed. If the file wasn't readable, the test
will return false, so the part after the || must be executed. This piece of
code is equivalent to an if-then-else statement.

  _____

VariablesInDetail
Variables In Detail

We already know how to create and use variables, but there are still some
aspects of them which we haven't covered. The first one we will look at are
variable constructs. These provide different ways of accessing a variable.
The commands we can use are:

* ${var:-expression} - If var is not null, equal to the value of the
variable, otherwise it is the expression.
* ${var:=expression} - Set var to be equal to expression unless var
already has a value.
* ${var:?expression} - If var is null, print expression to standard
error and exit.
* ${#var} - Returns the number of characters in the variable.

These variable constructs can help to make code much shorter and often
remove the need to cumbersome if statements. To see how they work, type in
and execute the following example.
  

#!/bin/sh

MY_VAR="Value"

printf "MY_VAR = ${MY_VAR}\n" # Write out MY_VAR

printf "NEW_VAR = ${NEW_VAR:-New}\n" # NEW_VAR doesn't exist yet,
should write New

printf "NEW_VAR = ${NEW_VAR:=Variable}\n" # Create NEW_VAR

printf "NEW_VAR = ${NEW_VAR:-New}\n" # NEW_VAR does exist now,
should write Variable
printf "NEW_VAR = ${NEW_VAR}\n" # See that it has been set to
Variable

printf "Checking if MY_VAR exists..."
printf "${MY_VAR:?No\n}" # MY_VAR does exist...
printf "It does exist\n"

printf "Checking if SOME_VAR exists..."
printf "${SOME_VAR:?No\n}" # SOME_VAR doesn't exist,
should print No and exit
printf "It does exist\n" # We won't get to this line

Another interesting feature of variables is the ability to export them. When
a variable is exported, any process started by the shell in which the script
is running can also see the variable. Consider the following two scripts:
  

#!/bin/sh
# script1

VAR=abc
script2

#!/bin/sh
# script2

printf "$VAR\n"

When we run script1, it creates a variable VAR and then executes script2.
Even though script2 is called from inside script1, it cannot read the
variables in script1, but if we alter script1 so that it exports the
variable, script2 will be able to see it, and we will get abc written to
standard output.
  

#!/bin/sh
# script1

VAR=abc
export VAR # Exporting the variable, now script2 can see it
script2

#!/bin/sh
# script2

printf "$VAR\n"

The final thing we will look at in variables is the difference between
single and double quotes. When a variable is written inside double quotes,
it is replaced with its value, but when a variable is written in single
quotes, it is not replaced. To get an idea of how this works, type in and
execute this script.
  

#!/bin/sh

MY_VAR="Value"

printf "Variable replacement...\n\n"
printf "Using double quotes : $MY_VAR \n"
printf 'Using single quotes : $MY_VAR \n'

printf "\n\nUsing backticks...\n\n"
printf "Using double quotes : `echo HELLO`\n"
printf 'Using single quotes : `echo HELLO`\n'

You will see that when something is written in single quotes, Unix doesn't
replace commands in backticks or variables. It will only do the replacement
when it is in double quotes.

  _____

UsefulCommands
Useful Commands

There are a number of commands and built-in functions which often prove
useful when writing a Unix script. Here we will look at a few of these
commands, describe what they do, and give examples of how they can be used.

* exit n
Finishes the current script with a given exit status. If the number isn't
provided, the script finishes with an exit status of 0.
  
* sleep n
Pause for n seconds. Often useful when you want to make the user stop and
read a message.
  
* read var
Pauses the script and waits for a line to be typed into standard input. The
variable var is set to be whatever was typed in. Used to get input from a
user, for example:
  

        #!/bin/sh

        printf "What is your name? "

        read NAME # Reads the next line from standard input
and sets NAME to be this value.

        printf "Hello $NAME\n"

          
* head -n filename
Writes the first n lines of the file to standard output.
  
* tail -n filename
Writes the last n lines of the file to standard output.
  
* dirname string
If the string is the path of a file, dirname will write the directory of the
file to standard output. For example, if the string is "usr/local/bin/perl",
dirname will output "usr/local/bin".
  
* basename string [ suffix ]
If the string is the path of a file, basename writes out the filename
without the directory. If the optional suffix is included, this is also
remove from the file. An example of how you can use basename is given below.

  

        #!/bin/sh

        printf "Enter a java file's path (must end in .java): "
read NAME

        printf "The class in this file is `basename $NAME .java`\n"

          
* shift n
When a script starts, you can only access the first 9 command line arguments
through $1, $2, ..., $9. To access arguments after the ninth one, we need to
use shift. Shift moves all of the command line arguments down n places, For
example, if we write shift 5, the value which was in $6 will now be in $1,
and the value now in $6 will be the eleventh command line argument.
  
* cut -d delimiter -f fieldnumbers filename
The cut command allows you to write out certain parts of a file. If the file
is a text file with fields seperated by a delimiter, we can get cut to write
out specific fields. For example, have a look at the file /etc/passwd. This
file contains information about all users names, logins, group numbers, home
directories and other assorted information. Each user has one line in the
file, and each of the fields in the file is seperated by a colon. If we
wanted to print all of the logins (The first field in the file) and names
(The fifth field), but none of the other information, we could use cut, as
is done in the script below:
  

        #!/bin/sh

        cut -d : -f 1,5 /etc/passwd

          
* grep pattern filenames
grep allows you to search through a file and write out any lines which match
a given regular expression. For example, if we wanted to see all of the
lines in a C file which call the sin function, we could run grep with a
command like 'grep 'sin(' file.c'. It is also possible to use wildcards in
the patterns, and to see how this can be done, consult grep's man page.

  _____

Please enter the directory where you want Appln to be installed
/tmp
This is the value of i : {Appln}
This is the value of Applin : /tmp

root@primo [/tmp]
#

Mahajan Sachin wrote:

Hi Admins,

I am trying to write a script with compounded variables....ie my variable
name is also a variabl...but it does'nt seem to work.

Could someone point out the mistake or give me an alternative way of doing
it ?

for i in Appln
do

        echo
        echo
        echo "Please enter the directory where you want $i to be installed"
        read "$i"
        echo $($i)
        echo $Appln

done

echo $Appln gives me the right input variable
whereas echo $($i) does'nt !!!

This is perplexing....

Thanks in advance...
Sachin.

=========================================================
This electronic message contains information from the mmO2 plc Group
which may be privileged or confidential. The information is intended to be
for the use of the individual(s) or entity named above. If you are not the
intended recipient be aware that any disclosure, copying, distribution or
use of the contents of this information is prohibited. If you have received
this electronic message in error, please notify us by telephone or email
(to the numbers or address above) immediately.
=========================================================


attached mail follows:


Sachin:
 
You need to take two passes at the variable to get what you want.
 
AA=one
BB=two
CC=three
 
so I now have three variables, AA, BB and CC.
 
echo $AA
 
returns
 
one
 
K=AA
 
echo $K
 
returns
 
AA
 
but we want the contents of the variable AA, so we say
 
eval echo \$$K
 
which returns
 
one.
 
The first pass of eval echo \$$K is "$AA", the second pass interprets $AA.
The backslash "hides" the first dollar-sign from the first pass.
 
But there is a better way using shell arrays.
 
Lets say that you have applications AA BB and CC
 
    set +A apps AA BB CC
    i=0
    while (( I < ${#apps[@]} ))
    do
        echo "Please enter the directory where you want ${apps[$i]} to be
installed:"
        read dirs[$i]
        echo "${apps[$i]} will be installed in ${dirs[$i]}
        (( i += 1))
    done
 
Now, for each entry in the array apps, you have a corresponding array entry
in the array dirs.
 
Enjoy,
-dlt-

-----Original Message-----
From: Mahajan Sachin [mailto:Sachin.Mahajan@o2.com]
Sent: Friday, November 08, 2002 5:14 AM
To: HP UX NETHERLANDS (E-mail)
Subject: [HPADM] Compound variable

Hi Admins,

I am trying to write a script with compounded variables....ie my variable
name is also a variabl...but it does'nt seem to work.

Could someone point out the mistake or give me an alternative way of doing
it ?

for i in Appln
do

        echo
        echo
        echo "Please enter the directory where you want $i to be installed"
        read "$i"
        echo $($i)
        echo $Appln

done

echo $Appln gives me the right input variable
whereas echo $($i) does'nt !!!

This is perplexing....

Thanks in advance...
Sachin.

=========================================================
This electronic message contains information from the mmO2 plc Group
which may be privileged or confidential. The information is intended to be
for the use of the individual(s) or entity named above. If you are not the
intended recipient be aware that any disclosure, copying, distribution or
use of the contents of this information is prohibited. If you have received
this electronic message in error, please notify us by telephone or email
(to the numbers or address above) immediately.
=========================================================

--
             ---> Please post QUESTIONS and SUMMARIES only!! <---
        To subscribe/unsubscribe to this list, contact majordomo@dutchworks.nl
       Name: hpux-admin@dutchworks.nl     Owner: owner-hpux-admin@dutchworks.nl
 
 Archives:  ftp.dutchworks.nl:/pub/digests/hpux-admin       (FTP, browse only)
            http://www.dutchworks.nl/htbin/hpsysadmin   (Web, browse & search)


This archive was generated by hypermail 2.1.7 : Sat Apr 12 2008 - 11:02:22 EDT