Score:0

Bash Script Rotation Cipher

pt flag

Hello I need to write a bash script rotation cipher. Unfortunately I can't get any further now. Next I need to use If-Else condition to convert the letters to numbers and shift plus 5 by the letter. Can someone please help me how to do it?

Thank you

#!/bin/bash
#ROT=$1
ROT=5
TEXT=$2
CRYPT_TEXT=""

echo $ROT
echo $TEXT

echo "crypted: $CRYPT_TEXT"

for c in $(echo $TEXT | sed -e 's/\(.\)/\1\n/g')
do
  echo $c
done
Score:6
ie flag

Conversion to upper case can be done in Bash using:

TEXT="foobar" 
echo ${TEXT^^}

A rotation cipher could be implemented using tr, e.g rot13:

echo $TEXT | tr 'A-Za-z' 'N-ZA-Mn-za-m'
# sbbone

rot5 would look like this:

echo $TEXT | tr 'A-Za-z' 'F-ZA-Ef-za-e'
# kttgfw

A partial version without tr command:

#!/bin/bash

TEXT="AZ"

for (( i=0; i<${#TEXT}; i++ )); do
  printf "%s -> %d\\n" "${TEXT:$i:1}" \'${TEXT:$i:1}
  printf -v val "%d" \'${TEXT:$i:1}
  shifted=$(($val + 5))
  echo "shifted: $shifted"
  printf "\\$(printf '%03o' $shifted)\n"

  # A-Z is in range:
  # 65-90
  if [[ $shifted -gt 90 ]];then
    # if value is greated than Z letter you need to subtrack 26
    # so that 91 would become letter A
    echo "$shifted val too large"
    corrected=$(( $shifted - 26))
    echo "corrected ord value $corrected"
    printf "\\$(printf '%03o' $corrected)\n"
  fi
done

the output should look like:

A -> 65
shifted: 70
F
Z -> 90
shifted: 95
_
95 val too large
corrected ord value 69
E

The script converts letters to its corresponding ASCII codes, performs shift and converts codes back to letters. You need to make sure that it works for uppper and lower case letters (or support only one of them). I'll leave the rest as an exercise for the kind reader.

pt flag
the problem is. I need to solve it without tr command. Only with if and else in the next step.
ie flag
@joshi1999 Using just if/else doesn't make much sense. You'd be writing code like if A, then print E. Which isn't really much programming. I've added a bash example of iterating over a text.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.