簡體   English   中英

如何通過Linux中的shell腳本創建新目錄?

[英]How do I create a new directory through a shell script in Linux?

我正在嘗試使用子目錄bin, cgi, sbin, etc...創建新目錄/proj bin, cgi, sbin, etc...如果/proj不存在。 但是,它不是創建多個子目錄,而是創建一個名為{Changes...src}子目錄{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,etc,html,lib,sbin,src}創建目錄。 但在那之后,你說“當我進入/ 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

如您所見,腳本將執行大括號擴展IFF我用/bin/bash -B scriptname調用它 - 此時它忽略腳本中的B標志。 如果您使用此方法調用目錄創建腳本,我希望您將得到相同的結果。

另外,請確保將下一行更改為

if [ -d ./proj ]

幾個人指出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM