INTRODUCTION TO BASH SCRIPTING - Conditional Execution

Exercise Script

Code: bash

#!/bin/bash
# Count number of characters in a variable:
#     echo $variable | wc -c

# Variable to encode
var="nef892na9s1p9asn2aJs71nIsm"

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

Questions

Answer the question(s) below to complete this Section and earn cubes!

  • Create an “If-Else” condition in the “For”-Loop of the “Exercise Script” that prints you the number of characters of the 35th generated value of the variable “var”. Submit the number as the answer.

My Answer

#!/bin/bash
# Count number of characters in a variable:
#     echo $variable | wc -c

# Variable to encode
var="nef892na9s1p9asn2aJs71nIsm"

for counter in {1..40}
do
    var=$(echo $var | base64);

    # Check if the current iteration is the 35th
    if [ $counter -eq 35 ]; then
        echo "$(echo $var | wc -c)"
    fi
done

[maximus@MaxBookPro Downloads]$ bash hi
800981

But it says that’s wrong? Why is this wrong?

array indexes in bash start at 0 not 1. that’s why. you’re one off of the answer. try changing the counter -eq to different numbers to find the answer.

Whats the answer of that question?

**** SPOILER ****

#!/bin/bash
# Count number of characters in a variable:
#     echo $variable | wc -c

# Variable to encode
var="8dm7KsjU28B7v621Jls"
value="ERmFRMVZ0U2paTlJYTkxDZz09Cg"

for counter in {1..40}
do
        var=$(echo $var | base64)
if [[ $var == *"$value"* ]] && [ ${#var} -gt 113450 ]; then
    echo $var | tail -c 20
    break
fi
done