Test if string has non whitespace characters in Ba

2020-06-07 06:37发布

My script is reading and displaying id3 tags. I am trying to get it to echo unknown if the field is blank but every if statement I try will not work. The id3 tags are a fixed size so they are never null but if there is no value they are filled with white space. I.E the title tag is 30 characters in length. Thus far I have tried

echo :$string: #outputs spaces between the 2 ::

if [ -z "$string" ] #because of white space will always evaluate to true

x=echo $string | tr -d ' '; if [ -z "$string" ]; #still evaluates to true but echos :$x: it echos ::

the script

#!bin/bash
echo "$# files";
while [ "$i" != "" ];
do
   TAG=`tail -c 128 "$i" | head -c 3`;
   if [ $TAG="TAG" ]
   then
      ID3[0]=`tail -c 125 "$1" | head -c 30`;
      ID3[1]=`tail -c 95 "$1" | head -c 30`;
      ID3[2]=`tail -c 65 "$1" | head -c 30`;
      ID3[3]=`tail -c 35 "$1" | head 4`;
      ID3[4]=`tail -c 31 "$i" | head -c 28`;
      for i in "${ID3[@]}"
      do
         if [ "$(echo $i)" ] #the if statement mentioned
         then
            echo "N/A";
         else
            echo ":$i:";
         fi
      done
   else
      echo "$i does not have a proper id3 tag";
   fi
   shift;
done

8条回答
我欲成王,谁敢阻挡
2楼-- · 2020-06-07 06:56

You can use bash's regex syntax.

It requires that you use double square brackets [[ ... ]], (more versatile, in general).
The variable does not need to be quoted. The regex itself must not be quoted

for str in "         "  "abc      " "" ;do
    if [[ $str =~ ^\ +$ ]] ;then 
      echo -e "Has length, and contain only whitespace  \"$str\"" 
    else 
      echo -e "Is either null or contain non-whitespace \"$str\" "
    fi
done

Output

Has length, and contain only whitespace  "         "
Is either null or contain non-whitespace "abc      " 
Is either null or contain non-whitespace "" 
查看更多
一夜七次
3楼-- · 2020-06-07 06:59

Sharing a more portable method not requiring double-brackets [[ ]] while keeping it simple, as much as possible.

Detect a string containing ONLY white space or = null:

if [ -z "${string// }" ]; then <do something>; fi

Which in this case would be:

${var// }

Such as:

if [ -z "${string// }" ]; then echo "It's empty!"; fi

What this does is simply replace all white space with null through substitution. If $string contains only white space, the -z test will evaluate TRUE. If there are non-white space characters in the string, the equation evaluates to FALSE.

Elaborating further on BASH (since OP asked about this in BASH), the example above will not catch tabs, though it is possible to do so. Here are more examples in BASH that do and don't work as expected.

Let's say there's a tab in the search string.

This works:

string=$(echo -e "\t"); if [ -z ${string// } ]; then echo "It's empty!"; fi

But these don't:

string=$(echo -e "\t"); if [ -z "${string// }" ]; then echo "It's empty!"; fi
string=$(echo -e "\t"); if [ -z '${string// }' ]; then echo "It's empty!"; fi

The two last examples above both report the string is not empty. If you consider a tab to be white space, that would be a problem and you'd want to use the first example.

What about newline? Let's take a look at an \n scenario. First off, "correct" behavior depends on your expectations.

These treat newlines (\n) as white space (equation evaluates TRUE):

string=$(echo -e "\n\n"); if [ -z ${string// } ]; then echo "It's empty!"; fi
string=$(echo -e "\n\n"); if [ -z "${string// }" ]; then echo "It's empty!"; fi

This doesn't (equation evaluates to FALSE), meaning this equation thinks a newline is not white space.

string=$(echo -e "\n\n"); if [ -z '${string// }' ]; then echo "It's empty!"; fi

If you require checking for newlines and tabs and spaces, you may need to use two IF/THEN statements or the CASE method described above.

查看更多
小情绪 Triste *
4楼-- · 2020-06-07 07:00
if [ "$(echo "$string" | tr -s ' ')" == " " ]; then
  echo "all white space"
fi

That compresses all repeated spaces down to one space, and compares for that.

查看更多
Animai°情兽
5楼-- · 2020-06-07 07:06

With extended globs enabled (shopt -s extglob):

if [ -n "${string##+([[:space:]])}" ]; then
    echo '$string has non-whitespace characters'
fi
查看更多
ら.Afraid
6楼-- · 2020-06-07 07:06

This one checks for Zero length or SPACE or TAB

S="Something"
if [[ "x$S" == "x" || "x$S" == x*\ * || "x$S" == x*\    * ]] ;then
  echo "Is not OK"
else
  echo "Is OK"
fi
查看更多
爷、活的狠高调
7楼-- · 2020-06-07 07:18

A non-bash specific, shell only variant:

case "$string" in
 *[!\ ]*) echo "known";;
 *) echo "unknown";;
esac
查看更多
登录 后发表回答