Hello,
Would anyone please tell me how to write a shell script which receives input argument from command line?
Thanks alot

Dani AI

Generated

For — csh does accept command-line arguments. The shell sets a wordlist variable named argv; the number of arguments is in $#argv, individual items are $argv[1], $argv[2], etc., and $argv[*] expands to the whole list. The positional shorthands $1, $2, ... also work, but argv makes ranges and foreach loops easier. (docs.oracle.com)

A minimal, safe pattern to show and iterate arguments:

#!/bin/csh -f

if ( $#argv == 0 ) then
  echo "Usage: $0 arg1 [arg2 ...]"
  exit 1
endif

echo "First arg: $argv[1]"
echo "All args: $argv[*]"

foreach a ($argv)
  echo "Arg: $a"
end

Notes: use -f in the shebang to avoid reading interactive startup files in a script, test $#argv before indexing to avoid out-of-range errors, and prefer $argv[...] when needing subranges or list operations. (linuxcommandlibrary.com)

A caution following ’s hint: csh has known scripting quirks and many administrators discourage nontrivial scripts in csh — see Tom Christiansen’s “Csh Programming Considered Harmful” and related distro guidance. For anything more than a short wrapper, consider writing the script in sh/bash (more portable and predictable). If csh is required, keep scripts simple, quote arguments carefully, and test with csh -n or -x while debugging. (ooblick.com)

Recommended Answers

All 2 Replies

What have you got so far?

I know that bash uses $1, $2, $3, and so on as variables for command arguments. I imagine csh might be similar, if not the same. Have you tried reading the manpage on csh? It might give you some pointers on accepting command arguments.

What have you got so far?

I know that bash uses $1, $2, $3, and so on as variables for command arguments. I imagine csh might be similar, if not the same. Have you tried reading the manpage on csh? It might give you some pointers on accepting command arguments.

Thanks alot
Rock31

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.