LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 10-17-2013, 01:02 AM   #1
Praveen waliitagi
LQ Newbie
 
Registered: Oct 2013
Posts: 3

Rep: Reputation: Disabled
cat command inside foreach loop not working


I have a file named abc.lst
Contents of abc.lst is as below (it has only one line)
-------------------------------------
$xyz/list/source/abcxyz.f

--------------------------------------



contents of file abcxyz.f is as below
--------------------------------------
/asdc/sfd/wefef/wef/we/e/ewww/abc.v
/afwef/trgt/wedwd/fvbtgb/axd.v
/asdc/sfd/wefef/wef/we/e/ewww/abcd.v
/afwef/trgt/wedwd/fvbtgb/axddd.v

---------------------------------------



My shell script is as below
---------------------------------------
foreach line ('cat abc.lst')
cat $line >> temp1
end
---------------------------------------

when i run this script i get a message saying
cat: cannot open $xyz/list/source/abcxyz.f


Instead of $line if i do cat $xyz/list/source/abcxyz.f >> temp1
then it works.
I have defined $xyz variable as well in my terminal.

Can you please let me know what is the problem in using $line variable?

Thanks
Praveen
 
Old 10-17-2013, 01:14 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

this appears to be (t)csh, at which I'm a little rusty, but the problem seems to be that the variable $xyz is not defined in your script. Additionally, I'm not sure if this sort of "nesting" of variables would work anyway (but nothing ever surprises me about csh).
Evo2.
 
Old 10-17-2013, 01:20 AM   #3
Praveen waliitagi
LQ Newbie
 
Registered: Oct 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks Evo,
I did set $xyz variable in my script but still i'm facing the same issue.
Any other suggestions would be helpful

Thanks
Praveen
 
Old 10-17-2013, 01:26 AM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,
Quote:
Originally Posted by Praveen waliitagi View Post
I did set $xyz variable in my script but still i'm facing the same issue.
Any other suggestions would be helpful
It seems that in your script $xyz is being treated as a literal string, and not a variable identifier (this is what I eluded to when I wrote "nesting" of variables). There may be some dirty way around this but, I'm no (t)csh expert, having escaped it early in my career, so I don't know what it may be.

My first advice would be to abandon (t)csh for scripting (and for interactive use if possible).

My second piece of advice would be to rethink what you are actually trying to do, since I suspect there is a much simpler way to achieve the desired end result.

Evo2.
 
Old 10-17-2013, 01:31 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
you need to use backticks instead of ' :
foreach line (`cat abc.lst`)
use tcsh -x -v <your script> to see what's happening
 
Old 10-17-2013, 01:49 AM   #6
Praveen waliitagi
LQ Newbie
 
Registered: Oct 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
I have used backticks in my script. Issue isn't with ticks for me. I
Issue is with "xyz" variable which is in $line.

If i write my script as below
foreach line (`cat abc.lst`)
cat $xyz/list/source/abcxyz.f >> temp1
end

Then it works.

But i need to use $line as my real time need may be with file containing multiple lines.

Thanks
Praveen
 
Old 10-17-2013, 02:09 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
so you need double evaluation:
eval cat $line >> temp1
but using eval can be dangerous and not suggested at all. as it was mentioned there should be a much better/simpler way to do that
 
1 members found this post helpful.
  


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
Sleep inside for loop inside while loop causing issues. TheOnlyQ Programming 13 12-19-2012 12:59 PM
Perl wonky IF outcome inside a @list foreach loop OldGaf Programming 3 09-18-2009 10:21 AM
cat command space problem with awk in for loop. shan_nathan Programming 3 06-18-2009 05:51 AM
Bash: command works but not inside of for loop. RijilV Programming 3 05-21-2006 08:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:51 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