0

I'm extremely new to Unix, and this is driving me crazy. I am getting this error:

./lines: line 21: [[: grep -c *.* $3: syntax error: operand expected
(error toke                                           n is ".* $3")
./lines: line 26: [[: grep -c *.* $3: syntax error: operand expected
(error toke n is ".* $3")

When running this script:

#!/bin/bash
#lines <start> <finish> <file> prints lines start-finish of file

if [[ $# != 3 ]]
then    echo "Command format: lines <starting line> <end line> <filename>"
    exit
fi

tLines='grep -c *.* $3'
start=$1
finish=$2

if [[ $finish -lt $start ]]
    then echo "$finish is less than $start. I'll go ahead and reverse those for you."
    start=$2
    finish=$1
fi

start=$((finish-start+1))

if [[ $tLines -lt $start ]] 
    then echo "$3 is only $tLines lines - that's less than $start"
    exit
fi

if [[ $tLines -lt $finish ]]
    then echo "3 is only $tLines line - that's less than $finish"
    exit
fi
head -$finish $3 | tail -$start
exit

I have no idea what those errors mean and searching them online have not given me much insight. I appreciate any help!

1
  • how are you invoking the script? Commented Oct 6, 2013 at 23:33

1 Answer 1

2

Seems like you wanted to use command substitution here:

tLines='grep -c *.* $3'

But you used the wrong quotes. The correct ones are the legacy backticks:

tLines=`grep -c *.* $3`

Or the newer form:

tLines=$(grep -c *.* $3)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.