if 语句不能正常工作

if 语句不能正常工作

我有一个循环比较字符串,如果匹配,它仍然不会将其踢出。设置有许多不同类型的文件扩展名,只想删除我不想保留的文件扩展名。更简单的方法是不知道目录中有什么,就是告诉它我想保留什么,然后让它删除其余的。它不起作用,它仍然会删除目录和子目录中的所有内容。这就是它在脚本中的基本设置方式,现在其中有两个循环,

find "$working_dir" -type f -name "*.*" | while [ $xf  -lt $numberToConvert ] ; 
do read FILENAME;


echo "before loop"
echo "path1 is -> "${path1}""
echo "Ext1 is -> "$ext1""

#Checks each movie file to see if it is just a not needed
sample    of the move to regain file space by deleting it



   j=$FILENAME
   xpath=${j%/*} 
   xbase=${j##*/}
   xfext=${xbase##*.}
   xpref=${xbase%.*}
   path1=${xpath}
   pref1=${xpref}
   ext1=${xfext}

for file in "${path1}" ; do 

echo "in for loop ext1 is -> "$ext1"" 

  if [[ "$ext1" != 'flac' || "$ext1" != 'mp3'  ]]; then

    echo "in loop if statement ext is -> "$ext1""
    echo "Removing "$FILENAME""

    removeme="$FILENAME"
    rm -v "$removeme"

  fi
done

if [[ "${ext1}" == 'mp3' || "${ext1}" == 'flac' ]] ; then 
# other code to do stuff to mp3 and flac files here within the outter loop

 fi
 #outter loop done statement
 done 

项的输出是:

before loop
path1 is -> /media/data/temp1/Joe Jackson - The Ultimate Collection/CD2
Ext1 is -> mp3

in for loop ext1 is -> mp3

in loop if statement ext is -> mp3
Removing /media/data/temp1/Joe Jackson - The Ultimate Collection/CD2/07 Joe Jackson - Be My Number Two.mp3
removed ‘/media/data/temp1/Joe Jackson - The Ultimate Collection/CD2/07 Joe Jackson - Be My Number Two.mp3’

Extension of before into first if statement foobar

Total Files Left are 41

第二个 if 语句有效,它只允许输入 mp3 和 flac,并且一直在工作我只是决定尝试这个来删除现在目录中不是 mp3 或 flac 的其余文件,并且,我已经尝试过我能想到的所有变体,包括 var 名称周围的引号“$var”“$var”和方括号“${var}”以及 if 语句上的 [ ] 和 [[ ]]。似乎没有任何作用。无论如何它都会删除所有内容。我已经知道在字符串比较中 == 等于并且 != 不等于。

http://tldp.org/LDP/abs/html/comparison-ops.html

答案1

您的第一个测试的布尔逻辑是错误的:

if [[ "$ext1" != 'flac' || "$ext1" != 'mp3'  ]]

每一个file 匹配:如果您的分机号是“mp3”,则为"$ext1" != 'flac'true。

你想要其中之一

if [[ "$ext1" != 'flac' && "$ext1" != 'mp3'  ]]
if ! [[ "$ext1" == 'flac' || "$ext1" == 'mp3'  ]]
if [[ ! ("$ext1" == 'flac' || "$ext1" == 'mp3')  ]]

相关内容