Am 25.02.2013 02:08, schrieb Chris
Down:
Here it is with the dependency on `awk' removed (no external dependencies):
tildeExpand() {
local path=$1
if [[ $path =~ ^~$ || $path =~ ^~/ ]]; then
path=${path/\~/$HOME}
elif [[ $path =~ ^~([[:alnum:]]+) ]]; then
user=${BASH_REMATCH[1]}
while IFS=: read -r pUser _ _ _ _ pHome _; do
if [[ $user == $pUser ]]; then
userHome=$pHome
break
fi
done < /etc/passwd
if [[ $userHome ]]; then
path=${path/\~$user/$userHome}
fi
fi
printf '%s\n' "$path"
}
Again, any suggestions for improvement welcome.
Best,
Chris
untested just example of how you could avoid most of the new bash
specific stuff you used. these operations should also be cheaper.
tildeExpand() {
local path pHome pUser userHome
case ${1%%/*} in
~) path="${HOME}${1#~}";;
[!~]* | ~*[![:alnum:]]*) path="${1}" ;;
*)
user=${1%%/*}
user=${user#~}
while IFS=: read -r pUser _ _ _ _ pHome _; do
if [[ $user == $pUser ]]; then
userHome=$pHome
break
fi
done < /etc/passwd
if [[ $userHome ]]; then
path=$userHome${1#~$user}
fi
;;
esac
printf '%s\n' "$path"
}