LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash scripting (https://www.linuxquestions.org/questions/programming-9/bash-scripting-279199/)

fnoyan 01-18-2005 04:19 AM

bash scripting
 
Hi
I am trying to write a script that assigns each line of a file to a shell array variable. The content of file is obtained with command below
(as root)
# fdisk -l | awk '/W95/ {print $1"\t\t"$7"\t\t"$8}' > anyfile
# export NUMLINES=`cat anyfile | wc -l` <- I got the number of lines
And here is the file content
/dev/hda1 W95 FAT32
/dev/hda3 Ext'd (LBA)
/dev/hda7 FAT32
/dev/hda8 FAT32
Which shows the my FAT partitons, Now, what I want to do is to assing each line to a shell array variable, like
$VAR[0] becomes /dev/hda1 W95 FAT32
$VAR[1] becomes /dev/hda3 Ext'd (LBA)
and so on.....
Is this possible, if so how?

Thanks for your advice....

homey 01-18-2005 07:35 AM

I'm kinda partial to scriptable fdisk ( sfdisk ) so I used that to make file.txt
Then, the example uses ( var=`echo "var${n}"` ) to give the variable a number .....
Code:

#!/bin/bash

#sfdisk -d /dev/hda > file.txt
n=0
a=file.txt
cat ${a} |grep "start" |awk -F= '{print$4}' |\
while read i; do
let n=$n+1
var=`echo "var${n}"`
echo "$var is .. type ${i}"

done



All times are GMT -5. The time now is 06:52 PM.