如何使用 if-else 语句(bash 脚本)获取第 35 个输出的字数

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

我开始学习 bash 脚本编写,任务是让下面的代码使用 if-else 语句打印第 35 次编码的字母数。

#!/bin/bash



# Variable to encode
var="nef892na9s1p9asn2aJs71nIsm"

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

我想到了:

#!/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

但我总是在 if [[$i == '35']] 处收到错误提示 command not found

[[之后和]]

之前,您必须至少有一个space

否则视为命令

正确的代码应该是:

   [[ $i == '35' ]]

提示:索引不需要单独的变量 i。可以直接使用变量counter.

喜欢:

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