Unix脚本在尝试分配变量时出错?

时间:2013-10-06 23:31:40

标签: 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

我不知道这些错误意味着什么,在线搜索并没有给我太多的洞察力。我感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

好像你想在这里使用command substitution

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

但你使用了错误的引号。正确的是传统的反叛:

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

较新的表单:

tLines=$(grep -c *.* $3)