shell help needed



1 #!/bin/bash

2 #

3 # Modify this script for part 3 of assignment 6.



6 key="qbktgrjlpyfehuswndczixmvao"

7 a=$1

8 b=$2

9 c=$3

10 if [-f$a];then

11 if ["$b"-eq"$-"];then

12 if [ "$c"-eq"$encrypt" ];then

13 cat $a | tr "A-Z" "a-z" | tr "a-z" "$key"

14 elif [ "$c"-eq"$decrypt" ];then

15 cat $a | tr "A-Z" "a-z" | tr "$key" "a-z"


17 fi

18 else if [ "$c"-eq"$encrypt" ]

19 then cat $a | tr "A-Z" "a-z" | tr "a-z" "$b"

20 elif [ "$c"-eq"$decrypt" ];then

21 cat $a | tr "A-Z" "a-z" | tr "$b" "a-z"

22 fi

23 fi

"" 27L, 703C 1,1 Top

2. Edit the script so that instead of text/alice.txt it takes in the first command line argument. Add code that will check whether the first command line argument is set. If it is set, the line with the tr command should be run. If no argument is given, print out "Need file as argument 1" and do nothing further. Run your script with different files as input, such as text/hamlet.txt for testing the encryption. ( Hint: you can refer to lecture 5, p.22-23 for detectng arguments.)

3. Edit further to take the key as a second command line argument. Also add code that does the following: If "-" is given as second argument, use the default key in, otherwise change the key to use the second command line argument. Run your script with different keys for testing.

4. Edit with a third command line argument (which acts as a flag):
a.) Add code so that the flag can be set to "encrypt" for encrypting the file with the key, and to "decrypt" for decrypting;
b.) Add code so that for any other string given as the third argument (or if it is missing), the code should do nothing;
c.) Add code for the "decrypt" case: if the flag is set to "decrypt", decrypt an encrypted text back to lower-case using the key.
Run your code to verify that encryption and decryption work correctly. Note that for running decryption, you should first run "encrypt" and save the output to a file, and then use that as input with the "decrypt" flag next time. What happens if your key is shorter than the alphabet, or if it contains duplicate letters?


Do you want help with what you have done, or do you expect us to do it all for you?
Break it down into smaller sections, and use echo or print to verify that that section works, or what's going wrong. Once you get one section nailed, go on to the other.

That's part of the learning process - mastering debugging techniques.

Members online