bash - ( , , ):
pax> export x=a ; if [[ "${x%%_*}" != "${x}" ]]; then
...> export bkpdir=/backups/${x%%_*}/backup
...> else
...> export bkpdir=/backups/others/backup
...> fi
pax> echo " ${bkpdir}"
/backups/others/backup
pax> export x=a_b ; if [[ "${x%%_*}" != "${x}" ]]; then
...> export bkpdir=/backups/${x%%_*}/backup
...> else
...> export bkpdir=/backups/others/backup
...> fi
pax> echo " ${bkpdir}"
/backups/a/backup
if , , . , .
${x%%_*}gives you a line before deleting the longest template _*(in other words, it removes everything from the first underscore to the end).
A (slightly) simpler option:
export bkpdir=/backups/others/backup
if [[ "${x%%_*}" != "${x}" ]]; then
export bkpdir=/backups/${x%%_*}/backup
fi
source
share