How can I set a default value when incorrect/invalid input is entered in Unix?

Posted by user2889968 on Stack Overflow See other posts from Stack Overflow or by user2889968
Published on 2013-10-31T03:35:53Z Indexed on 2013/10/31 3:53 UTC
Read the original article Hit count: 104

Filed under:

i want to set the value of inputLineNumber to 20. I tried checking if no value is given by user by [[-z "$inputLineNumber"]] and then setting the value by inputLineNumber=20. The code seems to be working correct when no value is given but I also receive ./t.sh: [-z: not found as message on the console. Is there some way to not print this message. Here's my full script as well.

#!/bin/sh
cat /dev/null>copy.txt
echo "Please enter the sentence you want to search:"
read "inputVar"
echo "Please enter the name of the file in which you want to search:"
read "inputFileName"
echo "Please enter the number of lines you want to copy:"
read "inputLineNumber"
[[-z "$inputLineNumber"]] || inputLineNumber=20
cat /dev/null > copy.txt
for N in `grep -n $inputVar $inputFileName | cut -d ":" -f1`
do
  LIMIT=`expr $N + $inputLineNumber`
  sed -n $N,${LIMIT}p $inputFileName >> copy.txt
  echo "-----------------------" >> copy.txt
done
cat copy.txt

© Stack Overflow or respective owner

Related posts about unix