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 05-04-2009, 04:05 PM   #1
vxc69
Member
 
Registered: Jul 2004
Distribution: Ubuntu
Posts: 387

Rep: Reputation: 33
csh Shell Script: Getting wc command output to variable to do math on it, how?


Hello guys,


A quick CSH question. I'm trying to run wc -l on a file. I want to get this line count on to a variable in my csh script. I'm not doing it right.

This is what i've tried to no avail:

Code:
1.) set numlinks = `cat` $file | wc -l
2.) numlinks = `cat` $file | wc -l
$file comes from a foreach loop. $file is ok because if I just run 'cat' $file | wc -l, it prints out the correct amount of lines.

Once I get this count into a variable I want to do some arithmetic on it. First time writing shell scripts by the way, so excuse my ignorance.



Thanks for any help,
vxc

Last edited by vxc69; 05-04-2009 at 04:08 PM.
 
Old 05-04-2009, 04:17 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
Your commands don't work because you put backticks around the cat command only, whereas you have to embed the whole command line:
Code:
set numlinks = `cat $file | wc -l`
Then use expr to do arithmetic:
Code:
set numlinks = `expr $numlinks + 1`
Hope this helps.
 
Old 05-04-2009, 04:17 PM   #3
Eycks
LQ Newbie
 
Registered: May 2007
Posts: 4

Rep: Reputation: 0
numlinks = <back quote>cat $file | wc -l<back quote>

My <back quote> key does not work. If this does not work try putting set in fron of numlinks
 
Old 05-04-2009, 04:22 PM   #4
vxc69
Member
 
Registered: Jul 2004
Distribution: Ubuntu
Posts: 387

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by colucix View Post
Your commands don't work because you put backticks around the cat command only, whereas you have to embed the whole command line:
Code:
set numlinks = `cat $file | wc -l`
Then use expr to do arithmetic:
Code:
set numlinks = `expr $numlinks + 1`
Hope this helps.
Oh! I thought that it would try to cat $file instead of what's in $file if i put that in back quotes too. Putting the entire thing in back quotes worked! Thanks. Well appreciated.
 
Old 05-04-2009, 04:27 PM   #5
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
Enclose the whole command in back quotes, and make sure your variable assignments don't have spaces around the equals signs.

For example:
Code:
foreach file (your_list)
  set numlinks=`cat $file | wc -l`
  echo "numlinks for $file is $numlinks"
end
hth

Last edited by raconteur; 05-04-2009 at 04:28 PM. Reason: beaten to the punch while composing ;-)
 
Old 05-04-2009, 04:31 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
Quote:
Originally Posted by raconteur View Post
and make sure your variable assignments don't have spaces around the equals signs.
Not strictly necessary in C-shells. That's true for Bourne-shells instead.
 
  


Reply



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
passing awk output to $variable for shell script? NewnanNOC Programming 3 10-23-2008 02:08 PM
Write output or command to variable (shell) kernel_geek Programming 9 03-12-2007 04:45 AM
Odd problem with making a variable the output of a command in a shell script linux=future Programming 3 12-13-2005 09:45 PM
shell scipting: append output of a command to a variable jonhewer Linux - Newbie 10 08-24-2005 05:42 AM
Assigning the output of one command to a variable (shell) guru_stew Programming 5 08-03-2003 06:12 PM

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

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