正如问题所暗示的,我正在努力将我的脚本压缩为尽可能少的代码行,而不是声明多个if
语句。
目标是在 audacity 中创建一个自动化脚本,以规范化我的音乐库的音频。窗口检测和其他功能都有效,我只是想知道如何将其压缩以使用更少的代码来运行。
提前感谢您的帮助。
#!/bin/bash
media=0
loop=0
cd "/home/satan/Music/.processing/"
while [[ $media -eq 0 ]]
do
if [[ "$media" -gt '0' ]]
then
break
else
cd *
fi
aa=`ls -1 *.aa 2>/dev/null | wc -l`
if [ $aa != 0 ]
then
media=$((aa+media))
echo $aa "aa files found in"
pwd
echo
ls -1
echo
fi
aac=`ls -1 *.aac 2>/dev/null | wc -l`
if [ $aac != 0 ]
then
media=$((aac+media))
echo $aac "aac files found in"
pwd
echo
ls -1
echo
fi
aax=`ls -1 *.aax 2>/dev/null | wc -l`
if [ $aax != 0 ]
then
media=$((aax+media))
echo $aax "aax files found in"
pwd
echo
ls -1
echo
fi
act=`ls -1 *.act 2>/dev/null | wc -l`
if [ $act != 0 ]
then
media=$((act+media))
echo $act "act files found in"
pwd
echo
ls -1
echo
fi
aiff=`ls -1 *.aiff 2>/dev/null | wc -l`
if [ $aiff != 0 ]
then
media=$((aiff+media))
echo $aiff "aiff files found in"
pwd
echo
ls -1
echo
fi
alac=`ls -1 *.alac 2>/dev/null | wc -l`
if [ $alac != 0 ]
then
media=$((alac+media))
echo $alac "alac files found in"
pwd
echo
ls -1
echo
fi
ape=`ls -1 *.ape 2>/dev/null | wc -l`
if [ $ape != 0 ]
then
media=$((ape+media))
echo $ape "ape files found in"
pwd
echo
ls -1
echo
fi
au=`ls -1 *.au 2>/dev/null | wc -l`
if [ $au != 0 ]
then
media=$((au+media))
echo $au "au files found in"
pwd
echo
ls -1
echo
fi
dss=`ls -1 *.dss 2>/dev/null | wc -l`
if [ $dss != 0 ]
then
media=$((dss+media))
echo $dss "dss files found in"
pwd
echo
ls -1
echo
fi
dvf=`ls -1 *.dvf 2>/dev/null | wc -l`
if [ $dvf != 0 ]
then
media=$((dvf+media))
echo $dvf "dvf files found in"
pwd
echo
ls -1
echo
fi
flac=`ls -1 *.flac 2>/dev/null | wc -l`
if [ $flac != 0 ]
then
media=$((flac+media))
echo $flac "flac files found in"
pwd
echo
ls -1
echo
fi
gsm=`ls -1 *.gsm 2>/dev/null | wc -l`
if [ $gsm != 0 ]
then
media=$((gsm+media))
echo $gsm "gsm files found in"
pwd
echo
ls -1
echo
fi
iklax=`ls -1 *.iklax 2>/dev/null | wc -l`
if [ $iklax != 0 ]
then
media=$((iklax+media))
echo $iklax "iklax files found in"
pwd
echo
ls -1
echo
fi
ivs=`ls -1 *.ivs 2>/dev/null | wc -l`
if [ $ivs != 0 ]
then
media=$((ivs+media))
echo $ivs "ivs files found in"
pwd
echo
ls -1
echo
fi
m4a=`ls -1 *.m4a 2>/dev/null | wc -l`
if [ $m4a != 0 ]
then
media=$((m4a+media))
echo $m4a "m4a files found in"
pwd
echo
ls -1
echo
fi
m4b=`ls -1 *.m4b 2>/dev/null | wc -l`
if [ $m4b != 0 ]
then
media=$((m4b+media))
echo $m4b "m4b files found in"
pwd
echo
ls -1
echo
fi
m4p=`ls -1 *.m4p 2>/dev/null | wc -l`
if [ $m4p != 0 ]
then
media=$((m4p+media))
echo $m4p "m4p files found in"
pwd
echo
ls -1
echo
fi
mmf=`ls -1 *.mmf 2>/dev/null | wc -l`
if [ $mmf != 0 ]
then
media=$((mmf+media))
echo $mmf "mmf files found in"
pwd
echo
ls -1
echo
fi
movpkg=`ls -1 *.movpkg 2>/dev/null | wc -l`
if [ $movpkg != 0 ]
then
media=$((movpkg+media))
echo $movpkg "movpkg files found in"
pwd
echo
ls -1
echo
fi
mpc=`ls -1 *.mpc 2>/dev/null | wc -l`
if [ $mpc != 0 ]
then
media=$((mpc+media))
echo $mpc "mpc files found in"
pwd
echo
ls -1
echo
fi
mp3=`ls -1 *.mp3 2>/dev/null | wc -l`
if [ $mp3 != 0 ]
then
media=$((mp3+media))
echo $mp3 "mp3 files found in"
pwd
echo
ls -1
echo
fi
msv=`ls -1 *.msv 2>/dev/null | wc -l`
if [ $msv != 0 ]
then
media=$((msv+media))
echo $msv "act files found in"
pwd
echo
ls -1
echo
fi
nmf=`ls -1 *.nmf 2>/dev/null | wc -l`
if [ $nmf != 0 ]
then
media=$((nmf+media))
echo $nmf "nmf files found in"
pwd
echo
ls -1
echo
fi
ogg=`ls -1 *.ogg 2>/dev/null | wc -l`
if [ $ogg != 0 ]
then
media=$((ogg+media))
echo $ogg "ogg files found in"
pwd
echo
ls -1
echo
fi
oga=`ls -1 *.oga 2>/dev/null | wc -l`
if [ $oga != 0 ]
then
media=$((oga+media))
echo $oga "oga files found in"
pwd
echo
ls -1
echo
fi
mogg=`ls -1 *.mogg 2>/dev/null | wc -l`
if [ $mogg != 0 ]
then
media=$((mogg+media))
echo $mogg "mogg files found in"
pwd
echo
ls -1
echo
fi
opus=`ls -1 *.opus 2>/dev/null | wc -l`
if [ $opus != 0 ]
then
media=$((opus+media))
echo $opus "opus files found in"
pwd
echo
ls -1
echo
fi
ra=`ls -1 *.ra 2>/dev/null | wc -l`
if [ $ra != 0 ]
then
media=$((ra+media))
echo $ra "ra files found in"
pwd
echo
ls -1
echo
fi
rm=`ls -1 *.rm 2>/dev/null | wc -l`
if [ $rm != 0 ]
then
media=$((rm+media))
echo $rm "rm files found in"
pwd
echo
ls -1
echo
fi
raw=`ls -1 *.raw 2>/dev/null | wc -l`
if [ $raw != 0 ]
then
media=$((raw+media))
echo $raw "raw files found in"
pwd
echo
ls -1
echo
fi
rt64=`ls -1 *.rt64 2>/dev/null | wc -l`
if [ $rt64 != 0 ]
then
media=$((rt64+media))
echo $rt64 "rt64 files found in"
pwd
echo
ls -1
echo
fi
sln=`ls -1 *.sln 2>/dev/null | wc -l`
if [ $sln != 0 ]
then
media=$((sln+media))
echo $sln "sln files found in"
pwd
echo
ls -1
echo
fi
tta=`ls -1 *.tta 2>/dev/null | wc -l`
if [ $tta != 0 ]
then
media=$((tta+media))
echo $tta "tta files found in"
pwd
echo
ls -1
echo
fi
voc=`ls -1 *.voc 2>/dev/null | wc -l`
if [ $voc != 0 ]
then
media=$((voc+media))
echo $voc "voc files found in"
pwd
echo
ls -1
echo
fi
vox=`ls -1 *.vox 2>/dev/null | wc -l`
if [ $vox != 0 ]
then
media=$((vox+media))
echo $vox "vox files found in"
pwd
echo
ls -1
echo
fi
wav=`ls -1 *.wav 2>/dev/null | wc -l`
if [ $wav != 0 ]
then
media=$((wav+media))
echo $wav "wav files found in"
pwd
echo
ls -1
echo
fi
wma=`ls -1 *.wma 2>/dev/null | wc -l`
if [ $wma != 0 ]
then
media=$((wma+media))
echo $wma "wma files found in"
pwd
echo
ls -1
echo
fi
webm=`ls -1 *.webm 2>/dev/null | wc -l`
if [ $webm != 0 ]
then
media=$((webm+media))
echo $webm "webm files found in"
pwd
echo
ls -1
echo
fi
cda=`ls -1 *.cda 2>/dev/null | wc -l`
if [ $cda != 0 ]
then
media=$((cda+media))
echo $cda "cda files found in"
pwd
echo
ls -1
echo
fi
threegp=`ls -1 *.3gp 2>/dev/null | wc -l`
if [ $threegp != 0 ]
then
media=$((threegp+media))
echo $threegp "3gp files found in"
pwd
echo
ls -1
echo
fi
eightsvx=`ls -1 *.8svx 2>/dev/null | wc -l`
if [ $eightsvx != 0 ]
then
media=$((eightsvx+media))
echo $eightsvx "8svx files found in"
pwd
echo
ls -1
echo
fi
done
nohup pcmanfm >/dev/null 2>&1 &
我无法弄清楚您要用
loop
和media
变量或但是做什么cd *
,否则,这似乎就是您要做的事情:显然,
...
用您感兴趣的其余后缀替换,但您可能更喜欢只使用目录中存在的任何后缀的脚本,而不是在脚本中硬编码列表,例如使用 GNU sed 和 GNU sort-z
:以上两项均未经测试。
bash for 循环可以处理这个问题。
如果您不介意切换到 Z shell(
zsh
),那么您可以更紧凑地完成此操作。例如,给定
files=( *.*(N) )
当前目录中所有带有“点扩展名”的非隐藏文件的数组,您可以获取它们的唯一扩展名的数组,并且可以使用获取${(u)files:e}
与给定扩展名匹配的所有文件名的数组。将它们放在一起并添加标志以获取每个结果中的元素数量:.ext
${(M)files:#*.ext}
#
如果愿意的话,可以将循环放在匿名函数中并直接在 glob 扩展中调用它:
TL;DR:使用提取函数重构来减少重复代码,例如 41 个 if 语句。或者使用 Bash 的数组扩展将所有文件类型计为一行。
代码异味和重构
您发现了 41 次非常相似的代码块。我们称之为代码异味:重复代码。
这是重复的代码块(唯一的具体区别是文件扩展名):
代码复制和提取功能
对于代码重复,建议的重构是提取函数。
让我们编写一个函数,以媒体文件扩展名
aa
(对于.aa
文件)作为参数并返回结果(即此扩展名的文件数):注意:为了压缩输出,我删除了文件列表输出
echo;ls -1;echo
并内联了pwd
。前面的计数累计类似
media=$((aa+media))
可以通过添加返回值来实现:环形
接下来,按照rfm 的回答,在循环中重复使用此函数:
另请参阅:在 Bash 中循环遍历字符串数组? - Stack Overflow。
完整且简化的脚本
替代方案:使用 Bash 的数组扩展来
ls
计数文件Usually you can use brace-expansion to specify ranges that are expanded by Bash. For example
ls *.{mp3,aac}
gets expanded to a sequence of 2 commands executed in order:ls *.mp3; ls *.aac
.You also can use parentheses to assign arrays like
suffixes=(mp3 aac)
and without braces expand these array elements in a command likels *.$suffixes
.Together we can simplify the count of files by all given file-extensions to:
This prints the total count of all files found by given suffixes.
References