2013-10-06 61 views
0

我對Unix非常陌生,這讓我瘋狂。我收到此錯誤:Unix腳本給出錯誤嘗試分配變量?

./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") 

運行此腳本:

#!/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 

我不知道那些錯誤的意思,並搜索到網上並沒有給我很大的啓示。我感謝任何幫助!

+0

你是如何調用腳本? – sehe

回答

2

好像你想在這裏使用command substitution

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

但是你用錯了引號。正確的有傳統反引號:

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

還是形式:

tLines=$(grep -c *.* $3) 
相關問題