Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can get the word count of the 35th output using if-else statements (bash scripting)

I started learning bash scripting and was given the task of making the code below print the count of letters for the 35th encoding with if-else statements.

#!/bin/bash



# Variable to encode
var="nef892na9s1p9asn2aJs71nIsm"

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

I came up with :

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

# Variable to encode
var="nef892na9s1p9asn2aJs71nIsm"
i=0 
for counter in {1..40}
do
        var=$(echo $var | base64)
((i=i+1))
if [[$i == '35']]
then
    echo $var | wc -c
fi
done

but I always get an error at if [[$i == ’35’]] saying command not found

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You must have space after [[ and before ]] otherwise it will be taken as command.

The correct code should be : [[ $i == ’35’ ]]

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading