[ale] OT: Bash Question
Asif
asife at mindspring.com
Wed Apr 3 12:25:05 EST 2002
The following works for me ( both for even,odd and 0-numbered list in the list
file )
#!/bin/bash
file=$1
count=`wc -l $file | awk '{ printf "%i", $1/2 }'`
i=0;
for item in `cat $file`
do
if [ $i -lt $count ]
then
i=`expr $i + 1`
echo $i $item
fi
done
Hope it helps.
Asif
Jeff Layton wrote:
> Sorry about the OT, but I didn't know where else to turn.
>
> I'm writing a simple shell script that cats a file to a
> variable (the file is just a list). I want to loop over only
> HALF of that list. So I want something like this:
>
> NODES=$(cat node.txt)
>
> for i in ${NODES}/2
> do
> echo ${i}
> done
>
> Of course, this doesn't work.
> I've also tried "counting" the number of items in the
> list, dividing by 2, and using a while loop. The only thing
> is, I don't know how to access a particular element in
> the list,
>
> NODE_FILE="node.txt"
> NODES=$(cat node.txt)
>
> declare -i NP
> declare -i NP2
> NP=`wc -l $NODES | awk '{print $1}'`
> NP2=NP/2
>
> iloop=1
> while (( $iloop <= ${NP2} ))
> do
> # Here I want to echo the "iloop" element of $NODES
> let iloop+=1
> done
>
> I really appreciate any help anyone can give. I'm also
> pretty tolerant of comments about my scripting capabilities :)
>
> Thanks!
>
> Jeff Layton
>
> ---
> This message has been sent through the ALE general discussion list.
> See http://www.ale.org/mailing-lists.shtml for more info. Problems should be
> sent to listmaster at ale dot org.
---
This message has been sent through the ALE general discussion list.
See http://www.ale.org/mailing-lists.shtml for more info. Problems should be
sent to listmaster at ale dot org.
More information about the Ale
mailing list