Reading foreach variable from a text file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Reading foreach variable from a text file
# 1  
Old 07-24-2015
Reading foreach variable from a text file

Hello,

I have a text file named "foreach.txt" which reads like ---

Code:
foreach cal ( 1 2 3 4 5 )

I am using a simple script which looks like ---
Code:
#!/bin/tcsh
foreach cal (1 2 3 4 5)
echo "$cal"
end

Is it possible to modify the script in such a way that instead of writing
Code:
foreach cal (1 2 3 4 5)------Second line of the script

, we can straight way call/access the text file "foreach.txt" and works as it is.
So, something like
Code:
#!/bin/tcsh
???????cat foreach.txt??????????
echo "$cal"
end

Thanks,
# 2  
Old 07-25-2015
Did you consider the source command/builtin?
# 3  
Old 07-25-2015
Hi RudiC,

Thanks for your r ply. So, do you mean ----
Code:
source foreach.txt

???

Thanks,
# 4  
Old 07-25-2015
Yes, but you'll need to have the entire for construct in that sourced file.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 07-25-2015
Thanks RudiC, how can I do that? I don't know much about it though I am trying to find this in the forum. Could you please help me
# 6  
Old 07-25-2015
Sorry, I don't know anything about tcsh. In bash, this would work:
Code:
for i in $(cat foreach.txt)
do
echo $i
done

with foreach.txt being 1 2 3 4 5
# 7  
Old 07-26-2015
One could remove the cat
With: $(<foreach.txt)
hth
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

How to write in other language in text/xml file by reading english text/xml file using C++?

Hello Team, I have 2 files.one contains english text and another contains Japanese. so i have to read english text and replace the text with Japanesh text in third file. Basically, I need a help to write japanese language in text/xml file.I heard wstring does this.Not sure how do i write... (2 Replies)
Discussion started by: SA_Palani
2 Replies

2. Shell Programming and Scripting

Comparing 2 variable in a list with foreach

cat file1 a b c d e f this is what is in my script foreach i (`cat file1`) foreach j (`cat file1`) #do something here end end basically i want to compare ab, ac, ad, ae, af, ba, bc, bd, be.... and also skipping aa,bb if possible.. if that anyway for me to just use 1 foreach? (2 Replies)
Discussion started by: ctphua
2 Replies

3. Shell Programming and Scripting

Reading text file, comparing a value in a line, and placing only part of the line in a variable?

I need some help. I would like to read in a text file. Take a variable such as ROW-D-01, compare it to what's in one line in the text file such as PROD/VM/ROW-D-01 and only input PROD/VM into a variable without the /ROW-D-01. Is this possible? any help is appreciated. (2 Replies)
Discussion started by: xChristopher
2 Replies

4. Shell Programming and Scripting

EXPECT: Assign variable by reading a line of text from a file

Hi All, I have been using a program on windows called AutoKey. My environment at work is Linux and I have been experimenting with expect. Very powerful. I can move my AutoKey scripts to Linux using Expect once I am educated on how to read from a file using Expect. My application would be... (1 Reply)
Discussion started by: quemalr
1 Replies

5. Shell Programming and Scripting

foreach variable used in awk (please help!!)

I have a list of markers, say marker.txt: rs913257 rs1018390 rs764180 and I need to know which ROW that each marker appears in another map file, say map.txt: a rs12354060 b rs913257 c rs6650104 d rs2185539 e rs6681105 f rs1018390 g rs764180 h rs12564807 i rs3094315 The result... (4 Replies)
Discussion started by: Zoho
4 Replies

6. Shell Programming and Scripting

reading variable value from a file

Hello, I need to read a variable value from script. Below is the scenario I am reading a value from an external file say a.txt a.txt: Jan Feb Mar I need the corresponding value of the months in in numerics such as Jan -->1, Feb-->2 etc. I have this mapping in another file... (1 Reply)
Discussion started by: aixjadoo
1 Replies

7. UNIX for Dummies Questions & Answers

Reading a variable from file

Hi, I have a situation where I need to read a variable from another file. But the problem is that the variable in the other file is starting with $. E.g. file1: $var1=out temp_ss.sh: . file1 echo "Print : $var1" It works fine if the file1 is having var1=out (note that it is... (6 Replies)
Discussion started by: shash
6 Replies

8. Shell Programming and Scripting

Reading variable from file variable values

Hi, Here is the output of lpstat. I would like to read value of Queue which is(abxxxxb1)and status that is DOWN in first line. i dont care what is in second line. any one can help me.thanks Queue Dev Status Job Files User PP % Blks Cp Rnk ------- ----- ---------... (5 Replies)
Discussion started by: sagii
5 Replies

9. UNIX for Advanced & Expert Users

Please Help!! Reading a string of text with concurrent spaces into a shell variable

Please Help!! Here is a very simplistic example of what I am trying to accomplish. I need what I have inbetween the quotes to be read into the shell variable. x="This is fun" echo $x The results of x from the above expression is: This is fun Notice the unix takes out the... (1 Reply)
Discussion started by: mjs3221
1 Replies

10. Shell Programming and Scripting

How to define two variable in foreach command??

Hello, I just want to know how If it's possiple to define 2 variable using foreach command ??? I have directory inside that directory around 1000 file, I want to rename all of this files to something I have it in a list. Example :- ------This is what in my directory---------- d1 d2... (14 Replies)
Discussion started by: geoquest
14 Replies
Login or Register to Ask a Question