我已经对此进行了搜索,但我猜使用路径的需求并不大。因此,我正在尝试编写一个bash脚本来使用tta和cue文件转换我的音乐收藏。我的目录结构如下:用于tta文件的/Volumes/External/Music/Just/Some/Dirs/Album.tta和用于提示表的/Volumes/External/Cuesheets/Just/Some/Dirs/Album.cue。
我当前的方法是将/Volumes/External设置为"root_dir“,并将album.tta文件的相对路径设置为$ROOT_DIR/Music (在本例中为/Some/Dirs/Alum.tta),然后将此结果添加到$ROOT_DIR/Cuesheets中,并将后缀从.tta更改为.cue。
我当前的问题是,当我当前的文件夹是$ROOT_DIR/Music并且给定了绝对路径时,dirname返回的路径是原样的,这意味着/Volumes/External/Music/Just/Some/DIR不会转换为./Just/Some/DIR/。
如果任何人有类似的问题,请添加以下脚本:
#!/bin/bash
ROOT_DIR=/Volumes/External
BASE="$1"
if [ ! -f "$BASE" ]
then
echo "Not a file"
exit 1
fi
if [ -n "$2" ]
then
OUTPUT_DIR="$HOME/tmp"
else
OUTPUT_DIR="$2"
fi
mkfdir -p "$OUTPUT_DIR" || exit 1
BASE=${BASE#"$ROOT_DIR/Music/"}
BASE=${BASE%.*}
TTA_FILE="$ROOT_DIR/Music/$BASE.tta"
CUE_FILE="$ROOT_DIR/Cuesheets/$BASE.cue"
shntool split -f "${CUE_FILE}" -o aiff -t "%n %t" -d "${OUTPUT_DIR}" "${TTA_FILE}"
exit 0发布于 2011-11-20 02:58:41
如果你的Cuesheets目录总是和你的音乐目录在同一个目录下,你只需要从路径中删除root_dir,剩下的就是相对路径了。如果您有album_path中的album.tta (album_path=/Volumes/External/Music/Just/Some/Dirs/Album.tta)和root_dir set(root_dir=/Volumes/External)的路径,只需执行${album_path#$root_dir}。这将从album_path的前面修剪root_dir,因此只剩下album_path=Just/Some/Dirs/Album.tta。
有关bash字符串操作的更多信息,请参见bash docs
编辑://将${$album_path#$root_dir}更改为${album_path#$root_dir}
发布于 2011-11-20 03:07:36
好的,我在过去用了几种方法来解决这个问题。我不建议修改paths和pwd环境变量,因为我已经看到了一些灾难性的事件。
这是我会做的
CURRENTDIR=/Volumes/External/Music # make sure you check the existence in your script
...
SEDVAL=$(echo $CURRENTDIR | sed s/'\/'/'\\\/'/g)
#run your loops for iterating through files
for a in $(find ./ -name \*ogg); do
FILE=`echo $a | sed s/$SEDVAL/./g` # strip the initial directory and replace it with .
convert_file $FILE # whatever action to be performed
done如果这是您可能经常做的事情,我实际上只会为此编写一个单独的脚本。
https://stackoverflow.com/questions/8196354
复制相似问题