【发布时间】:2012-04-03 18:56:32
【问题描述】:
我的脚本正在读取和显示 id3 标签。如果该字段为空白,我试图让它回显未知,但我尝试的每个 if 语句都不起作用。 id3 标记是固定大小的,因此它们永远不会为空,但如果没有值,它们将被空白填充。即标题标签的长度为 30 个字符。到目前为止我已经尝试过
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 评估为 true 但回显 :$x: 它回显 ::
脚本
#!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
【问题讨论】:
-
Michal Politowski:
-z表示操作数的长度为零 -
@Peter.O: 是的,$x 是应该测试的字符串,而不是 $string。
-
是的,在我上传脚本之前,我以 $string 为例; $i 是字符串,它是数组中的一个值。我的循环可能有问题吗?
-
您还必须将
if [ $TAG="TAG" ]更改为if [ "$TAG" = "TAG" ]- 第一个版本仅向[命令发送一个非空参数,因此它始终返回true。 -
@yamikoWebs 除了 glenn jackman 的 评论之外,您还混淆了您的
$1和$i,以及head 4(?)head -c 4...解决了这些问题,并且您选择了if语句,脚本就可以工作了 :)
标签: linux string bash if-statement comparison