LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 03-22-2005, 08:25 AM   #1
vous
Member
 
Registered: Mar 2003
Location: Macondo
Distribution: Mandrake 9.1, 10.1, SuSE 8.1 pro, 10.1, Red Hat 8.0/9.0
Posts: 380

Rep: Reputation: 30
How do I read from a file in BASH?


Hello all,

I'm trying to do this (which works fine):


#!/usr/bin/bash

LIST="Jupiter Venus Mercury"
n=1
for planet in $LIST
echo $planet
echo $n
let "n += 1"

done

But instead of doing it from the list I provide: LIST="Jupiter Venus Mercury"
I would like it to read from a file that has a complete list.

What do I have to change so it reads from a file in BASH?

Last edited by vous; 03-22-2005 at 08:45 AM.
 
Old 03-22-2005, 08:48 AM   #2
trevelluk
Member
 
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388

Rep: Reputation: 32
Change the first line to LIST=`cat planets.txt`

Where planets.txt contains:

Jupiter Venus Mercury Mars...

This'll also work with each planet on a new line.
 
Old 03-22-2005, 08:52 AM   #3
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
Using command substitution, you can easily loop through the contents of a file. If the file will be more than one line, the only thing different you'll need to do is pipe the output of the file through tr and replace all newlines with spaces.

This will only work, though, if your file does not contain words that have spaces in them.

Code:
filename=./planetlist.txt
for planet in `cat "$filename" | tr '\n' ' '`; do
  echo $planet
done
 
Old 03-22-2005, 09:38 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
Or simpler:
Code:
filename=./planetlist.txt
for planet in $(<$filename)
do
  echo $planet
done
 
Old 03-22-2005, 07:51 PM   #5
bahadur
Member
 
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141

Rep: Reputation: 15
files can be easily read using the head command

check the man pages for the command head.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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: read a line from file zokik Linux - General 6 12-10-2008 10:24 AM
Read a line in a txt file with bash orgazmo Programming 5 05-03-2005 08:10 AM
Read a line in a txt file with bash orgazmo Linux - Newbie 3 05-03-2005 05:16 AM
bash scripting read from file cadj Programming 2 02-29-2004 11:42 PM
bash - read from file question sqn Programming 4 04-01-2003 05:30 AM


All times are GMT -5. The time now is 12:23 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration