これは私が新卒のときにNYのチームでOJTしたときに、Unix SAのKさんが使用していたものです。ちなみに当時、そのチームでよく使われているWindowManagerはratpoison… It’s really cool to work without mouse という感じでした^^;;
歴史のある会社で働いているとディレクトリがたくさんあり、階層も深くなっていると思います。たまに覚えている人もいますが、やはりどこかに書いておくのが現実的ですよね。to()を定義して使うと覚える必要がありません:)
まずは使い方から
$ to abc
$ pwd
/nfs/here/is/also/too/long/path/I/cant/remember
こんな感じで移動できます。また引数なしで使うと一覧を表示するので
$ to
Please choose from the following ‘to’ destinations:
hoge:/nfs/this/is/too/long/path/I/cant/remember
abc:/nfs/here/is/also/too/long/path/I/cant/remember
で関数は下記のようになります。.bashrcのようなrcファイルで定義すればよいです。zshでも動くはずです。
#-----------------------------------------------
# for those who can't remember the file place
#-----------------------------------------------
dest_type()
{
key=$1
check_host=`ypmatch $key hosts 2> /dev/null`
check_passwd=`ypmatch $key passwd 2> /dev/null`
if [ "${check_host}X" != "X" ]; then
echo host
elif [ "${check_passwd}X" != "X" ]; then
echo user
else
echo dir
fi
}
to()
{
mapfile=“$HOME/.custom/dir_maps.txt"
if [ ! -f $mapfile ]; then
echo "directory map file doesn't exist"
else
if [ "${1}X" = "X" ]; then
echo "Please choose from the following 'to' destinations:"
echo ""
cat $mapfile | tr ':' '\t\t'
else
key=$1
# the directory key takes precedence
line=`grep "^${key}:" $mapfile`
if [ "${line}X" = "X" ]; then
type=`dest_type $key`
else
type="dir"
fi
case $type in
'host')
echo "ssh $key"
ssh $key
;;
'user')
echo "~$key"
$_user_dir=`/bin/echo ~$key`
cd $_user_dir
;;
'dir')
dest=`echo $line | awk -F':' '{print $2}'`
echo "$dest"
cd $dest
;;
*)
echo "Unknown key: \"$key\""
;;
esac
fi
fi
}
でdir_maps.txtは下記のように定義します。
# $HOME/.custom/dir_maps.txt
hoge:/nfs/this/is/too/long/path/I/cant/remember
abc:/nfs/here/is/also/too/long/path/I/cant/remember
ちなみに to HOST でssh. to USERNAMEでユーザのホームに行けますが、私はあんまり使用していません。
以上です^^ Have fun :)