通过mkdir创建目录
我想在 /tmp/vnc/ 中创建一个以用户名命名的文件夹,我可以使用 perl -e 'mkdir("$ENV{USER}")' 在命令行中创建该文件夹
,但对于以下代码无法工作。
chdir ("/tmp/vnc") or die -1;
mkdir ("$ENV{USER}", 0777) or die -1;
如果我在命令行中使用 mkdir -p /tmp/vnc/$ENV{USER} 来创建文件夹,则不会发生任何情况,也不会报告任何错误。
I want to create a folder named by the user name in /tmp/vnc/, I can create that folder in command line with perl -e 'mkdir("$ENV{USER}")'
, but for the following code cannot work.
chdir ("/tmp/vnc") or die -1;
mkdir ("$ENV{USER}", 0777) or die -1;
If I use mkdir -p /tmp/vnc/$ENV{USER}
in command line to make folder, nothing happens and no error reports.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这对我有用。
也许
/tmp/vnc
目录不存在,并且chdir
失败。或者可能未定义
$USER
环境变量,因为您是从 init.d 脚本运行它,例如......或者您可能没有
/tmp/vnc
目录的写入权限。您是否尝试过从 shell 执行mkdir /tmp/vnc/$USER
?没有细节就不可能了解更多。
It works for me.
Maybe the
/tmp/vnc
directory does not exist, and thechdir
fails.Or maybe the
$USER
environment variable is not defined, because you are running it from a init.d script, for example...Or maybe you do not have write permissions in the
/tmp/vnc
directory. Have you tried executingmkdir /tmp/vnc/$USER
from the shell?Impossible to know more without details.
也许当您运行脚本时某些东西正在重置您的环境?您可以打印
$ENV{USER}
的内容并确保它包含您认为应该包含的内容吗?Perhaps something is resetting your environment when you are running the script? Can you print the contents of
$ENV{USER}
and make sure it contains what you think it should?如果 $ENV{USR} 是绝对路径(带有前导斜杠),则 chdir 是无用的,因为您没有使用相对路径
if $ENV{USR} is an absolute path (with the leading slash), then the chdir is useless since you are not using a relative path