必须确定所有用户的主目录 - 波形符脚本问题

发布于 2024-08-18 03:24:57 字数 255 浏览 8 评论 0原文

假设 someuser 有一个主目录 /home/someuser

NAME=someuser

在 bash 中 - 我使用什么表达式组合波浪号 (~) 和 $NAME 来返回用户主目录?

HOMEDIRECTORY=~someuser
echo $HOMEDIRECTORY
/home/someuser
NAME=someuser
echo ~$NAME
~someuser

有什么建议吗?

Assume someuser has a home directory /home/someuser

NAME=someuser

In bash - what expression to I use combining tilde (~) and $NAME to return the users home directory?

HOMEDIRECTORY=~someuser
echo $HOMEDIRECTORY
/home/someuser
NAME=someuser
echo ~$NAME
~someuser

any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

叶落知秋 2024-08-25 03:24:57

更安全

eval HOMEDIRECTORY="$(printf "~%q" "$NAME")"

此处 printf%q 选项引用并转义危险字符。

如果 $NAME 是 joe,您会得到类似 /home/joe 的内容。对于 root,您可能会得到 /root。对于“abc;rm some”,您会得到“~abc;rm some”,而不是删除某些内容。

Safer:

eval HOMEDIRECTORY="$(printf "~%q" "$NAME")"

Here the %q option to printf quotes and escapes dangerous characters.

If $NAME is joe, you'd get something like /home/joe. For root, you might get /root. For "abc;rm something" you'd get "~abc;rm something" instead of having something removed.

等你爱我 2024-08-25 03:24:57

如果您有权访问 getent

getent passwd "$NAME" | cut -d: -f 6

If you have access to getent:

getent passwd "$NAME" | cut -d: -f 6
十年不长 2024-08-25 03:24:57

bash 和 csh 之间有趣的区别,其中 ~$VARNAME 实际上执行了您所期望的操作!

这很难看,但它似乎可以在 bash 中工作:

homedir=`eval "echo ~$USERNAME"`

现在 $homedir 保存与 $USERNAME 关联的主目录。

Interesting difference between bash and csh, where ~$VARNAME actually does what you'd expect!

This is ugly, but it seems to work in bash:

homedir=`eval "echo ~$USERNAME"`

Now $homedir holds the home directory associated with $USERNAME.

空名 2024-08-25 03:24:57

最佳方法

必需:无
注意,这与 getent 技术相同,不需要 getent

home() { # returns empty string on invalid user
    grep "^$1:" /etc/passwd | cut -d ':' -f 6
}

# grep "^$user:" /etc/passwd | cut -d ':' -f 6
/var/lib/memcached

ROOT LINUX 的好方法

必需:Linux ,root(或sudo

home() { # returns errorlevel 1 on invalid user
    su "$1" -s '/bin/sh' -c 'echo $HOME'
} 

# su memcached -s '/bin/sh' -c 'echo $HOME'
/var/lib/memcached

完整扩展的解决方案

magic() { # returns unexpanded tilde express on invalid user
    local _safe_path; printf -v _safe_path "%q" "$1"
    eval "ln -sf $_safe_path /tmp/realpath.$"
    readlink /tmp/realpath.$
    rm -f /tmp/realpath.$
}

示例用法:

$ magic ~nobody/would/look/here
/var/empty/would/look/here

$ magic ~invalid/this/will/not/expand
~invalid/this/will/not/expand

利用 CSH 的方法

这是一个 BASH 脚本,它只是调用csh

必需:csh

home() { # return errorlevel 1 on invalid user
    export user=$1; csh -c "echo ~$user"
}

$ export user=root; csh -c "echo ~$user"
/var/root

$ export user=nodfsv; csh -c "echo ~$user"
Unknown user: nodfsv.

绝望方法

必需:finger(已弃用)

home() {
    finger -m "$1" | 
    grep "^Directory:" | 
    sed -e 's/^Directory: //' -e 's/ .*//'
}

# finger -m "haldaemon" | 
> grep "^Directory:" | 
> sed -e 's/^Directory: //' -e 's/ .*//'
/home/haldaemon

您可以将 grep 操作合并到 sed,但由于这种方法很糟糕,所以我不会打扰。

BEST METHOD

Required: nothing
(n.b., this is the same technique as getent without requiring getent)

home() { # returns empty string on invalid user
    grep "^$1:" /etc/passwd | cut -d ':' -f 6
}

# grep "^$user:" /etc/passwd | cut -d ':' -f 6
/var/lib/memcached

NICE METHOD FOR ROOT LINUX

Required: Linux, root (or sudo)

home() { # returns errorlevel 1 on invalid user
    su "$1" -s '/bin/sh' -c 'echo $HOME'
} 

# su memcached -s '/bin/sh' -c 'echo $HOME'
/var/lib/memcached

SOLUTION FOR COMPLETE EXPANSION

magic() { # returns unexpanded tilde express on invalid user
    local _safe_path; printf -v _safe_path "%q" "$1"
    eval "ln -sf $_safe_path /tmp/realpath.$"
    readlink /tmp/realpath.$
    rm -f /tmp/realpath.$
}

Example usage:

$ magic ~nobody/would/look/here
/var/empty/would/look/here

$ magic ~invalid/this/will/not/expand
~invalid/this/will/not/expand

METHOD FOR HARNESSING CSH

This is a BASH script, it just calls csh.

Required: csh

home() { # return errorlevel 1 on invalid user
    export user=$1; csh -c "echo ~$user"
}

$ export user=root; csh -c "echo ~$user"
/var/root

$ export user=nodfsv; csh -c "echo ~$user"
Unknown user: nodfsv.

METHOD OF DESPERATION

Required: finger (deprecated)

home() {
    finger -m "$1" | 
    grep "^Directory:" | 
    sed -e 's/^Directory: //' -e 's/ .*//'
}

# finger -m "haldaemon" | 
> grep "^Directory:" | 
> sed -e 's/^Directory: //' -e 's/ .*//'
/home/haldaemon

You can combined the grep operation into sed, but since this method is sucky, I wouldn't bother.

乱了心跳 2024-08-25 03:24:57

一种替代方法

awk -F":" '{print "user: "$1", Home directory is: "$6}' /etc/passwd

one alternative way

awk -F":" '{print "user: "$1", Home directory is: "$6}' /etc/passwd
-小熊_ 2024-08-25 03:24:57

波形符 ( ~ ) 与 $HOME 相同,因此并非所有用户都以 root 身份访问同一目录。

但是,如果您坚持使用波浪号,则可以完成此工作:

echo ~/../$NAME

请参阅:

$ pwd
/home/oreyes
$ export NAME=john 
$ export DIRECTORYNAME=~/../$NAME
$ cd $DIRECTORYNAME
$ pwd
/home/john

Tilde ( ~ ) it's the same as $HOME so, not all the user will have as root to home the same directory.

But if you insist in using the tilde this do the work:

echo ~/../$NAME

See:

$ pwd
/home/oreyes
$ export NAME=john 
$ export DIRECTORYNAME=~/../$NAME
$ cd $DIRECTORYNAME
$ pwd
/home/john
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文