LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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
 
LinkBack Search this Thread
Old 07-08-2009, 03:34 PM   #1
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,956
Blog Entries: 1

Rep: Reputation: 81
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: 60
AFAIK, the answer is no.
 
Old 07-08-2009, 04:31 PM   #3
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,956
Blog Entries: 1

Original Poster
Rep: Reputation: 81
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
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,361

Rep: Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909
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: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,956
Blog Entries: 1

Original Poster
Rep: Reputation: 81
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
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,361

Rep: Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909
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
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,361

Rep: Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are 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


All times are GMT -5. The time now is 06:54 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration