Visit Our UNIX and Linux User Community


looping some statements


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting looping some statements
# 1  
Old 12-23-2010
looping some statements

Hi,
assume there are some dir structure like -

Quote:
/xyz/abc/123/qwe
/xyz/dfe/123/qwe
/xyz/ghi/123/qwe
I need to write a script to create 5 new directories under 'qwe' dir of all the above 3 dir structures. these 5 dir will have same name.
I don't want to use 15 mkdir statements. i just want to write 5 mkdir statemets and use them 3 times. I appreciate any help.

I am on UX-HP machine.

Last edited by Sriranga; 12-23-2010 at 06:26 AM..
# 2  
Old 12-23-2010
use -p option of mkdir.

Code:
mkdir -p /xyz/abc/123/qwe/dir1/dir2/dir3/dir4/dir5
mkdir -p /xyz/def/123/qwe/dir1/dir2/dir3/dir4/dir5
mkdir -p /xyz/ghi/123/qwe/dir1/dir2/dir3/dir4/dir5

R0H0N
# 3  
Old 12-23-2010
Thanks Rohan. I am using mkdir -p option. my req is not to create dir1 to dir5 one below the other but to create all 5 under 'qwe'
/xyz/abc/123/qwe/dir1
/xyz/abc/123/qwe/dir2
.
.
/xyz/def/123/qwe/dir4
.
.
.
./xyz/ghi/123/qwe/dir5
# 4  
Old 12-23-2010
Try:

Code:
for dir in /xyz/abc/123/qwe /xyz/dfe/123/qwe /xyz/ghi/123/qwe
do
 mkdir -p $dir/dirA $dir/dirB $dir/dirC $dir/dirD $dir/dirE
done

This User Gave Thanks to clx For This Post:
# 5  
Old 12-23-2010
try this
Code:
 
mkdir -pv /xyz/{ghi,dfe,abc}/qwe/dir{1,2,3,4,5}

# 6  
Old 12-23-2010
thanks Anchal...it works !
Still 1 more doubt Smilie is there any way we can assign the path
'/xyz/abc/123/qwe' to a variable and then use it in for loop's condition? like

Code:
path1 = /xyz/abc/123/qwe
path2 = /xyz/dfe/123/qwe
path3 = /xyz/ghi/123/qwe

for dir in path1 path2 path3
do
 mkdir -p $dir/dirA $dir/dirB $dir/dirC $dir/dirD $dir/dirE
done


Last edited by Scott; 12-23-2010 at 08:26 AM.. Reason: Quote tags to Code tags
# 7  
Old 12-23-2010
Code:
path1="/xyz/abc/123/qwe"
path2="/xyz/dfe/123/qwe"
path3="/xyz/ghi/123/qwe"

for dir in $path1 $path2 $path3
do
mkdir -p $dir/dirA $dir/dirB $dir/dirC $dir/dirD $dir/dirE
done


Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nested looping statements

I cannot get the code below to work correctly. The IF statement works just fine, but not the looping. The inner loop tries to find files for a given vendor; if found, I need to sleep giving another process time to move the files. Once the given vendor's files are gone, then I want to move on to the... (1 Reply)
Discussion started by: dgreene
1 Replies

2. UNIX for Dummies Questions & Answers

if and then statements

I came across a bash script that outputs the forecast for the day and the max temperature but at the end of the day the max temperature disappears ($6) and I am left with "C" after the forecast. Here is the script: #! /bin/bash curl -s --connect-timeout 30... (7 Replies)
Discussion started by: _light_
7 Replies

3. Homework & Coursework Questions

Using While and If statements

1. The problem statement, all variables and given/known data: Two problems I need solving please. I created a script where the user types in 7 numbers as standard input and each one is then stored in an array. Now I need to perform the following calculations on those numbers: 1) Use a while... (11 Replies)
Discussion started by: jjb1989
11 Replies

4. Shell Programming and Scripting

Using While and If statements

Hi guys, Two problems I need solving please. I created a script where the user types in 7 numbers as standard input and each one is then stored in an array. Now I need to perform the following calculations on those numbers: 1) Use a while loop to determine the largest number in the range. ... (2 Replies)
Discussion started by: jjb1989
2 Replies

5. Shell Programming and Scripting

vi and if statements

Hi I am very new to Unix programming and shell scripting. I am trying t figure out how to write a little script that will output the number of directories. I can find the number of directories using ls -l | grep "^d" | wc -l I can not figure out how to do it so when I type the name... (8 Replies)
Discussion started by: Reddoug
8 Replies

6. Shell Programming and Scripting

Help with IF statements

I am writing a script that does a search for a argument in a file and lists all like occurrences. The script verifies that it is a file and then runs another script that list the lines. My problem is that I need the script to accept a file or a directory and then go to that directory check all... (1 Reply)
Discussion started by: zero3ree
1 Replies

7. Shell Programming and Scripting

Looping through a shell script with sql statements

Hello members, I'm working on the Solaris environment and the DB i'm using is Oracle 10g. Skeleton of what I'm attempting; Write a ksh script to perform the following. I have no idea how to include my sql query within a shell script and loop through the statements. Have therefore given a... (4 Replies)
Discussion started by: novice82
4 Replies

8. Shell Programming and Scripting

Please help on IF statements.

I had different problem scenarios with IF statement. Can any expert please enlighten me on the difference with these scenarios. Thank you. 1st Scenario: testdate=`date +%Y%m` test=`cat /var/log/database0.$testdate*.log | grep "Errors found during processing" | tail -10` if then ... (4 Replies)
Discussion started by: filthymonk
4 Replies

9. UNIX for Dummies Questions & Answers

Else in If Statements

Sorry to be a pain, but how does the else work in the if statements? Ive been making scripts with if statements but i cant get the else statements working. Can you help? (8 Replies)
Discussion started by: chapmana
8 Replies

10. Shell Programming and Scripting

or statements?

how do i do an or in an if-then statement? i tried: if ; then bleh fi how???? (1 Reply)
Discussion started by: Blip
1 Replies

Featured Tech Videos