我只需要使用 bash 并且我是新手
#!/opt/bin/bash
SAVEIFS=$IFS
IFS=$'\n'
array=($(mysql --host=xxxx --user=xxxx --password=xxxx -s -N -e 'use xbmc_video; SELECT strFilename FROM movie, files WHERE files.idFile=movie.idFile ORDER BY idMovie DESC LIMIT 10;'))
自从我在 Windows samba 共享上工作以来,这会生成一个文件名数组,其中包含空格。问题是如何删除每个字符串中的最后 4 个符号以摆脱扩展名,而不必担心我想要获取纯文件名的扩展名
最佳答案
将此添加到脚本的末尾:
for i in ${!array[@]}; do
array[i]="${array[i]%%.???}"
done
这里使用了两个技巧:
为了确保(稍后当您使用数组时)您获得文件的完整名称,请在循环中使用以下技巧:
for file in "${array[@]}"; do # the double-quotes are important
echo "$file"
done
有关详细信息,请参阅 the bash hackers site和 bash manual
关于arrays - 用 bash 替换数组中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7483491/