Saya menulis skrip yang dipanggil saat pengguna masuk dan memeriksa apakah folder tertentu ada atau ada symlink yang rusak. (Ini pada sistem Mac OS X, tetapi pertanyaannya murni bash).
Itu tidak elegan, dan tidak berfungsi, tetapi sekarang ini terlihat seperti ini:
#!/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."
Inti masalahnya adalah bahwa ekspansi tilde tidak berfungsi seperti yang saya inginkan.
Katakanlah saya memiliki nama pengguna george
, dan folder rumahnya adalah /a/path/to/georges_home
. Jika, pada suatu shell, saya mengetik:
cd ~george
itu membawa saya ke direktori yang sesuai. Jika saya mengetik:
HOME_DIR=~george
echo $HOME_DIR
Ini memberi saya:
/a/path/to/georges_home
Namun, jika saya mencoba menggunakan variabel, itu tidak berfungsi:
USERNAME="george"
cd ~$USERNAME
-bash: cd: ~george: No such file or directory
Saya sudah mencoba menggunakan tanda kutip dan backticks, tetapi tidak tahu bagaimana membuatnya berkembang dengan baik. Bagaimana saya membuat ini bekerja?
Tambahan
Saya hanya ingin memposting skrip saya yang sudah selesai (sungguh, itu tidak seburuk pekerjaan yang sedang berjalan di atas!) Dan mengatakan bahwa itu tampaknya berfungsi dengan benar.
#!/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