LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-10-2011, 07:38 PM   #1
AndrewJS
LQ Newbie
 
Registered: Dec 2010
Posts: 16

Rep: Reputation: 0
reading a value from a textfile using bash script (beginner problem)


I have a text file that contains a single word and I want to write a bash script that will read the word from the text file... The following is my incorrect attempt, as it assigns the name of the textfile to the variable as opposed to the word stored within the textfile:


(assume I have a text file value.txt that has its contents a single word, say wordone)

Code:
#!/bin/sh
for f in value.txt
do
    echo $f
done
so the output of the above script is value.txt, however I want it to be wordone.
to summarise: how do I assign the value of the word contained within a textfile to a variable?

Last edited by Tinkster; 06-10-2011 at 09:35 PM.
 
Old 06-10-2011, 08:16 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
As you've discovered, a for loop takes the values following "in" as literal strings, broken up into words according to your IFS settings (default space/tab/newline). You have to use some kind of substitution to generate a list of values that go there if you want to use it.
Code:
for f in $(<value.txt) ; do
  echo "$f"
done
But when taking the values from a text file, a while+read loop is usually better, because read takes in a line at a time, and word-breaking isn't performed. You also don't need to use command substitution, as simple file redirection will work.
Code:
while read f ; do
  echo "$f"
done <value.txt
PS: Please use [code][/code] tags around your code, to preserve formatting and to improve readability.
 
  


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
Writing to and reading from a socket from bash script. vouser Linux - Networking 2 03-12-2010 11:07 PM
Beginner question, Bash-script - date kickarzt Programming 15 03-10-2010 01:18 PM
interchanging lines in a textfile in bash script neo2k Linux - Newbie 5 07-28-2009 12:48 PM
reading data from textfile in java? hazeposse Linux - Software 2 07-22-2006 07:00 AM
reading file, bash script marri Programming 3 11-15-2004 10:13 AM


All times are GMT -5. The time now is 10:38 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration