2014-01-27 6 views
0

리눅스 쉘 스크립트의 논리 OR 오류 :내가 인쇄 도서에 대한 쉘 스크립트를 작성했습니다

나는 "인쇄 책"파일에이 코드를 저장하고 좋아하는 실행
#!/bin/sh 
if [ -z "$1" ] 
then 
    exit 1 
fi 
filename=$1 
options="" 
mode="color" 
first="" 
last="" 
pages="All pages from" 
shift 
until [ -z "$1" ] 
do 
    if [ $1 = "gray" -o $1 = "grey" -o $1 = "grayscale" -o $1 = "greyscale" ] 
    then 
     options=" -o ColorModel=KGray" 
     mode=$1 
    elif [ $1 = "from" ] 
    then 
     shift 
     first="$1" 
    elif [ $1 = "to" ] 
    then 
     shift 
     last="$1" 
    fi 
    shift 
done 
if [ $first -o $last ] 
then 
    pages="Pages" 
    if [ $first ] 
    then 
     pages="$pages $first" 
     first=" -f $first" 
    else 
     pages="$pages 1" 
    fi 
    if [ $last ] 
    then 
     pages="$pages to $last" 
     last=" -l $last" 
    else 
     pages="$pages to last" 
    fi 
    pages="$pages from" 
fi 
echo -n "$pages $filename will be printed in $mode mode. If it's OK, put paper in your printer and press ENTER. Else press CTRL+C. " 
read ack 
pdftops$first$last -expand $filename - | psbook | psnup -2 > tmp.ps 
psselect -o tmp.ps | lpr$options 
echo -n "Wait for the end of printing, then take printed pages, put them back in printer to print on other side and press ENTER again." 
read ack 
psselect -e -r tmp.ps | lpr$options 
rm tmp.ps 
exit 0 

:

print-book test.pdf gray 

Pages 1 to last from test.pdf will be printed in gray mode. If it's OK, put paper in your printer and press ENTER. Else press CTRL+C 

즉 조건 "$ 먼저 지난 $를 -o"사실 :

나는이 있어요. 그러나이 위치에서 "$ first"와 "$ last"를 따로 확인하면 둘 다 거짓입니다.

어떻게 가능합니까? $first$last가 비어있는 경우

답변

3

, [ $first -o $last ] 당신이 원하는하지 않은, [ -o ]로 평가됩니다.

대신 [ "$first" -o "$last" ]을 사용해야합니다. 이는 [ "" -o "" ]과 동일합니다. (당신이 무슨 일을하는지 알지 못한다면)


그들을 인용하지 않고 변수를 사용하지 마십시오 : 결과는 대부분의 시간을 예상하지 못한 것입니다.

또한 이상한 행동을 명령 줄에서 대화 형으로 테스트하십시오. 을 입력하면 빠르게 진행되고있는 작업을보고 변수로 재생할 수 있습니다.

+1

POSIX 표준에서는 바로이 이유 때문에'-o'를 사용하지 말 것을 권장합니다.'[...] || [...]'대신 '[... -o ...]'. – chepner

관련 문제