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

Time:04-21

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

CodePudding user response:

You must have at least one space after [[ and before ]]

otherwise it will be taken as command.

The correct code should be :

   [[ $i == '35' ]]

Tip : You do not need a separate variable i for the indexing. You can directly use the variable counter.

Like:

if [[ "$counter" == "35" ]];then
       // Your codes...
fi
  • Related