jeudi 23 septembre 2021

Bash/Shell: Why am I getting the wrong output for if-else statements? [duplicate]

So here's a simple script to evaluate a string length and print whether it is greater than or less than a value.

#!/bin/sh

FILENAME="thisislongerthantencharacters"
echo $FILENAME
echo "$FILENAME" | wc -c 
if [ $( "$FILENAME" | wc -c ) -gt 10 ]
    then echo "FILENAME is longer than 10 characters for: $FILENAME."

elif [ $( "$FILENAME" | wc -c ) -lt 10 ]
    then echo "FILENAME is less than 10 characters for: $FILENAME."
else
    echo "None of the above"
fi

For some reason, I only get the output that it is less than, regardless of the string length. Output:

thisislongerthantencharacters
30
FILENAME is less than 10 characters for: thisislongerthantencharacters.

May I know what I am doing wrong exactly? And why is the character count a bit off?

Aucun commentaire:

Enregistrer un commentaire