1

Problem is that when I execute this script using source overspeed (after giving myself execute permission), it asks me "How fast are you going?" as what I set but after I enter any value it gives an error "Event not found" instead of displaying "You are over speeding!!!".

Here is my script by the way:

#!/bin/csh
# Over speed indicator
#
echo -n "How fast are you going?"
set speed = $<
if (speed > 100) echo "You are over speeding!!!"
Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
Anonymous
  • 287
  • 4
  • 11

1 Answers1

2

There are a couple of mistakes in your script. The first line should point to your csh executable, which you've identified in the comments as /usr/bin/csh (rather than /bin/csh). The if line is missing the $ to identify speed as a variable. Here is a corrected script

#!/usr/bin/csh
# Over speed indicator
#
echo -n "How fast are you going?"
set speed = $<
if ($speed > 100) echo "You are over speeding\!\!\!"

Ideally you would then run it as ./overspeed rather than source overspeed so that any variables it sets are retained in its own context rather than polluting your interactive shell.

Better than all of this, stop trying to learn a shell language that's fundamentally broken for scripting, and use one of the sh variants instead (ksh or bash). Here is your script rewritten to use bash:

#!/bin/bash
# Over speed indicator
#
read -p "How fast are you going? " speed
if $(( speed > 100 ))
then
    echo 'You are over speeding!!!'
fi

As before, if the script file is executable you can run it with ./overspeed.

roaima
  • 107,089
  • 14
  • 139
  • 261
  • Thanks a lot @roaima you cleared a lot of my concepts. Just one question that `./overspeed` doesn't work. It says command not found. – Anonymous Jun 20 '16 at 00:10
  • Shouldn't it be `(( $speed > 100 ))` or even `(( speed < 100 ))` for numeric comparison? [When used with [[, the ‘<’ and ‘>’ operators sort lexicographically using the current locale.](https://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html#Conditional-Constructs) – steeldriver Jun 20 '16 at 00:14
  • @Ninja `chmod +x overspeed` – Jeff Schaller Jun 20 '16 at 01:26
  • @Jeff Schaller permission was already set but it didn't work. Its ok thanks – Anonymous Jun 20 '16 at 01:35
  • I found the answer why I was not been able to execute the script using `./overspeed`. It's because I wrote "usr" while writing directory path in the first line as told by @roaima. If I do only `#!/bin/csh` then I can execute the script using './overspeed' without any problem. – Anonymous Jun 20 '16 at 01:52
  • @Ninja the reason I changed the first line is because your SHELL declared itself so. If you've got a working line now that's good – roaima Jun 20 '16 at 08:02
  • @steeldriver oops, yes. I was mixing and matching between languages. Usually I'd have gone with `test $x -gt $y` type syntax but with your reminder I've stayed with `$(( x > y ))` this time. Thank you – roaima Jun 20 '16 at 09:44