LinuxQuestions.org
Help answer threads with 0 replies.
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 01-11-2012, 12: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, 04:54 PM   #2
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
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, 04: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



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 06:23 AM
sh, Bourne problem kdyzsa Programming 5 05-09-2008 06:59 PM
Bourne and Decimals? kdyzsa Programming 4 05-07-2008 01:16 AM
bourne vs bash alaios Linux - Software 2 03-13-2004 12:16 AM
Bourne Again SHell help MagInnovision Linux - Newbie 2 02-04-2003 03:43 PM

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

All times are GMT -5. The time now is 01:10 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