0

While attempting to extract a single argument given from a bash script I am getting an error:

./intercept.sh: line 23: [run: command not found
./intercept.sh: line 28: [run: command not found
USAGE: ./intercept.sh run|term

How I'm processing the argument provided:

function main () {
  if [$1 == 'run']
  then
    echo "turning intercept proxies on";
    # enableInterceptProxies;
    echo "intercept proxies turned on";
  elif [$1 == 'term']
  then
    echo "turning intercept proxies off";
    # disabbleInterceptProxies;
    echo "intercept proxies turned off";
  else
    usage;
  fi
}

main $@;

What am I doing wrong here? All I need to do is check if the single argument is run or term, how can I do this successfully?

  • 2
    You need a space between `[` and `$1` – Sean Bright Mar 22 '18 at 18:05
  • Welcome to the site! Check out the [tour](https://stackoverflow.com/tour) and the [how-to-ask page](https://stackoverflow.com/help/how-to-ask) for more about asking questions that will attract quality answers. You can [edit your question](https://stackoverflow.com/posts/49435563/edit) to include more information. – cxw Mar 22 '18 at 18:06
  • @SeanBright I can't believe it was that simple, post it as an answer and I'll accept it. – SomeWhatCertified Mar 22 '18 at 18:06
  • BTW, `main $@` is buggy -- quotes are important; in this context, that means `main "$@"`. Otherwise, `./yourprogram "one argument"` becomes identical to `./yourprogram "one" "argument"`. – Charles Duffy Mar 22 '18 at 18:24
  • Also, note that the POSIX-standardized way to declare a function is `main() {`. The legacy ksh syntax is `function main {` -- note that there's no `()` in that. Combining the two forms, as done here, gives you a hybrid that's compatible with neither ancient ksh *nor* with the POSIX standard. See also http://wiki.bash-hackers.org/scripting/obsolete. (Moreover, in the ksh versions where `function foo {` was useful, defining a function that way made variables local-by-default, which it *doesn't* do in bash, so this syntax is likely to be... surprising... even to folks coming from ksh). – Charles Duffy Mar 22 '18 at 18:25
  • @CharlesDuffy Thanks for the info, I was wondering if there was a bash standard or not – SomeWhatCertified Mar 23 '18 at 13:12
  • Bash is a superset of the POSIX sh standard (or rather very nearly so; its `echo` is incompatible by default, as `echo -e` is not allowed to do anything but print `-e` on output). There is no formal standard covering bash's various extensions -- they're obligated to comply only to the shell's own documentation, though when/where they were borrowed from another shell, arguments about that other shell's behavior are often considered relevant in discussing what constitutes correctness. – Charles Duffy Mar 23 '18 at 16:06

0 Answers0