LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-18-2005, 04:19 AM   #1
fnoyan
LQ Newbie
 
Registered: Sep 2004
Posts: 23

Rep: Reputation: 15
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....
 
Old 01-18-2005, 07:35 AM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
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

Last edited by homey; 01-18-2005 at 08:33 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash scripting help (su ...) shwong Linux - General 1 11-02-2005 12:26 PM
Bash scripting pete1234 Programming 1 09-27-2005 01:48 AM
bash scripting vadon Linux - Newbie 6 05-10-2005 04:07 AM
need help with bash scripting rich2oo1 Programming 2 12-17-2003 12:50 PM
HELP with BASH scripting atwah Linux - Newbie 6 09-09-2003 01:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration