0

i am beginner in writing scripts in csh/tcsh so that's why i need you help. I have to find out if arguments of my script are written correctly on stdin. I have some script for example called 'first_script' that must have arguments in this form: first_script -d (and this is my problem) ---> how can i find out, if there's number (integer - not only digit) after -d argument? Thanks a lot for helping me.

  • You can use `getopts` for this. Check this good answer to see an example -> http://stackoverflow.com/a/14203146/1983854 – fedorqui Oct 25 '13 at 09:29

1 Answers1

0

Processes can only pass strings as arguments, so what you will get will always be a string. It's up to you to interpret the value as what you need (e. g. an integer).

In your case I think checking if the given string consists solely of digits would solve your issue. There are many ways to do this check, but here is my favorite:

if ( "$1" == "-d" ) then
   expr "$2" : '[0-9]*$' > /dev/null && echo "We have a number" || echo "We have a non-number"
endif
Alfe
  • 56,346
  • 20
  • 107
  • 159