LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   bash script to create folders including making recursive folders.... (http://www.linuxquestions.org/questions/linux-software-2/bash-script-to-create-folders-including-making-recursive-folders-771900/)

linux-bandit 11-27-2009 07:11 AM

bash script to create folders including making recursive folders....
 
hi all,


im trying to write a script in the bash shell. but im stuck. (noob)



the command im trying to do is.


CLIENT_NAME= name put in on command line


mkdir -p /srv/store/nearling-storage/clients/{$CLIENT_NAME/audio,graphics,file,test/{test-staging,TEST-EXPORT},test1/{gra,aud}}



currently the files are being made incorrectly, which is another issue. ive got the above wrong i think. (well i know duh.... )



ideally what i need is a script to run from the command line...


so ./scriptname -newclientname

and it then create the above folders.


anyhelp would be great.

thanks

catkin 11-27-2009 07:24 AM

Quote:

Originally Posted by linux-bandit (Post 3771325)
the command im trying to do is.

CLIENT_NAME= name put in on command line

mkdir -p /srv/store/nearling-storage/clients/{$CLIENT_NAME/audio,graphics,file,test/{test-staging,TEST-EXPORT},test1/{gra,aud}}

currently the files are being made incorrectly, which is another issue. ive got the above wrong i think. (well i know duh.... )

What sort of "incorrectly"? If the above is wrong and it is the only specification you have given us how can we know what you are trying to achieve?

If the client was XX, would you want to create these folders
Code:

/srv/store/nearling-storage/clients/XX/audio
/srv/store/nearling-storage/clients/XX/graphics
/srv/store/nearling-storage/clients/XX/file
/srv/store/nearling-storage/clients/XX/test/test-staging
/srv/store/nearling-storage/clients/XX/test/TEST-EXPORT
/srv/store/nearling-storage/clients/XX/test1/gra
/srv/store/nearling-storage/clients/XX/test1/aud


anitemp 11-27-2009 09:48 AM

Try getopt
 
I think 'getopt' command is what you're looking for. If '-newclientname' is of the type '-newclientname=XXX' then you need to parse XXX out before using it.

linux-bandit 11-27-2009 11:52 PM

Quote:

Originally Posted by catkin (Post 3771335)
What sort of "incorrectly"? If the above is wrong and it is the only specification you have given us how can we know what you are trying to achieve?

If the client was XX, would you want to create these folders
Code:

/srv/store/nearling-storage/clients/XX/audio
/srv/store/nearling-storage/clients/XX/graphics
/srv/store/nearling-storage/clients/XX/file
/srv/store/nearling-storage/clients/XX/test/test-staging
/srv/store/nearling-storage/clients/XX/test/TEST-EXPORT
/srv/store/nearling-storage/clients/XX/test1/gra
/srv/store/nearling-storage/clients/XX/test1/aud


sorry i thought about that exact thing the moment i shut my pc down last night. catkin, you are correct this is what i am after. where XX would be passed in at time of running script, or even a prompt.


thanks ( sorry for the confusion )

GrapefruiTgirl 11-28-2009 12:01 AM

Quote:

Originally Posted by anitemp (Post 3771474)
I think 'getopt' command is what you're looking for. If '-newclientname' is of the type '-newclientname=XXX' then you need to parse XXX out before using it.

getopt is probably fine too, but to simplify matters, why not just pass the clientname as the argument to the script? Like:

shell$ ./script john

for client john? And if $1 happens to be empty when the script error-checks for the clientname, exit with an error message (and of course, make sure john doesn't already exist too!)

linux-bandit 11-28-2009 01:32 AM

ive had a though, i might aswell just do, mkdir 7 times. was trying to be to clever, but the definition of clever is to realise that there may be a better way of doing something, but if it takes longer than the simpler version to get working and achieves the same thing...

silly me. ive got exactly what i need now anyway. momment of madness. sorry for the stupid post. thanks for the help.

catkin 11-28-2009 01:38 AM

Quote:

Originally Posted by linux-bandit (Post 3772023)
sorry i thought about that exact thing the moment i shut my pc down last night. catkin, you are correct this is what i am after. where XX would be passed in at time of running script, or even a prompt.


thanks ( sorry for the confusion )

GrapefruiTgirl's suggestion regards the script sounds good so
Code:

#!/bin/bash

[[ $# -ne 1 || "$1" == '' ]] && echo "Usage: ${0##*/} client_name" >&2 && \exit 1

CLIENT_NAME="$1"

dirs=( $( echo /srv/store/nearling-storage/clients/$CLIENT_NAME/{audio,graphics,file,test/{test-staging,TEST-EXPORT},test1/{gra,aud}} ) )
for dir in ${dirs[@]}
do
    if [[ -e "$dir" ]]; then
        echo "$dir exists!" >&2
        \exit 1
    else
        mkdir -p "$dir"
        [[ $? -ne 0 ]] && \exit 1
        echo "Created directory $dir"
    fi
done

This will fail if the client name includes embedded whitespace such as a space character.

catkin 11-28-2009 01:46 AM

Quote:

Originally Posted by linux-bandit (Post 3772068)
ive had a though, i might aswell just do, mkdir 7 times. was trying to be to clever, but the definition of clever is to realise that there may be a better way of doing something, but if it takes longer than the simpler version to get working and achieves the same thing...

Truly there is a pragmatic trade-off between the perfect and how long it takes to achieve! But it's good to trap and report errors and that would mean a lot of coding for 7 separate mkdirs. And it's good to push the envelope a bit, learning more of bash's features (until the point of forgetting them faster than learning them!).

Thanks for making me learn about the {<string>[,<string> ...]} technique -- it's useful but I have never used it :doh:

GrapefruiTgirl 11-28-2009 01:50 AM

Quote:

Originally Posted by catkin (Post 3772075)
Thanks for making me learn about the {<string>[,<string> ...]} technique -- it's useful but I have never used it :doh:

Me neither -- first time I have looked at it is right here AFAIK. Thanks catkin!

I was in the process of making a simple sed to replace any spaces with underscores, but then realized this would mean other (unnecessary and excessive) mods to catkins script, to grab >1 words given as a client_name and make them one name. If the OP wishes for this, it wouldn't be too hard, but as it stands, this looks adequate :)

Sasha


All times are GMT -5. The time now is 07:56 AM.