[Solved-4 Solutions] Test-line 4-2- command not found



Error Description:

    • When we try the following code:
    #!/bin/bash
    echo Script name: $0
    echo $# arguments 
    if [$# -ne 1]; 
        then echo "illegal number of parameters"
    fi
    
    click below button to copy the code. By Bash tutorial team
    • We get the following error:

    Test-line 4-2- command not found

    Solution 1:

      • Just like any other simple command, [ ... ] or test requires spaces between its arguments.
      if [ "$#" -ne 1 ]; then
          echo "Illegal number of parameters"
      fi
      click below button to copy the code. By Bash tutorial team
      • Or
      if test "$#" -ne 1; then
          echo "Illegal number of parameters"
      fi
      click below button to copy the code. By Bash tutorial team
      • When in Bash, prefer using [[ ]] instead as it doesn't do word splitting and pathname expansion to its variables that quoting may not be necessary unless it's part of an expression.
      [[ $# -ne 1 ]]
      
      click below button to copy the code. By Bash tutorial team
      • It also has some other features like unquoted condition grouping, pattern matching (extended pattern matching with extglob) and regex matching.
      • The following example checks if arguments are valid. It allows a single argument or two.
      [[ ($# -eq 1 || ($# -eq 2 && $2 == <glob pattern>)) && $1 =~ <regex pattern> ]]
      
      click below button to copy the code. By Bash tutorial team
      • For pure arithmetic expressions, using (( )) to some may still be better, but they are still possible in [[ ]] with its arithmetic operators like -eq, -ne, -lt, -le, -gt, or -ge by placing the expression as a single string argument:
      A=1
      [[ 'A + 1' -eq 2 ]] && echo true  ## Prints true.
      
      click below button to copy the code. By Bash tutorial team
      • That should be helpful if you would need to combine it with other features of [[ ]] as well.

      Solution 2:

        • It might be a good idea to use arithmetic expressions if you're dealing with numbers.
        if (( $# != 1 )); then
            echo "Illegal number of parameters"
        fi
        
        
        click below button to copy the code. By Bash tutorial team

        Solution 3:

          • A simple one liner that works can be done using:
          [ "$#" -ne 1 ] && ( usage && exit 1 ) || main
          
          click below button to copy the code. By Bash tutorial team
          • This breaks down to:
            • test the bash variable for size of parameters $# not equals 1 (our number of sub commands)
            • if true then call usage() function and exit with status 1
            • else call main() function
          • Thinks to note:
            • usage() can just be simple echo "$0: params"
            • main can be one long script

          Solution 4:

            • If you're only interested in bailing if a particular argument is missing, Parameter Substitution is great:
            #!/bin/bash
            # usage-message.sh
            
            : ${1?"Usage: $0 ARGUMENT"}
            #  Script exits here if command-line parameter absent,
            #+ with following error message.
            #    usage-message.sh: 1: Usage: usage-message.sh ARGUMENT
            
            
            click below button to copy the code. By Bash tutorial team

            Related Searches to Test-line 4-2- command not found