0

I am looking for a test expression with a usage message. When I enter in command line a file such as ./file I need the script to stop and echo error. When I enter ./file test I need the file to run and enter file. I am having a hard time using test expression message. So far I have this and it sort of does what I want it to but I need to check and make sure ./file test is actually input to enter file. if it's not input at command properly it should spit an error and exit.

   if [$# -eq 0 ]
      then
           echo"error"
      exit 1
   fi

closed as off-topic by jasonwryan, G-Man, cuonglm, don_crissti, garethTheRed Nov 16 '15 at 8:02

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – jasonwryan, G-Man, cuonglm, don_crissti, garethTheRed
If this question can be reworded to fit the rules in the help center, please edit the question.

5

[ is a command; [$# is not. You need to separate a command from its arguments:

if [ $# -eq 0 ]; then
    echo "error"   # Here, too
    exit 1
fi

Incidentally, you can replace that whole block with a special parameter expansion:

: ${1?error}

If the first positional parameter isn't set (i.e., there are no positional parameters), print the message following the ? and exit.

  • Im lost sorry for my ignorance I don't understand – jake Nov 14 '15 at 19:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.