更改脚本目录用户的宿主目录中的shell脚本 [英] Change script directory to user's homedir in a shell script
问题描述
在我的bash脚本,我需要当前目录切换到用户的主目录。
In my bash script I need to change current dir to user's home directory.
如果我想改变用户的的富的家目录,在命令行中我可以这样做:
if I want to change to user's foo home dir, from the command line I can do:
cd ~foo
这工作得很好,但是当我做同样的距离的脚本的它告诉我:
./bar.sh: line 4: cd: ~foo: No such file or directory
喜欢它的接缝会是这样一个平凡的事情,但它不工作。这里有什么问题吗?我需要难逃〜或者丢失的报价或其他什么东西?
Seams like it would be such a trivial thing, but it's not working. What's the problem here? Do I need to escape the "~" or perhaps missing quotes or something else?
修改
当我说的用户的我不是指的电流的运行该脚本,但总体上任何的其他的用户系统上的用户
when I say user I don't mean current user that runs the script, but in general any other user on the system
修改
下面是脚本:
#!/bin/bash
user="foo"
cd ~$user
如果用户名是硬codeD像
if username is hardcoded like
cd ~foo
它的工作原理,但如果是在用户的变量则没有。缺少什么我在这里?
it works, but if it is in the user variable then it doesn't. What am I missing here?
推荐答案
什么
cd $(getent passwd foo | cut -d: -f6)
和
USER=foo
eval cd ~$USER
的作品,太(foo是用户名)
works, too (foo is the username)
这篇关于更改脚本目录用户的宿主目录中的shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!