Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如果要工作,就不能得到否定条件_Bash_If Statement - Fatal编程技术网

Bash 如果要工作,就不能得到否定条件

Bash 如果要工作,就不能得到否定条件,bash,if-statement,Bash,If Statement,出于某种明显的原因,我无法理解为什么下面的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: $a

出于某种明显的原因,我无法理解为什么下面的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
这样做的目的是隔离当前由audacious播放的歌曲的名称,而不列出艺术家和专辑名称。但无论如何,var$title总是以==var$artist结尾,这意味着被否定的条件不起作用

实际上,我甚至将if语句的&&部分拆分为嵌套的if语句,这没有什么区别。Var$title仍然设法==Var$artist。正如预期的那样,使用if[[!$t1==$artist&&!$t1==$album]];那也没什么区别


因此,我被难住了,为什么“如果”声明没有取消vars$artist和$album的同等资格,因此被称为var$title。如果有人能帮我指出一个明显的疏忽,我将不胜感激。

我的系统中没有配置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$

所有变量的值是什么?调试任何shell脚本的第一步是将set-x放在开头,这样您就可以看到所有填充了变量的语句。此外,还要注意不可见字符、非打印字符、变量末尾的空格等。请尝试使用例如echo\$title:'${title}打印变量“| cat-vt使它们可见。好笑-悲哀我在脚本中从未使用过set-x选项。非常有用。谢谢你。我看到现在有一个空格被添加了,呃没有从三个变量$t1,t2和$t3的末尾删除,尽管我认为我实现了“sed”来删除变量赋值开始和结束时的所有假空格。哦,好吧,现在可以了,谢谢你的大力帮助,但很可能是显而易见的帮助。不要自责。只有当你不知道set-x并且不使用它时,你在做什么。。。呃…,非最佳;-。有趣的代码+1.感谢你努力解决自己的问题。祝你好运