LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-27-2009, 07:11 AM   #1
linux-bandit
LQ Newbie
 
Registered: Nov 2009
Posts: 3

Rep: Reputation: 0
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
 
Old 11-27-2009, 07:24 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by linux-bandit View Post
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
 
Old 11-27-2009, 09:48 AM   #3
anitemp
LQ Newbie
 
Registered: Oct 2008
Posts: 9

Rep: Reputation: 0
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.
 
Old 11-27-2009, 11:52 PM   #4
linux-bandit
LQ Newbie
 
Registered: Nov 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
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 )
 
Old 11-28-2009, 12:01 AM   #5
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Quote:
Originally Posted by anitemp View Post
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!)

Last edited by GrapefruiTgirl; 11-28-2009 at 12:02 AM.
 
Old 11-28-2009, 01:32 AM   #6
linux-bandit
LQ Newbie
 
Registered: Nov 2009
Posts: 3

Original Poster
Rep: Reputation: 0
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.
 
Old 11-28-2009, 01:38 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by linux-bandit View Post
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.
 
Old 11-28-2009, 01:46 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by linux-bandit View Post
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
 
1 members found this post helpful.
Old 11-28-2009, 01:50 AM   #9
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Quote:
Originally Posted by catkin View Post
Thanks for making me learn about the {<string>[,<string> ...]} technique -- it's useful but I have never used it
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to create folders recurssively Tekken Linux - Server 20 08-22-2009 10:15 AM
bash script that will check folder x for new folders. Techno Guy Linux - Newbie 14 08-08-2009 10:50 AM
Script to create folders and subfolders leupi Linux - Newbie 12 07-18-2008 10:39 AM
Cannot create folders with bash script called from php keyF Linux - Software 4 06-25-2006 10:58 AM
Samba can create new files and folders but access denied in any new folders k.king Linux - Networking 2 01-15-2006 06:14 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration