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
cat $3 | tr '[A-Z] '$2' > $3
-- the'[A-Z]'
is missing the second quote – ilkkachu Feb 12 '19 at 21:59#!/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:36ssh
, 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