将用户名传递给脚本,找到用户的主目录

Cli*_*ore 8 unix bash

我正在编写一个脚本,当用户登录并检查某个文件夹是否存在或是否是损坏的符号链接时,该脚本会被调用。(这是在 Mac OS X 系统上,但问题纯粹是 bash)。

它不优雅,也不起作用,但现在它看起来像这样:

#!/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."
Run Code Online (Sandbox Code Playgroud)

问题的关键是波浪号扩展没有像我想要的那样工作。

假设我有一个名为 的用户george,他的主文件夹是/a/path/to/georges_home. 如果在 shell 中,我输入:

cd ~george
Run Code Online (Sandbox Code Playgroud)

它带我到适当的目录。如果我输入:

HOME_DIR=~george
echo $HOME_DIR
Run Code Online (Sandbox Code Playgroud)

它给了我:

/a/path/to/georges_home
Run Code Online (Sandbox Code Playgroud)

但是,如果我尝试使用变量,它不起作用:

USERNAME="george"
cd ~$USERNAME
-bash: cd: ~george: No such file or directory
Run Code Online (Sandbox Code Playgroud)

我尝试使用引号和反引号,但无法弄清楚如何使其正确扩展。我如何使这项工作?


附录

我只是想发布我完成的脚本(真的,它不像上面正在进行的工作那么难看!)并说它似乎工作正常。

#!/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  
Run Code Online (Sandbox Code Playgroud)

Mik*_*eyB 16

使用$(eval echo ...)

michael:~> USERNAME=michael
michael:~> echo ~michael
/home/michael
michael:~> echo ~$USERNAME
~michael
michael:~> echo $(eval echo ~$USERNAME)
/home/michael
Run Code Online (Sandbox Code Playgroud)

所以你的代码应该是这样的:

HOMEDIR="$(eval echo ~$USERNAME)"
Run Code Online (Sandbox Code Playgroud)