LinuxQuestions.org
Visit Jeremy's Blog.
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-11-2009, 04:16 PM   #1
horacioemilio
Member
 
Registered: Dec 2007
Posts: 61

Rep: Reputation: 15
Strange error in bash


Hi,

I have the following text file;

....
Match database: score.txt
.....

I want to read with a bash script the "score.txt" and assign it to a variable. So i do;

#!/bin/bash

read l1 l2 scfile <<< $(cat text.txt | grep Match | grep database | grep txt )
echo $scfile
ls -l $scfile

with the echo command i get on screen;

score.txt

but with ls -l $scfile or another command I get errors like;

\r: No such file or directory

What could be the reason?
 
Old 06-11-2009, 06:08 PM   #2
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora, Lubuntu, FreeBSD
Posts: 3,930
Blog Entries: 5

Rep: Reputation: Disabled
\r may represent a carriage return (hex D) in this context. Perhaps you edited the file using Windows notepad or some such? (Just speculating.)

In any case, please explain exactly what it is you're trying to accomplish here. If all you're really trying to do is read a file into a variable, why not use something simpler like:

Code:
foo=$(cat file.txt)
 
Old 06-11-2009, 06:54 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
why make it so complex, in bash, just use the while read loop
Code:
while read line
do
 #do something
done  < file
$line will be your variable for each line while iterating
 
Old 06-12-2009, 03:54 AM   #4
horacioemilio
Member
 
Registered: Dec 2007
Posts: 61

Original Poster
Rep: Reputation: 15
thanks for the answer

just one silly question,

using your procedure, each line can be;

10 20 score1.txt
10 15 42 score3.txt

how could i do for each line something like "separate the line into pieces (10, 20, score1.txt), and then check which chunk is equal to score

??

Quote:
Originally Posted by ghostdog74 View Post
why make it so complex, in bash, just use the while read loop
Code:
while read line
do
 #do something
done  < file
$line will be your variable for each line while iterating
 
Old 06-12-2009, 06:23 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
i prefer awk
Code:
awk -v score=$score '
{
 for(i=1; i<=NF;i++){
   if ($i == score){
     print $0
   }
 }
}
' file
 
  


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
Strange script error - any ideas (bash hanging) bagpussnz Linux - General 1 01-30-2009 10:29 PM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 06:00 AM
Strange bash problem mixtr Slackware 17 08-19-2006 01:33 PM
A strange bash script kingsyl Linux - Newbie 2 07-13-2004 11:07 PM
strange bash login error figmentium Linux - Newbie 3 08-11-2003 04:27 PM


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

Main Menu
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