The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Bash and Awk for creating directories and moving files Kiint Shell Programming and Scripting 11 07-24-2008 10:30 PM
help needed in creating an image from a 7za compressed file wrapster SUN Solaris 0 06-20-2008 10:59 AM
Bash shell: Creating Preferences airsmurf Shell Programming and Scripting 4 05-20-2008 02:49 AM
creating directories on the same box vivek_damodaran HP-UX 3 11-14-2007 03:06 PM
creating directories carlvernon UNIX for Dummies Questions & Answers 3 06-01-2006 01:45 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-28-2009
I-1 I-1 is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 26
help needed with creating challenging bash script with creating directories

Hi,

Can someone help me with creating a bash shell script.
I need to create a script that gets a positive number n as an argument.
The script must create n directories in the current directory with names like map_1, map_2 etcetera. Each directory must be contained within its predecessor. So map_1 contains map_2, map_2 contains map_3 and so forth.

Thanks for the help!
  #2 (permalink)  
Old 04-28-2009
durden_tyler's Avatar
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2009
Posts: 527
Quote:
Originally Posted by I-1 View Post
Hi,

Can someone help me with creating a bash shell script.
I need to create a script that gets a positive number n as an argument.
The script must create n directories in the current directory with names like map_1, map_2 etcetera. Each directory must be contained within its predecessor. So map_1 contains map_2, map_2 contains map_3 and so forth.

Thanks for the help!
Looks like a homework question, so here's a hint: given the number "n", loop from 1 through n and create a string that represents the directory tree you want. Then use the "-p" option of "mkdir" command to create the directory tree.

Hope that helps,
tyler_durden

______________________________________________
"Only after disaster can we be resurrected."
  #3 (permalink)  
Old 04-28-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361

Code:
n=1
while [ $n -le $1 ]
do
  dir=map_$n
  mkdir "$dir" && cd "$dir" || exit 1
  n=$(( $n + 1 ))
done
  #4 (permalink)  
Old 04-29-2009
I-1 I-1 is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 26
Code:
# n is 1 by default 
n=1
# this is a loop where it actually says when n is less then 1 or on do something
while [ $n -le $1 ]
# this part I don't understand
do
  dir=map_$n
  mkdir "$dir" && cd "$dir" || exit 1
  n=$(( $n + 1 ))
done
  #5 (permalink)  
Old 04-29-2009
devtakh devtakh is offline
Registered User
  
 

Join Date: Oct 2007
Location: Bangalore
Posts: 514
Quote:
Originally Posted by I-1 View Post
Code:
# n is 1 by default 
n=1
# this is a loop where it actually says when n is less then 1 or on do something
while [ $n -le $1 ]
# this part I don't understand
do
  dir=map_$n
  mkdir "$dir" && cd "$dir" || exit 1
  n=$(( $n + 1 ))
done
$1 represents the input parameter(first parameter). So if you supply 4 when running the script, $1 will be 4. In that case, the while loop will be like
while [ 1 -le 4 ]...and then increment 1 by 1..till it reaches 4.


cheers,
Devaraj Takhellambam
  #6 (permalink)  
Old 04-28-2009
I-1 I-1 is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 26
thanks tyler_durden & cfajohnson,

Can you also put in the comments tor this what does what?

Thanks
  #7 (permalink)  
Old 04-28-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by I-1 View Post
Can you also put in the comments tor this what does what?

What part of it do you not understand?
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:25 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0