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!!!"
8
  • 6
    I'll be one of the first to suggest that your shell script education may be better served learning anything other than csh or tcsh.
    – Jeff Schaller
    Commented Jun 19, 2016 at 23:23
  • 1
    In this case, the !!! in your string are likely being interpreted as history expansion commands: see Event not found in sed call in csh script Commented Jun 19, 2016 at 23:31
  • @steeldriver I have even removed that "!!!" from my syntax now it is giving an error "if: Expression syntax"
    – Anonymous
    Commented Jun 19, 2016 at 23:37
  • 1
    you probably mean ($speed >100).
    – michas
    Commented Jun 19, 2016 at 23:38
  • Output of echo $SHELL please Commented Jun 19, 2016 at 23:45

1 Answer 1

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.

7
  • 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
    Commented Jun 20, 2016 at 0: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. Commented Jun 20, 2016 at 0:14
  • @Ninja chmod +x overspeed
    – Jeff Schaller
    Commented Jun 20, 2016 at 1:26
  • @Jeff Schaller permission was already set but it didn't work. Its ok thanks
    – Anonymous
    Commented Jun 20, 2016 at 1: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
    Commented Jun 20, 2016 at 1:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .