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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
11-27-2009, 07:11 AM
|
#1
|
LQ Newbie
Registered: Nov 2009
Posts: 3
Rep:
|
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
|
|
|
11-27-2009, 07:24 AM
|
#2
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Quote:
Originally Posted by linux-bandit
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
|
|
|
11-27-2009, 09:48 AM
|
#3
|
LQ Newbie
Registered: Oct 2008
Posts: 9
Rep:
|
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.
|
|
|
11-27-2009, 11:52 PM
|
#4
|
LQ Newbie
Registered: Nov 2009
Posts: 3
Original Poster
Rep:
|
Quote:
Originally Posted by catkin
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 )
|
|
|
11-28-2009, 12:01 AM
|
#5
|
LQ Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
Quote:
Originally Posted by anitemp
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.
|
|
|
11-28-2009, 01:32 AM
|
#6
|
LQ Newbie
Registered: Nov 2009
Posts: 3
Original Poster
Rep:
|
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.
|
|
|
11-28-2009, 01:38 AM
|
#7
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Quote:
Originally Posted by linux-bandit
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.
|
|
|
11-28-2009, 01:46 AM
|
#8
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Quote:
Originally Posted by linux-bandit
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.
|
11-28-2009, 01:50 AM
|
#9
|
LQ Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
Quote:
Originally Posted by catkin
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
|
|
|
All times are GMT -5. The time now is 01:41 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|