Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Linux中通过shell脚本创建新目录?_Linux_Shell - Fatal编程技术网

如何在Linux中通过shell脚本创建新目录?

如何在Linux中通过shell脚本创建新目录?,linux,shell,Linux,Shell,我正在尝试创建新目录/proj,其中包含子目录bin、cgi、sbin等。如果/proj不存在。但是,它不是创建多个子目录,而是创建一个名为{Changes…src} #If project directory exists, then do not create a new one. if [ -d /proj ]; then echo "Project directory exists. New directory will

我正在尝试创建新目录
/proj
,其中包含子目录
bin、cgi、sbin等。
如果
/proj
不存在。但是,它不是创建多个子目录,而是创建一个名为
{Changes…src}

#If project directory exists, then do not create a new one.                             
if [ -d /proj ]; then
     echo "Project directory exists. New directory will not be created."

#Otherwise, create a new project directory.                                             
else
     echo "Project directory does not exist. Creating a new project directory..."
     mkdir -p proj/{Changes,Makefile,bin,cgi,doc,etc,html,lib,sbin,src}
fi
我缺少什么来创建我想要的子目录

sgeorge-mn:tmp sgeorge$ ls proj
ls: proj: No such file or directory
sgeorge-mn:tmp sgeorge$ mkdir -p proj/{Changes,Makefile,bin,cgi,doc,etc,html,lib,sbin,src}
sgeorge-mn:tmp sgeorge$ ls proj
Changes     Makefile    bin     cgi     doc     etc     html        lib     sbin        src
供参考:
以下表示目录
proj
位于
/

if [ -d /proj ]; then
编辑

我看到的一种可能性是(可能不是真的):

在评论中,您说您可以使用
mkdir-p proj/{Changes、Makefile、bin、cgi、doc等、html、lib、sbin、src}
创建目录。但在此之后,您会说“当我将cd放入/proj时,它仍然是一个子目录”。这可能是你的问题

您可能已经有一个名为
/proj
;-)的目录

我可能找到了一个:

通过“set-B”命令和shell的“-B”命令行选项启用支架扩展,并通过命令行上的“set+B”和“+B”禁用支架扩展

您运行的其他脚本(可能自动运行,或通过
.bashrc
)是否可能“关闭”大括号扩展?以下是我做的一些测试:

floris% /bin/bash -B
bash-3.2$ echo {1..3}
1 2 3
## ^^ brace expansion is ON

floris% /bin/bash +B
bash-3.2$ echo {1..3}
{1..3}
## ^^ brace expansion is OFF
这就是它变得有点疯狂的地方。。。如果我创建一个简单的shell脚本
testbrace-

#!/bin/bash -B
echo {1..3}
然后从命令行运行它,如下所示:

floris% ./testbrace-
{1..3}
等效脚本
testbrace+

#!/bin/bash +B
echo {1..3}
给我

floris% ./testbrace+
{1..3}
换句话说,+-B标志似乎不会影响脚本的运行方式。在这两种情况下,支架扩展都被禁用

但是,当我使用标志从命令行专门调用bash时,我会影响结果:

floris% /bin/bash +B testbrace-
{1..5}
floris% /bin/bash +B testbrace+
{1..5}
floris% /bin/bash -B testbrace-
1 2 3 4 5
floris% /bin/bash -B testbrace+
1 2 3 4 5
如您所见,如果我使用
/bin/bash-B scriptname
-调用它,脚本将执行大括号扩展,此时它将忽略脚本中的B标志。如果使用此方法调用目录创建脚本,我希望您将得到相同的结果

另外,请确保将下一行更改为

if [ -d ./proj ]

正如一些人指出的那样。

您使用的是什么外壳?我在
/bin/csh
/bin/bash
中测试了
mkdir-p proj/{a,b,c}
,并得到了预期的结果(所有子目录都已创建)。您的脚本在bash(版本4.2.42)中为我工作。(我确实添加了
#!/bin/bash
,并且删除了
项目前面的斜杠,在
if[-d proj];
行中。我不知道这是否有区别。)如果你键入
printenv | grep SHELL
,你应该知道你正在运行什么SHELL…@mrunion-我认为这没有区别;并不是说“没有目录”被创建,而是“一个长名称的目录”。所以问题肯定出在
mkdir
行上。@user1854603
echo proj/{Changes,Makefile,bin,cgi,doc等,html,lib,sbin,src}
。您对此的输出是什么。例如您所做的“FYI”编辑;所以你建议他应该使用
if[-d./proj]
,对吗?这仍然不能解释为什么他没有创建目录,而你(和我)是…@Floris,我也不理解这里的错误。对于用户1854603,您能从shell执行
echo proj/{Changes,Makefile,bin,cgi,doc等,html,lib,sbin,src}
,然后将其输出粘贴到您的问题中吗?@Suku,这是我得到的:proj/Changes proj/Makefile proj/bin proj/cgi proj/doc proj/etc proj/html proj/lib proj/sbin proj/src,但当我
cd
插入/proj时,它仍然是一个子目录。我用
mkdir-p./proj/{Changes,Makefile,bin,cgi,doc等,html,lib,sbin,src}
创建子目录没有问题,但在shell脚本中不起作用