2

I am trying to write a C shell equivalent script for the bash script mentioned here.

This is what I have :

#! /bin/tcsh

set now=`date +%Y%m%d%H%M.%S`
if (( ! -f "./cache" ) || (-n  "`find ./monme -newer ./cache`" ))
then
    touch cache -t "$now"
    echo "new files added" | mail -s "new build" myemail@myserver.com
endif

and this is the error I get

$ ./scr
if: Badly formed number.
$

This page mentions that "Numbers in the C-shell must be integers", so I tried

set now=`date +%Y%m%d%H%M`

but I get the same error still.

Community
  • 1
  • 1
Lazer
  • 90,700
  • 113
  • 281
  • 364
  • Spacing on this part `( -n ..... ))` between parenthesis and dash *n* and also *then* should be on the same line as *if*? – t0mm13b Sep 11 '10 at 18:32
  • @tommieb75: still the same error. – Lazer Sep 11 '10 at 18:34
  • @Lazer: last shot... `( -n eval("`find ./monme -newer ./cache\`") ))` – t0mm13b Sep 11 '10 at 18:36
  • @tommieb75: if (( ! -f "./cache" ) || ( -n eval("`find ./monme -newer ./cache`" ))) then => `if: Badly formed number.` – Lazer Sep 11 '10 at 18:42
  • @Lazer: dang.... fingers crossed for ya... someone who has better knowledge than I do will hopefully give you the right answer.... I tried my best :) – t0mm13b Sep 11 '10 at 18:43
  • @Lazer: P.S: You might get a better chance of this script working by posting across on superuser.com which might be a better fit rather than here... :) – t0mm13b Sep 11 '10 at 18:53
  • ...the snarky answer would be "You wrote it in csh." Bourne derived shells are not exactly free of weird corner cases, but they are widely believed to be easy to program in. – dmckee --- ex-moderator kitten Sep 11 '10 at 19:25
  • no more shell scripts for me. Perl it is! – Lazer Sep 12 '10 at 18:37

1 Answers1

3

I cut down your script to this:

#! /bin/tcsh

if ( -n  "`find ./monme -newer ./cache`" ) then
    echo hello
endif

This gives the same error. I think the culprit is

-n  "`find ./monme -newer ./cache`"

What is -n supposed to do? I think it wants a number, but gets something else...

Update: -n in bash means "length of string is non-zero". In my version of tcsh it is as easy to replace as to use == "" like this:

if (( ! -f "./cache" ) || ("`find ./monme -newer ./cache`" != ""))
then
    touch cache -t "$now"
    echo "new files added" | mail -s "new build" myemail@myserver.com
endif

Try that and see if it works.

Lazer
  • 90,700
  • 113
  • 281
  • 364
Peter Jaric
  • 5,162
  • 3
  • 30
  • 42
  • @Peter: I myself have not used it before. From [this page](http://www.mkssoftware.com/docs/man1/csh.1.asp), "-n parses commands but does not execute them. This aids in checking the syntax of shell scripts". – Lazer Sep 11 '10 at 19:48
  • As far as I understand it, -n is an option to csh when you run it (as in `tcsh -n "echo hello there"`) and not something you can use in an if like that. -n seems to mean something in an if, though, but I can not find it quickly in my man page. – Peter Jaric Sep 11 '10 at 19:51
  • OK, I see that you took it verbatim from the bash script. In bash, or rather the test command, -n means that the length of the following string should be non-zero. Since this does not work in tcsh, you'll have to find the equivalent in tcsh or restructure your code somewhat. – Peter Jaric Sep 11 '10 at 19:59
  • @Peter: Sure, I'll wait some time in case anyone has some good suggestions. – Lazer Sep 11 '10 at 20:22
  • Thanks for the edit, Lazer :) My answer was kinda stupid before that. – Peter Jaric Sep 12 '10 at 09:59