LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 01-11-2012, 01:15 PM   #1
vellance
LQ Newbie
 
Registered: Jan 2012
Posts: 2

Rep: Reputation: Disabled
bourne / csh while differences


Hi,

I hope this belongs in the "Programming" section (I'm new to LQ)....

I am a junior FreeBSD sysadmin and really like csh (standard shell on our FreeBSD machines). Sometimes the need for a while loop arises. These last couple of weeks I have been forced to do certain stuff in bourne shell because my csh skills are not op to the task. This is a bit cumbersome and frustrating: how difficult can csh's while implementation be ?

I've searched a lot of websites (including LQ) but haven't been able to translate a simple while loop into correct csh syntax. Csh seems a bit elusive

Example:

file "kwaak.txt" contains :

a
b
c
1
2
3


I'm able to do stuff for each entry in bourne by issuing:

cat kwaak.txt | while read example ; do echo "Displaying $example" ; done

Output:

Displaying a
Displaying b
Displaying c
Displaying 1
Displaying 2
Displaying 3


If I issue the same command while in csh things obviously don't work because of the different syntax that is required by csh. After asking several coworkers and abusing google for quite some time I haven't been able to find the correct csh syntax.

My solution now is to enter a traditional bourne shell and do it there or to write a script that does it; both are not ideal.

Perhaps somebody on this forum can help out?

Any help would be greatly appreciated!

Regards,

FuX
 
Old 01-11-2012, 05:54 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Hi FuX and welcome to LinuxQuestions!

Reading a file line-by-line is one of the Top 10 reasons not to use the C-shell. Furthermore, you cannot feed a while loop with input from a pipe or from input redirection, since the if/while/foreach construct - and even the read statement - expect the standard input always from outside the script.

Anyway - you're lucky - a simple solution exists:
Code:
#!/bin/csh
foreach example (`cat kwaak.txt`)
  echo "Displaying $example"
end
Hope this helps.
 
Old 01-17-2012, 05:35 AM   #3
vellance
LQ Newbie
 
Registered: Jan 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Hi colucix,

TnX for your quick and helpfull reply. While your syntax is correct it seems only usable in shell scripts; the thing I wanted to avoid. TnX for the correct syntax though; might prove to be handy in the future

The link/info you provided made me "give up" on csh. I've accepted that for more complex stuff it is not suitable/ideal. I'm currently devising a way to

- log onto our machines and use /bin/sh instead of /bin/csh
- forward my ".shrc" over the ssh-connection to our machines

so that I don't have to work with csh anymore.

Thank you for your help!

Regards,

FuX
 
  


Reply

Tags
csh, loop


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Getting the Bourne again Shell drmjh Linux - General 16 07-03-2011 07:23 AM
sh, Bourne problem kdyzsa Programming 5 05-09-2008 07:59 PM
Bourne and Decimals? kdyzsa Programming 4 05-07-2008 02:16 AM
bourne vs bash alaios Linux - Software 2 03-13-2004 01:16 AM
Bourne Again SHell help MagInnovision Linux - Newbie 2 02-04-2003 04:43 PM


All times are GMT -5. The time now is 06:44 PM.

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