由于某些明显的原因,我无法辨别为什么以下bash片段中的条件“if”语句不起作用:
artist=$(audtool --current-song-tuple-data artist|sed -r 's/^[ ]*(.*)[ ]*$/\1/') ; echo "\$artist: $artist"
album=$(audtool --current-song-tuple-data album|sed -r 's/^[ ]*(.*)[ ]*$/\1/') ; echo "\$album: $album"
#need to isolate the three sections of info data available from '--current-song' option; they are 'artist', 'album-title' and 'song-title', except they are never strictly in the same order for all songs. Therfore by isolating them, and eliminating the 'artist' and 'album-title' based on their own vars it can be more certain the remaining var is the 'song-title'
t1=$(audtool --current-song|sed -r 's/^([^-]*)[ ]*-.*$/\1/'|sed -r 's/^[ ]*(.*)[ ]*$/\1/') ; echo "\$t1: ${t1}"
t2=$(audtool --current-song|sed -r 's/^[^-]*-[ ]*([^-]*)[ ]*-.*$/\1/'|sed -r 's/^[ ]*(.*)[ ]*$/\1/') ; echo "\$t2: ${t2}"
t3=$(audtool --current-song|sed -r 's/^[^-]*-[^-]*-[ ]*(.*)[ ]*$/\1/'|sed -r 's/^[ ]*(.*)[ ]*$/\1/') ; echo "\$t3: ${t3}"
if [[ "$t1" != "$artist" && "$t1" != "$album" ]] ; then #'if'
title="$t1"
elif [[ "$t2" != "$artist" && "$t2" != "$album" ]] ; then #'if'
title="$t2"
elif [[ "$t3" != "$artist" && "$t3" != "$album" ]] ; then #'if'
title="$t3"
fi
echo "\$title: ${title}"
终端输出:
$artist: Van Halen
$album: Best Of Volume 1
$t1: Van Halen
$t2: Best Of Volume 1
$t3: Unchained
$title: Van Halen
目的是隔离大胆播放的当前歌曲的名称,而不列出艺术家和专辑标题。但无论如何,var $title
总是以== var $artist
结束,这意味着否定的条件不起作用。
我实际上甚至将&&&嵌入if的'if'语句的一部分没有区别。 Var $title
仍设法== var $artist
。正如预期的那样,使用if [[ ! "$t1" == "$artist" && ! "$t1" == "$album" ]] ; then
也没有任何区别。
所以我很难理解为什么'if'语句没有取消vars $artist
和$album
等于(并因此被回应)为var $title
。如果有人能帮助指出我确信这是一个明显的疏忽,我会很感激帮助。
答案 0 :(得分:0)
我的系统中没有配置audtool。所以我假设了变量值,如下所示。但我也修改了if检查了一下。如果这不起作用,可能你会在变量值中有一些额外的字符(空格或制表符......)
sdlcb@Goofy-Gen:~/AMD$ cat ff
#!/bin/bash
artist="Van Halen"
album="Best Of Volume 1"
t1="Van Halen"
t2="Best Of Volume 1"
t3="Unchained"
echo "\$artist: ${artist}"
echo "\$album: ${album}"
echo "\$t1: ${t1}"
echo "\$t2: ${t2}"
echo "\$t3: ${t3}"
if [ "$t1" != "$artist" ] && [ "$t1" != "$album" ]
then
title="$t1"
elif [ "$t2" != "$artist" ] && [ "$t2" != "$album" ]
then
title="$t2"
elif [ "$t3" != "$artist" ] && [ "$t3" != "$album" ]
then
title="$t3"
fi
echo "\$title: ${title}"
sdlcb@Goofy-Gen:~/AMD$ ./ff
$artist: Van Halen
$album: Best Of Volume 1
$t1: Van Halen
$t2: Best Of Volume 1
$t3: Unchained
$title: Unchained
sdlcb@Goofy-Gen:~/AMD$