for / foreach syntax issues (in bash or tcsh)

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
# 1  
for / foreach syntax issues (in bash or tcsh)

So I am new to unix, and actually anything outside drag and drop with the mouse (been learning for about a week so far) . I have been using the foreach command in tcsh because I am working on a group of files. Basically what I need is to insert part of the filename as the first line in the file. Here is what I have:
% foreach i (first)
foreach? set ${i}
foreach? perl -pi -le' print($ENV{$i}) if $. == 1' "${i}merge_1.txt"
foreach? end

I also tried with double quotes - print($ENV{"i"}), but both only insert a blank space in the beginning of the file.

I tried in bash as well:
for i in first; do
  export "${i}";
  perl -pi -le' print($ENV{"VAR"}) if $. == 1' "${i}merge_1.txt";

Again, only a blank space at the beginning of the file...
I have read some things about how it is difficult to export variables between languages (e.g. into perl), but I found one reference using $ENV with a variable from bash into perl. Is it possible to export the foreach or for variable into perl as in the command above?

Your forum has been mighty helpful over the past 3 days (I have learned tons because of it) so a big thank you to everyone that posts here!


---------- Post updated 02-08-10 at 01:28 AM ---------- Previous update was 02-07-10 at 07:49 PM ----------

Figured it out...
$for i in OC_Wv_merge_1.txt; 
do export i=${i}; 
perl -pi -le' print $ENV{"i"} if $. == 1' $i; 

And I learned about environment variables in the process...

Last edited by Scott; 02-08-2010 at 03:42 AM.. Reason: Please use code tags
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #40
Difficulty: Easy
HTML stands for 'Hyper Technology Transfer Protocol'.
True or False?

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Use foreach with sed in a bash script

I want to extract data from a ASCII file that looks like the one provided here (see input.txt). For this purpose I used sed commands. I want to chain the sed commands into a script that I can call with custom variables, instead of having to run it multiple times (Need to run the code for 30*24 =... (1 Reply)
Discussion started by: learningtocode
1 Replies

2. Shell Programming and Scripting

TCSH IF syntax issue

Hi All, I'm trying to write a simple if statement in TCSH and I honestly can't figure out what I'm doing wrong. I've played around with all sorts of permutations of syntax. if ($DESKTOP_SESSION == "kde") then replace "forceFontDPI=0" "forceFontDPI=96" --... (0 Replies)
Discussion started by: VerticalMule
0 Replies

3. Shell Programming and Scripting

Issues with awk and tcsh

Hello experts, I have two files which I'm uploading. One is an awk script and other file acts as an input to the script via positional parameter. awk -f intlmenu.awk jobsq.txt This run fine in C shell on SCO OpenServer Release 5.0.7. When I run it on Solaris 10 ( tcsh shell ) I get... (2 Replies)
Discussion started by: maverick_here
2 Replies

4. Shell Programming and Scripting

foreach in bash (simple)

#!/bin/bash foreach valuex ( word1 word2 word3 word4 ) echo $valuex done the output should be: word1 word2 word3 word4 HOW DO I MAKE THIS WORK? (1 Reply)
Discussion started by: ajp7701
1 Replies

5. UNIX for Dummies Questions & Answers

tcsh: how to prevent a foreach from terminating a script when the result is null?

Sorry if this has been answered. I did search both Google and this site and did find this post: However, it wasn't answered. I have the same question - how do you prevent a tcsh script from terminating when the... (4 Replies)
Discussion started by: deepstructure
4 Replies

6. UNIX for Dummies Questions & Answers

tcsh issues

HI, I am having strange issues with my tcsh shell. First, the "ln" command doesnt seem to work properly. I have a file "target" that is pointing to "file1". I cannot access file1 but that shouldnt matter. when I do this, ln -sf file2 target I get permission denied that I cannot access the... (1 Reply)
Discussion started by: sardare
1 Replies

7. UNIX for Dummies Questions & Answers

$ in sed under tcsh vs bash

In bash, I can match the ' character in a substition involving the line ending symbol $, easily. In tcsh I ran into a problem. Code: sed "s/$/'/g" filename sed "s/$/'/g" < filename sed -e "s/$/'/g" filename Unmatched '. Where can I find out why this is the case? (2 Replies)
Discussion started by: uiop44
2 Replies

8. Shell Programming and Scripting

simple tcsh question using foreach

I want to search line-by-line for a string in a file, and I want to do this to a series of files in a directory. I'm doing this in tcsh This works fine to do the search: while read i; do grep $i file2; done <file1.txt This also works fine to read a directory: foreach file ('/bin/ls... (1 Reply)
Discussion started by: moldoverb
1 Replies

9. Shell Programming and Scripting

foreach syntax error

I wrote a simplistic script which works fine in my HP Korn environment using for in do done but when I converted it for csh it is balking at my syntax specifically the parens for the argument. Any help would be appreciated set -x cut -f1 -d,... (3 Replies)
Discussion started by: r1500
3 Replies

Featured Tech Videos