0

Trying to run an IF statement on two machines-

1.SunOS 5.8 Generic_Virtual sun4u sparc SUNW,Sun-Fire-V240 (bash)

2.SunOS 5.10 Generic_127112-11 i86pc i386 i86pc (tcsh)

The command which I run from shell -

if ( echo 13 | grep -w date +%e>/dev/null ) ; then echo "present" ; fi

It runs fine on machine 1. But get following error on machine 2 (tcsh)-

if: Expression Syntax.

How can I correct this in tcsh ? I need to run this from a crontab file-

30 09 * * * if ( echo 13 | grep -w date +%e>/dev/null ) ; then echo "present" ; fi

kaustav datta
  • 687
  • 2
  • 11
  • 31
  • 1
    You need to use square brackets instead of parenthesis. Like `if [condition]; then doSomething; fi` –  Nov 13 '12 at 16:08
  • @NNzz Why don't you put that down as an answer? – Anirudh Ramanathan Nov 13 '12 at 16:10
  • 2
    @NNzz: that's not exactly true. [ is a program that returns 0 as true and 1 for false. () is a subshell call that should return something as well. – d33tah Nov 13 '12 at 16:18
  • @d33tah: although there typically is a program `/bin/test` and a link to it called `/bin/[`, the `[` operator has been built into shells since UNIX™ System V, and probably even UNIX™ System III (a long time). The status from a sub-shell is the status of the last command that executes in the sub-shell; in the example, that's the `grep`. – Jonathan Leffler Nov 14 '12 at 06:52
  • 2
    The syntaxes of `if` in real shells is different from the syntax in sea-shells, and ne'er the twain shall meet. Use real shells; don't use sea-shells (leave 'em on the C shore). – Jonathan Leffler Nov 14 '12 at 06:53
  • If you want to run a script from `cron`, place the script, with shebang, in an appropriate directory and run the script from `cron`. Also, for looking for `13` in the date at 09:30, you'll probably want to specify 13 in the 'day' field of the crontab pattern; it's simpler. IMNSHO, the entries in crontabs should be short, sharp and largely devoid of special characters (such as pipes and other I/O redirection, semi-colons and whatnot). You should have a command name, possibly some arguments, and that's about it. See also [SO 2229825](http://stackoverflow.com/questions/2229825) for my views. – Jonathan Leffler Nov 14 '12 at 07:01

2 Answers2

0

You're probably using different shells on these machines, which'd explain the syntax errror. You're using subshells in your code, which might have different syntax on different shells.

Also note that you're redirecting the stdout of the echo to /dev/null, so you wouldn't see anything anyway.

d33tah
  • 10,999
  • 13
  • 68
  • 158
  • 1
    The point of the redirection is to lose the output from `grep`; the only item of interest is the exit status, which will be zero (success) if 13 is spotted and non-zero (failure) if it is not. There are options to `grep` to suppress the output (`-q` or `-s`; `-q` suppresses the regular output on match, while `-s` suppresses error messages about missing or unreadable files, etc). – Jonathan Leffler Nov 14 '12 at 06:56
  • 1
    If you want `grep -q` on Solaris, you have to use `/usr/xpg4/bin/grep` (assuming it's even installed). The option is missing from `/bin/grep` – mavit Dec 19 '12 at 20:52
0

Your cron jobs will run under /bin/sh, regardless of your login shell.

To run this from the commandline, simply start /bin/sh (or, if you prefer, /bin/bash) first.

mavit
  • 619
  • 6
  • 13