简体   繁体   中英

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

I'm trying to create new directory /proj with subdirectories bin, cgi, sbin, etc... if /proj does not exist. However, instead of creating multiple subdirectories, it creates a single subdirectory named {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

What am I missing to make the subdirectories I want?

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

FYI:
The following means, the directory proj is in / .

if [ -d /proj ]; then

EDIT

One probability I am seeing is (may not be true):

In the comment your are saying that you are able to create directory with mkdir -p proj/{Changes,Makefile,bin,cgi,doc,etc,html,lib,sbin,src} . But after that, you are saying that "when I cd into /proj, it's still one single subdirectory". May be here is your problem.

You may already have such a directory called /proj .;-)

I may have found a clue :

" Brace expansion is enabled via the "set -B" command and the "-B" command line option to the shell and disabled via "set +B" and "+B" on the command line. "

Is it possible that some other script that you run (maybe automatically, or through .bashrc ) "turns off" brace expansion? Here are some tests I did:

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

Here is where it gets a little bit crazy though... If I create a simple shell script testbrace- :

#!/bin/bash -B
echo {1..3}

and run it from the command line like so:

floris% ./testbrace-
{1..3}

the equivalent script testbrace+ :

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

gives me

floris% ./testbrace+
{1..3}

In other words, the +-B flag doesn't seem to affect the way the script is run. In either case, brace expansion is DISABLED.

HOWEVER, when I specifically invoke bash from the command line with a flag, I CAN affect the outcome:

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

As you can see, the script will perform brace expansion IFF I call it with /bin/bash -B scriptname - at which point it ignores the B flag inside the script. I expect that you will get the same result if you use this method for calling your directory-creating script.

Also, make sure you change the next line to

if [ -d ./proj ]

as several people pointed out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM