Getting relative paths in BASH

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I already searched for this, but I guess there was no great demand on working with paths. So I m trying two write a bash script to convert my music collection using tta and cue files. My directory structure is as following: /Volumes/External/Music/Just/Some/Dirs/Album.tta for the tta files and /Volumes/External/Cuesheets/Just/Some/Dirs/Album.cue for cue sheets.

My current approach is setting /Volumes/External as "root_dir" and get the relative path of the album.tta file to $ROOT_DIR/Music (in this case this would be Just/Some/Dirs/Album.tta), then add this result to $ROOT_DIR/Cuesheets and change the suffix from .tta to .cue.

My current problem is, that dirname returns paths as they are, which means /Volumes/External/Music/Just/Some/Dirs does not get converted to ./Just/Some/Dirs/ when my current folder is $ROOT_DIR/Music and the absolute path was given.

Add://Here is the script if anybody has similar problems:

#!/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

Answers

If your Cuesheets dir is always in the same directory as your Music, you can just remove root_dir from the path, and what is left is the relative path. If you have the path to your album.tta in album_path (album_path=/Volumes/External/Music/Just/Some/Dirs/Album.tta) and your root_dir set(root_dir=/Volumes/External), just do ${album_path#$root_dir}. This trims root_dir from the front of album_path, so you are left with album_path=Just/Some/Dirs/Album.tta.

http://tldp.org/LDP/abs/html/string-manipulation.html http://tldp.org/LDP/abs/html/string-manipulation.html

EDIT:// Changed ${$album_path#$root_dir} to ${album_path#$root_dir}

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/8196354/getting-relative-paths-in-bash

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils