Click to See Complete Forum and Search --> : bash - passing variables


swangods
08-03-2003, 07:37 PM
So I'm trying to make a backup script - who doesn't? I actually made one that was quite functional, with the intention being to only backup a select list of directories. As a "feature upgrade" I wanted the ability to store my sources in an external text file, rather than code them in the script. I was using an array, with each element holding the name of a location. Very functional, but not easily extensible. So how about files?

Well, run into a slight problem. I am using cat and awk to pull data from the textfile, but am running into a bit of a problem - when looping through the data it's not passing the loop counter quite right.

First things - my textfile is called bexlist and contains (for the purpose of this example) two columns - one is a counter for the item to backup and the second is the actual directory. The columns are tab-delimited. For example:
1 /pub/src
2 /pub/docs
3 /pub/mp3

Here's the snippet of code that would normally read the textfile into variables, one representing the counter field and one representing the source dir.


1 #!/bin/bash
2
3 BEXLIST=./bexlist
4
5 bexCount=$(cat $BEXLIST | awk '{ print $1 }')
6 bexSource=$(cat $BEXLIST | awk '{ print $2 }')
7
8 for x in $bexCount
9
10 do
11
12 echo "when x equals $x:"
13 echo $bexSource
14 bexDir=$(echo $bexSource | awk '{ print $x }')
15 echo $bexDir
16
17 bexDir=$(echo $bexSource | awk '{ print $1 }')
18 echo $bexDir
19
20 bexDir=$(echo $bexSource | awk '{ print $2 }')
21 echo $bexDir
22
23 echo
24 done


Most of the echos are just to demonstrate what works and what doesn't.

x is the generic looper - $bexCount reads a 1 2 3 and the "for $x in $bexCount" does loop through them, as evidenced by line 12. Also, $bexSource being piped into awk does reveal the correct column when specifying the column - ie '{ print $1 }'.

However, it's line 14 that bugs me. For some reason it's not recognizing $x as the same x being used by the loop and, for example, line 12. By encapsulating the awk inside a $( ) it appears to stop recognizing $x as the same variable. I've even tried something like:


$bexDir=$(awk '{print $x}' $BEXLIST)


and once again it doesn't understand that $x equals 1, 2, or 3.

So in a nutshell, how can I make sure $x is being passed to that command??

I'm probably missing something basic about encapsulating commands - any help would be appreciated! Thanks!

terribleRobbo
08-04-2003, 04:03 AM
Umm... I think you can do it by:

awk '{print "'$((x))'"}'

(that's a " followed by a ', then variable, then ' then " :D )

(Edit - Clarification for those who don't get it: You're seperating the one string ( {print "foo"} ) into two strings ( { print " and } ) and putting the value of a variable in-between.)

phlipant
08-04-2003, 05:04 AM
for a simple loop, wouldn`t this be easier?

#!/bin/bash

BEXLIST=./bexlist

while
read bexCount bexSource
do

echo $bexCount $bexSource

done < $BEXLIST

swangods
08-04-2003, 07:51 AM
Thanks for both responses - they both work great!

Phlipant - I think I may go with your method if only for brevity. I wasn't familiar with the read, done < (input file) method.

Thanks again!