-1

I got error message as if[ 0 -lt 1] command not found. I just started shell scripting. I don't know what's wrong in my code:

if[ $# -lt  1 ]   
then
  echo "Give a number as a parameter. Try again."        
else        
  n=$1    
  sum= 0  
  sd=0    
  while[ $n -gt 0 ]                                     
  do     
    sd=`expr $n % 10`                                  
    sum=`expr $sum + $sd`                               
    n=`expr $n / 10`                                    
  done    
  echo "Sum of digits for $1 is $sum"
Karol Selak
  • 4,248
  • 6
  • 35
  • 65
  • 1
    Note that you probably don't have a command called `0` (zero), and it probably doesn't pay any attention to the empty environment variable `sum` (but that's the command you invoke and what the preceding `sum=` means in the `sum= 0` line). With shell scripts, you have to be very careful with spacing. – Jonathan Leffler Nov 04 '17 at 19:30
  • 1
    `if[` is not `if`. `[` is a command, just like `ls` is; you can't run `ifls`, so for the same reason you can't run `if[`. – Charles Duffy Nov 04 '17 at 19:42

2 Answers2

2

You need a space between if and [. You'll have the same issue after while.

The [ ] notation is kind of an oddity in shell. It looks like part of the language, but it actually isn't. The if and while words always need to be followed by whitespace and then a command, which is executed, and considered to be true or false according to whether its exit code is zero or nonzero.

So there is actually a command named [ which evaluates the conditions given on its command line, and terminates with an exit code according to whether the condition evaluated true or false. You can see an executable in the /usr/bin directory on most systems (though the shell usually has a built-in version for efficiency). It works the same as test.

Also, keep in mind that if needs a matching fi after the else clause.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
2

Check out https://www.shellcheck.net/ (Open Source - free)

It allows you to check your shell scripts before running them. You just copy your shell script into the browser and do a syntax check, you can also run locally.

Why?

Well, this I think will really help you learn what you are doing wrong.

  • i.e. Fix spacing errors etc.

Example Output:

$ shellcheck myscript

Line 1:
if[ $# -lt  1 ]                                                                                                   
^-- SC1046: Couldn't find 'fi' for this 'if'.
^-- SC1073: Couldn't parse this if expression.
  ^-- SC1069: You need a space before the [.

Line 8:
    while[ $n -gt 0 ]                                                                                             
         ^-- SC1069: You need a space before the [.

Line 15:

^-- SC1047: Expected 'fi' matching previously mentioned 'if'.
^-- SC1072: Expected 'fi'. Fix any mentioned problems and try again.

$ 
Technophobe01
  • 8,212
  • 3
  • 32
  • 59
  • 1
    What does github have to do with it? (I mean, the *code* for shellcheck is hosted there, but you aren't linking to the code). And recommending shellcheck is excellent advice -- it's common contents for a *comment* -- but if an answer that was nothing but a big shellcheck recommendation and copied/pasted output were acceptable here, half of the bash tag would be nothing but. – Charles Duffy Nov 04 '17 at 19:37
  • @CharlesDuffy Thanks, good point. I removed the reference to Github. My point was to say the code is hosted via Github. personally, I found the repo well worth reviewing. – Technophobe01 Nov 04 '17 at 19:40
  • @CharlesDuffy Agreed - in this context my sense was to recommend the tool to someone who is new to shell script programming. I provided an example of the output of shellcheck by way of example. That seemed more applicable than explain all the errors (there were a quite few). – Technophobe01 Nov 04 '17 at 19:48
  • I should add they are all mistakes I have made many, many times myself - chuckle. Alas, I expect I'll continue to make such mistakes - hence my love of shellcheck. :-) – Technophobe01 Nov 04 '17 at 19:56