LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-08-2009, 03:34 PM   #1
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Costa Rica
Distribution: Kubuntu, Debian, Knoppix
Posts: 2,092
Blog Entries: 1

Rep: Reputation: 90
csh: while read


Is there an equivalent in csh to bash's while read variable list?
 
Old 07-08-2009, 04:17 PM   #2
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 71
AFAIK, the answer is no.
 
Old 07-08-2009, 04:31 PM   #3
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Costa Rica
Distribution: Kubuntu, Debian, Knoppix
Posts: 2,092

Original Poster
Blog Entries: 1

Rep: Reputation: 90
Well, I had to do it with a rather long trick:

say, in bash:

Code:
cat /etc/passwd | while read blah; do
    echo $blah
done
in csh:
Code:
set lines=`cat /etc/passwd`
set i=1
while ( $i <= $#lines )
    echo $lines[$i]
    @ i = $i + 1
end
Or something like that
 
Old 07-08-2009, 04:38 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Or something like:
Code:
foreach line ( "`cat /etc/passwd`" )
  echo $line
end
 
Old 07-08-2009, 04:43 PM   #5
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Costa Rica
Distribution: Kubuntu, Debian, Knoppix
Posts: 2,092

Original Poster
Blog Entries: 1

Rep: Reputation: 90
That's right, but in my case I had two variables to get from every line, so I couldn't use the foreach and had to go with the while instead.
 
Old 07-08-2009, 05:17 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
In C-shell it's not possible to read multiple variables, but you can do a trick like this:
Code:
foreach line ( "`cat file`" )
   set argv = ( $line )
   set name1 = $1
   set name2 = $2
   echo $name1
   echo $name2
end
or eventually use the cut command to split the line into fields, using a custom field separator as : (colon) to parse /etc/passwd.
 
Old 07-08-2009, 05:48 PM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
This should work for /etc/passwd:
Code:
foreach line ( "`cat /etc/passwd`" )
   set line = "$line:gas/ /_/"
   set line = "$line:gas/:/ /"
   set argv = ( $line )
   set name1 = $1
   set name2 = "$5:gas/_/ /"
   echo $name2
end
It uses variable modifiers. It replace any blank space with _ (underscore), then any : with a blank space. Finally it extracts the fields, replacing _ with a blank space in those fields where a blank space can appear (needed only for those fields where a blank space can actually appear, like the fifth field).
 
  


Reply

Tags
bash, csh, while



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
csh is gone. Where can I find it? Agresvig Linux - Software 1 09-26-2007 03:15 PM
csh nirmaltom Programming 1 05-18-2007 08:07 AM
csh script help vijraj Linux - Newbie 2 10-04-2006 07:49 AM
New to Solaris/csh slick_willie Solaris / OpenSolaris 19 12-04-2004 02:47 PM
csh zaza Linux - Newbie 6 05-21-2002 06:26 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:59 AM.

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