Bash scripting worries

Kevin Annies anniesk at coventry.ac.uk
Sun Apr 1 01:51:43 PDT 2007


Thanks for the tips, what you advised worked perfect.

However, I now have a slightly different issue though within the same script.

With your help, I modified the following lines accordingly

PARTITION=$((PARTITION-1))
export NODE="$GRUB,$PARTITION"
echo $NODE

I now wish to use the $NODE variable in a sed command to modify the word 'DRIVE' in menu.lst,
I used the following command:
sed "s/DRIVE/$NODE/" /etc/install/lfsmenu.lst > /home/TEMP/boot/grub/menu.lst

******************************
# The first entry is for LFS.
title Coventry University Linux
root (DRIVE)
kernel /boot/lfskernel-2.6.16.27 root=/dev/ROOT
***************************************

I used a similar command to modify the last word 'ROOT' and it works fine. However DRIVE refuses to change and I have a suspicious feeling its because of the brackets it's enclosed by. What regular expression techniques would you suggest I use to get round this?

Regards
Kevin

ps - ttp://tldp.org/LDP/abs/html is a superb link!
--------------------------------------------------------------------

   5. Bash scripting worries (Kevin Annies)
   6. Re: Bash scripting worries (Simon Geard)
    8. Re: Bash scripting worries (Matthias Feichtinger)
   9. Re: Bash scripting worries (anirudh vij)
  

----------------------------------------------------------------------



------------------------------

Message: 5
Date: Fri, 30 Mar 2007 09:49:59 +0100
From: "Kevin Annies" <anniesk at coventry.ac.uk>
Subject: Bash scripting worries
To: <blfs-support at linuxfromscratch.org>
Message-ID:
	<2207C7BCFE7BEF40BE8DD833AF5455105E51A0 at VSEXMS-03.coventry.ac.uk>
Content-Type: text/plain; charset="iso-8859-1"

Hi,

I am trying to convert grub hard drive definitions (as part of an installation script)

There a several issues. Below is a section of the code:
----------------------------------------------------------
echo "please enter the root partition drive ie; hda, hdb, sda,sda"
read DISK

if [ $DISK = "hda" ]
        then
        export GRUB=hd0
elif [ $DISK = "hdb" ]
        then
        export GRUB=hd1
elif  [ $DISK = "sda" ]
        then
        export X=2
        if [ -f /dev/hdb ]
        then
                $X = $X - 1
        fi

        export GRUB=hd$X
else
        export X=3
        if [ -f /dev/hdb ]
        then
                $X = $X - 1
        fi

        export GRUB=hd$X
fi

echo $GRUB
echo "please enter the partition number without the prefix ie; instead of hdb4, just type 4"
read PARTITION

$PARTITION = $PARTITION - 1              #line 130
export NODE = "$GRUB,$PARTITION"         #line 131
echo $NODE

----------------------------------------
when executed I recieve this output:
--------------------------------------------------------
please enter the root partition drive ie; hda, hdb, sda,sda
sda
hd2
please enter the partition number without the prefix ie; instead of hdb4, just type 4
3
./INSTALLATION: line 130: 3: command not found
./INSTALLATION: line 131: export: `=': not a valid identifier
./INSTALLATION: line 131: export: `hd2,3': not a valid identifier
--------------------------------------------------------------------
ERROR 1 - I suspect this has something to do with 'read' capturing the input value as a string rather than a number. I tried 'read' with the 'n' option but no avail.

ERROR 2 & 3 - I suspect this has something to do with the Unix quoting mechanisms. I tried nearly all combinations but still no cigar.

Any advice on how to solve these issues is appreciated.

Also if anyone knows a good reference on Arithmetic in Shell script please speak up. I am using 'Linux Programming 3rd Edition' Mathews and Stones (which is one thick book!) and there seems to be but a paragraph on aritmetic in bash.

Thanks in advance

Kevin Annies
 
 
--------------------------------------------------------

NOTICE

This message and any files transmitted with it is intended for the addressee only and may contain information that is confidential or privileged. Unauthorised use is strictly prohibited. If you are not the addressee, you should not read, copy, disclose or otherwise use this message, except for the purpose of delivery to the addressee. 

Any views or opinions expressed within this e-mail are those of the author and do not necessarily represent those of Coventry University.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://linuxfromscratch.org/pipermail/blfs-support/attachments/20070330/f2cf0ac2/attachment-0001.html 

------------------------------

Message: 6
Date: Fri, 30 Mar 2007 21:40:37 +1200
From: Simon Geard <delgarde at ihug.co.nz>
Subject: Re: Bash scripting worries
To: BLFS Support List <blfs-support at linuxfromscratch.org>
Message-ID: <1175247638.23866.12.camel at aiglos.home>
Content-Type: text/plain; charset="us-ascii"

On Fri, 2007-03-30 at 09:49 +0100, Kevin Annies wrote:
> Hi,
> 
> I am trying to convert grub hard drive definitions (as part of an
> installation script)
> 
> There a several issues. Below is a section of the code:
> ----------------------------------------------------------
> echo "please enter the root partition drive ie; hda, hdb, sda,sda"
> read DISK
> 
> if [ $DISK = "hda" ]
>         then
>         export GRUB=hd0
> elif [ $DISK = "hdb" ]
>         then
>         export GRUB=hd1
> elif  [ $DISK = "sda" ]
>         then
>         export X=2
>         if [ -f /dev/hdb ]
>         then
>                 $X = $X - 1
>         fi
> 
>         export GRUB=hd$X
> else
>         export X=3
>         if [ -f /dev/hdb ]
>         then
>                 $X = $X - 1
>         fi
> 
>         export GRUB=hd$X
> fi
> 
> echo $GRUB
> echo "please enter the partition number without the prefix ie; instead
> of hdb4, just type 4"
> read PARTITION
> 
> $PARTITION = $PARTITION - 1              #line 130
> export NODE = "$GRUB,$PARTITION"         #line 131
> echo $NODE
> 
> ----------------------------------------
> when executed I recieve this output:
> --------------------------------------------------------
> please enter the root partition drive ie; hda, hdb, sda,sda
> sda
> hd2
> please enter the partition number without the prefix ie; instead of
> hdb4, just type 4
> 3
> ./INSTALLATION: line 130: 3: command not found
> ./INSTALLATION: line 131: export: `=': not a valid identifier
> ./INSTALLATION: line 131: export: `hd2,3': not a valid identifier

Easy fix. Repeatedly, you've used commands like "$X = $X -1". This won't
work, since by prefixing the first X with the $ sign, you're evaluating
it, and trying to set the result to something else. Also, you can't do
subtraction quite that easily, since all BASH sees in this case is text.

What you actually want is something like:
	X=$((X-1))
or
	PARTITION=$((PARTITION-1))

Note three things. a) The value on the left doesn't have a $ sign in
front of it. b), there's no whitespace on either side of the = sign. And
c), the use of $((...)) marks, which tells bash that the contents should
be evaluated as a math expression.

That covers the problems on line 130. The ones on 131 are probably for
the second reason - i.e the spaces around the = sign. It needs to
instead be:
	export NODE="$GRUB,$PARTITION"

The reason for this is that each parameter to export must be either
simply the name of an existing variable, or an assignment. By putting
spaces in, you're splitting the latter into three instances of the
former, so it tries to export variables called 'NODE', '=', and 'hd2,3'.

For tips on bash scripting, try the following links:

* Advanced Bash Scripting Guide - http://tldp.org/LDP/abs/html
* Wikipedia page - http://en.wikipedia.org/wiki/Bash

Simon.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : http://linuxfromscratch.org/pipermail/blfs-support/attachments/20070330/863f6595/attachment-0001.bin 



------------------------------

Message: 8
Date: Fri, 30 Mar 2007 13:59:48 +0200
From: Matthias Feichtinger <asdf at vienna.at>
Subject: Re: Bash scripting worries
To: BLFS Support List <blfs-support at linuxfromscratch.org>
Message-ID: <20070330115948.GD1928 at comp10.at>
Content-Type: text/plain; charset=iso-8859-15

Am Fri, Mar 30, 2007 at 09:40:37PM +1200 schrieb Simon Geard in <1175247638.23866.12.camel at aiglos.home>:
> On Fri, 2007-03-30 at 09:49 +0100, Kevin Annies wrote:
> 
> For tips on bash scripting, try the following links:
> 
> * Advanced Bash Scripting Guide - http://tldp.org/LDP/abs/html
> * Wikipedia page - http://en.wikipedia.org/wiki/Bash
 
As I am concerned, I won't recommend Advances Bash thingies.
Nevertheless it comes with some interesting bash scripts
most prepared quite well. So I am filling the scripts
in an extra folder to be looked through with grep,
on another terminal I have a complete look at the script
and on a third I read man bash. This helped me much more.
Very much more, because the explanations in this "Guide"
are curious. At least!

Adieu
Matthias

-- 
http://linuxfromscratch.org/mailman/listinfo/blfs-support
FAQ: http://www.linuxfromscratch.org/blfs/faq.html
Unsubscribe: See the above information page


------------------------------

Message: 9
Date: Fri, 30 Mar 2007 17:52:08 +0530
From: "anirudh vij" <anirudhvij at gmail.com>
Subject: Re: Bash scripting worries
To: "Matthias Feichtinger" <asdf at vienna.at>, 	"BLFS Support List"
	<blfs-support at linuxfromscratch.org>
Message-ID:
	<1aa72c450703300522o52bcceb6g22168f02ba24ec3e at mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

>the explanations in this "Guide"
are curious
hmmm....cant say i agree.
maybe you would like to try "Learning the bash shell" from o'reilly.
if you want a pdf version,go to pdfchm.com,register and download.
its pretty exhaustive,yet understandable.

On 3/30/07, Matthias Feichtinger <asdf at vienna.at> wrote:
> Am Fri, Mar 30, 2007 at 09:40:37PM +1200 schrieb Simon Geard in
> <1175247638.23866.12.camel at aiglos.home>:
> > On Fri, 2007-03-30 at 09:49 +0100, Kevin Annies wrote:
> >
> > For tips on bash scripting, try the following links:
> >
> > * Advanced Bash Scripting Guide - http://tldp.org/LDP/abs/html
> > * Wikipedia page - http://en.wikipedia.org/wiki/Bash
>
> As I am concerned, I won't recommend Advances Bash thingies.
> Nevertheless it comes with some interesting bash scripts
> most prepared quite well. So I am filling the scripts
> in an extra folder to be looked through with grep,
> on another terminal I have a complete look at the script
> and on a third I read man bash. This helped me much more.
> Very much more, because the explanations in this "Guide"
> are curious. At least!
>
> Adieu
> Matthias
>
> --
> http://linuxfromscratch.org/mailman/listinfo/blfs-support
> FAQ: http://www.linuxfromscratch.org/blfs/faq.html
> Unsubscribe: See the above information page
> --
> http://linuxfromscratch.org/mailman/listinfo/blfs-support
> FAQ: http://www.linuxfromscratch.org/blfs/faq.html
> Unsubscribe: See the above information page
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: winmail.dat
Type: application/ms-tnef
Size: 7586 bytes
Desc: not available
URL: <http://lists.linuxfromscratch.org/pipermail/blfs-support/attachments/20070401/b67f6882/attachment.bin>


More information about the blfs-support mailing list