1

I have written a script for my assignment that takes in 3 or 4 arguments: 1st argument is -e (encode) or -d (decode), 2nd argument is the encoding/decoding key, 3rd argument is the name of the output file, and 4th argument is optional and will be the target file to be encoded/decoded. If only the first 3 arguments is given, then a user input is required by using the read command.

However, when i tried to run the script I get this error:

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

This is my script:

#!/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
Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
  • 1
    Here: cat $3 | tr '[A-Z] '$2' > $3 -- the '[A-Z]' is missing the second quote – ilkkachu Feb 12 '19 at 21:59
  • 1
    Anything that has syntax highlighting is useful for finding stuff like that. As here in the question, the colors change when the quotes are off. – ilkkachu Feb 12 '19 at 22:01
  • Oh thank you so much! I was wondering why some comments and stuff were all red. The UNIX console doesn't highlight anything so I had trouble looking through the code. Thank you again ^^ – Pengibaby Feb 12 '19 at 22:03
  • 1
    Also, shellcheck.net is really useful with shell scripts, it actually finds this one too, though the message is a bit unclear (but then, it can't know for sure where the quote should end) – ilkkachu Feb 12 '19 at 22:04
  • You can install shellcheck locally. And get a good editor. What editor are you using? – ctrl-alt-delor Feb 12 '19 at 22:31
  • @ctrl-alt-delor I am using the school unix servers and they have nano which is what i am using. – Pengibaby Feb 12 '19 at 22:32
  • When I open a file starting #!/bin/bash in nano, I get syntax highlighting. Does it work for you. Notice the colours, they are your friend, if they go strange, then there is an error. (it will not spot all errors, but it does spot a lot, and with a fast feedback cycle) – ctrl-alt-delor Feb 12 '19 at 22:36
  • @ctrl-alt-delor nope, i don't get any syntax highlighting, maybe it's because I'm connecting through ssh? I'm not sure, it just shows regular white font colours :/ – Pengibaby Feb 12 '19 at 22:50
  • I just tested over ssh, it works. You could try asking a new question about this, or ask your school IT support team. What terminal are you using? – ctrl-alt-delor Feb 12 '19 at 22:52

1 Answers1

0

Found it : the line starting with

cat $3

Has a missing quote

  • It's sometimes hard to catch something like this, especially after writing/ working for a while :) – Radu Chirovici Feb 12 '19 at 22:17
  • @Pengibaby Note also that the '$2' in all instances should be "$2" for the $2 to be expanded. And that ALL expansion need to be double-quoted (try using the script with a third argument that has a space in it, like "test me", for example, or which is "*"). Actually, don't test with *, it would ovrewrite existing files. – Kusalananda Feb 12 '19 at 22:18
  • @Kusalananda Thank you so much as well, I changed it! ^^ – Pengibaby Feb 12 '19 at 22:20
  • @Pengibaby If this solves your issue, please consider accepting the answer. – Kusalananda Feb 12 '19 at 22:20
  • @Pengibaby BTW, there's a space missing before ] in [ "$#" -eq 4]; as well. Just run it through shellcheck.net – Kusalananda Feb 12 '19 at 22:23
  • @Pengibaby Sorry, me again... tr does not need the [ or ] in its arguments. tr '[A-Z]' '[a-z]' would change all letters A to Z to lower case, and also all [ to [ and all ] to ]. – Kusalananda Feb 12 '19 at 22:27
  • @Kusalananda omg, I was literally wondering why all the encoding was 1 letter off, thank you so so much! You can probably tell I am still new to this. :P – Pengibaby Feb 12 '19 at 22:30