Conditional Execution | Introduction to Bash Scripting

Thanks for the comment, I was fighting with the code for a while until I read your comment and I realized I didn’t understood the question, and I was answering something different.

1 Like

Hmmm… Here are my thoughts:

  1. I struggled for an hour or two on this using an ubuntu to simulate the output and it gave me one output with counter just literally echoing {1…40} and the wc output (i’m not sure why)
  2. I used a local toolbox called mobaxterm to run the code, and it ran the basic code there, and echoed everything
  3. you dont really need the if-else, because you only need the answer.

enjoy (if you can)

I’ve spent more time than I should on this f*cking exercise.
So, here’s my code:


#!/bin/bash

var="nef892na9s1p9asn2aJs71nIsm"

for counter in {1..5}
do
    var=$(echo -n $var | base64)
    echo $var
done

echo -n $var | wc -m

If you run that snippet, you will get up to 5 iteration and get the base64 hash for that 5th iteration along with the number of characters.
That being said. you can change 5 for 35, and you should get the right answer.
However, the answer is wrong – according to HTB.

So, I give up. Aint got time to spend more than 2 hours on this.

As someone mentioned in this thread, results will differ depending if you run your code from Linux, or macOS, and perhaps Windows?

That being said. answer can be found below, you’re welcome.

#!/bin/bash

# Variable to encode
var="nef892na9s1p9asn2aJs71nIsm"

for counter in {1..40}; do
    var=$(echo $var | base64)
    
    # Check if the loop is at the 35th iteration
    if [ $counter -eq 35 ]; then
        # Print the number of characters in the variable
        echo $var | wc -c
    fi
done