意外的文件结束错误,但似乎无法找出原因

意外的文件结束错误,但似乎无法找出原因

我为我的作业编写了一个脚本,它接受 3 或 4 个参数:第一个参数是 -e (编码)或 -d (解码),第二个参数是编码/解码密钥,第三个参数是输出文件的名称,第四个参数是可选的,将是要编码/解码的目标文件。如果仅给出前 3 个参数,则使用该命令需要用户输入read

但是,当我尝试运行该脚本时,出现此错误:

./cipher.sh: line 20: Unexpected EOF while looking for matching `''
./cipher.sh: line 26: syntax error: unexpected end of file

这是我的脚本:

#!/bin/bash

if [ "$#" -lt 3 ] || [ "$#" -gt 4 ]; then           #checks for 3 or 4 arguments, 
                                                    #error otherwise
    echo "Error: Need 3 or 4 arguments"
    exit 1
fi

if [ "$1" != "-e" ] && [ "$1" != "-d" ]; then       #Checks if the first argument 
                                                    #is -e or -d, error otherwise
    echo "Error: First argument must be -e or -d"
    exit 1
fi

if [ "$#" -eq 3 ]; then                            #If only 3 arguments are given
    read -p "Enter your input: " userinput
    echo $userinput | tr '[a-z]' '[A-Z]' > $3      #changes all letters to capital
    cat $3 | tr '[A-Z] '$2' > $3                   #Replaces all letters with 
                                                   #letters in key..
elif [ "$#" -eq 4]; then                           #if target file is specified..
    if [ -f $4 ]; then                             #If the file exists and is 
                                                   #regular..
        cat $4 | tr '[a-z]' '[A-Z]' > $3
        cat $3 | tr '[A-Z]' '$2' > $3              #(line 20)
    elif [ ! -f $4 ]; then                         #If the file does not exist
        echo "Error: Target file does not exist"
        exit 1
    fi
fi

答案1

找到它:以以下开头的行

cat $3

缺少报价

相关内容