What's new

counting comand line args in a unix csh script (1 Viewer)

Ronan

Auditioning
Joined
Jul 5, 2001
Messages
6
I want to write a script to count command line arguments in a unix csh script.

I have had ago but only seem to be able to count the total number of line wealther they are command line or not.

If someone could help me alter the script below I would be very greatful.

# Part2.2 count the number of command line arguments types.

echo "the number of command line arguments is: "

wc -l < [file name].
 

Peter Yee

Premium
Joined
Aug 31, 1998
Messages
131
I don't have handy access to a csh, but using bash, $#

evaluates to the number of arguments on the command line,

not counting argv[0] (the command name). Generically, you could use lots of other means to count the arguments. Using awk, for example, you could do:

echo $* | awk '{print NF}'

You could probably use sed, swapping spaces for newlines and piping the results to 'wc -l' to get a similar effect.

-Peter
 

Glenn L

Stunt Coordinator
Joined
Jun 21, 1999
Messages
56
There's dozens of different ways to do this, but you're on the right track with 'wc'.

First examine what a command line is:

% command arg1 arg2 arg3 ...etc

it's one line with a bunch of words, what you want to count are the number of words. 'wc -l' counts the number of lines, 'wc -w' counts the number of words...

In csh, the variable '$*' has the command line (minus the actual command itself), so echo that variable into 'wc -w':

#!/bin/csh

echo "The number of command line arguments is"

echo "$*" | wc -w
 

Peter Yee

Premium
Joined
Aug 31, 1998
Messages
131
Well, I checked on csh. '$#' evaluates to the number of command line arguments, so you won't have to call out of the shell to any other programs to count them.

-Peter
 

Users who are viewing this thread

Sign up for our newsletter

and receive essential news, curated deals, and much more







You will only receive emails from us. We will never sell or distribute your email address to third party companies at any time.

Forum statistics

Threads
357,016
Messages
5,128,459
Members
144,240
Latest member
hemolens
Recent bookmarks
0
Top