Estoy escribiendo un script que recibe una llamada cuando un usuario inicia sesión y comprueba si existe una carpeta determinada o si hay un enlace simbólico roto. (Esto está en un sistema Mac OS X, pero la pregunta es puramente bash).
No es elegante y no funciona, pero en este momento se ve así:
#!/bin/bash
# Often users have a messed up cache folder -- one that was redirected
# but now is just a broken symlink. This script checks to see if
# the cache folder is all right, and if not, deletes it
# so that the system can recreate it.
USERNAME=$3
if [ "$USERNAME" == "" ] ; then
echo "This script must be run at login!" >&2
exit 1
fi
DIR="~$USERNAME/Library/Caches"
cd $DIR || rm $DIR && echo "Removed misdirected Cache folder" && exit 0
echo "Cache folder was fine."
El quid del problema es que la expansión de tilde no funciona como me gustaría.
Digamos que tengo un usuario llamado george
y que su carpeta de inicio es /a/path/to/georges_home
. Si, en un shell, escribo:
cd ~george
Me lleva al directorio apropiado. Si escribo:
HOME_DIR=~george
echo $HOME_DIR
Me da:
/a/path/to/georges_home
Sin embargo, si trato de usar una variable, no funciona:
USERNAME="george"
cd ~$USERNAME
-bash: cd: ~george: No such file or directory
He intentado usar comillas y comillas, pero no puedo encontrar la manera de expandirlo correctamente. ¿Cómo hago para que esto funcione?
Apéndice
Solo quería publicar mi guión completo (en realidad, ¡no es tan feo como el trabajo en progreso anterior!) Y decir que parece estar funcionando bien.
#!/bin/bash
# Often users have a messed up cache folder -- one that was redirected
# but now is just a broken symlink. This script checks to see if
# the cache folder is all right, and if not, deletes it
# so that the system can recreate it.
#set -x # turn on to help debug
USERNAME=$3 # Casper passes the user name as parameter 3
if [ "$USERNAME" == "" ] ; then
echo "This script must be run at login!" >&2
exit 1 # bail out, indicating failure
fi
CACHEDIR=`echo $(eval echo ~$USERNAME/Library/Caches)`
# Show what we've got
ls -ldF "$CACHEDIR"
if [ -d "$CACHEDIR" ] ; then
# The cache folder either exists or is a working symlink
# It doesn't really matter, but might as well output a message stating which
if [ -L "$CACHEDIR" ] ; then
echo "Working symlink found at $CACHEDIR was not removed."
else
echo "Normal directory found at $CACHEDIR was left untouched."
fi
else
# We almost certainly have a broken symlink instead of the directory
if [ -L "$CACHEDIR" ] ; then
echo "Removing broken symlink at $CACHEDIR."
rm "$CACHEDIR"
else
echo "Abnormality found at $CACHEDIR. Trying to remove." >&2
rm -rf "$CACHEDIR"
exit 2 # mark this as a bad attempt to fix things; it isn't clear if the fix worked
fi
fi
# exit, indicating that the script ran successfully,
# and that the Cache folder is (almost certainly) now in a good state
exit 0