LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   cat command inside foreach loop not working (https://www.linuxquestions.org/questions/linux-newbie-8/cat-command-inside-foreach-loop-not-working-4175481124/)

Praveen waliitagi 10-17-2013 01:02 AM

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

evo2 10-17-2013 01:14 AM

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.

Praveen waliitagi 10-17-2013 01:20 AM

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

evo2 10-17-2013 01:26 AM

Hi,
Quote:

Originally Posted by Praveen waliitagi (Post 5047295)
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.

pan64 10-17-2013 01:31 AM

you need to use backticks instead of ' :
foreach line (`cat abc.lst`)
use tcsh -x -v <your script> to see what's happening

Praveen waliitagi 10-17-2013 01:49 AM

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

pan64 10-17-2013 02:09 AM

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


All times are GMT -5. The time now is 04:57 AM.