put a shell in for statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting put a shell in for statement
# 1  
Old 12-15-2008
Question put a shell in for statement

I'm a newbie here, today I've got a problem. Here's the shell:

b.sh
Code:
#!/bin/bash
rm -rf  $1

a.sh
Code:
#!/bin/bash
for file in '/root/Desktop/test/*'
do
echo $file
sh ./b.sh $file
done
ls /root/Desktop/test

When I sh a.sh, the result is :
/root/Desktop/test/a /root/Desktop/test/b /root/Desktop/test/c /root/Desktop/test/d
b c d

I got the result that was not my expect. I wanna the whole folders are removed. It seems that only folder "a" was deleted. I've no idea how to modify my shell, or where cause the problem. Could you help me to modify this shell and please tell me why.
Thanks!
# 2  
Old 12-15-2008
Remove the single quotation marks and have it look like:
Code:
#!/bin/bash

for file in /root/Desktop/test/*
do
     echo $file
     ./b.sh $file
done

ls /root/Desktop/test

exit 0

Also you don't have to call the second script in a new shell. I hope this is just a training script for trying for loops, handing over arguments to scripts etc. You can have this task much easier going if it's for something "real".
# 3  
Old 12-15-2008
Quote:
Originally Posted by very.very.sorry
b.sh
Code:
#!/bin/bash
rm -rf  $1


That will fail if there are any spaces in $1. Use:

Code:
rm -rf  "$1"

Quote:
a.sh
Code:
#!/bin/bash
for file in '/root/Desktop/test/*'


You will only go through the loop once, and $file will have the literal value /root/Desktop/test/*. It will not be expanded because it is quoted. Remove the quotes and it should behave as you expect.
Quote:
Code:
do
echo $file
sh ./b.sh $file


There is no need for sh:

Code:
./b.sh

Quote:
Code:
done
ls /root/Desktop/test

Quote:
Code:

When I sh a.sh, the result is :
/root/Desktop/test/a /root/Desktop/test/b /root/Desktop/test/c /root/Desktop/test/d
b c d

I got the result that was not my expect. I wanna the whole folders are removed. It seems that only folder "a" was deleted. I've no idea how to modify my shell, or where cause the problem. Could you help me to modify this shell and please tell me why.
Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script variable $1 used with put command

I have the following script used, i am new to shell scripting. tryign to understand. in the put $BASE_FOLDER/$base_name holds which path. What does it mean by $1 second path in put command is it constructing this path: /user/hive/warehouse/stage.db/$1 what is $1 holding in above path. ... (2 Replies)
Discussion started by: cplusplus1
2 Replies

2. Shell Programming and Scripting

How to put a trace on shell script running in AIX?

Please help me in putting a trace on shell script running in AIX Best regards, Vishal (3 Replies)
Discussion started by: Vishal_dba
3 Replies

3. Shell Programming and Scripting

How to put the multiple job on-hold using shell script?

Hi.. I need to put multiple jobs on ON HOLD in Autosys.. please help me on this. For Example: 1)ABCD_EFGH_IJKL 2)abcd_efgh_ijkl (2 Replies)
Discussion started by: Maanjesh
2 Replies

4. UNIX for Advanced & Expert Users

put data in excel file using shell script

Hi. I wish to add data in a specific excel file on daily basis.However the currect dat's data should always come on top i.e for example should always occupy cell A7,B7,C7 .. and the data of day before which was earlier on 7th row of each coloumn should move to 8th row..data on 8th row should... (1 Reply)
Discussion started by: kanus
1 Replies

5. Shell Programming and Scripting

how to put data using shell script to a excel file

Hi, Can any one tell me how to put data using shell script to a excel file from text file to other columns of excel file,leaving first column unaffected i.e it should not overwrite data in first column. Say my text file data is: 15-dec-2008 15-dec-2009 16-dec-2008 16-dec-2009 say my first... (1 Reply)
Discussion started by: siri_886
1 Replies

6. Shell Programming and Scripting

New to Shell scripts and where do you put them

Hey all, I'm trying to setup an external XML feed for my site and I've received a script to make it all possible. This script places the feed in the MySQL datbasa and can remove the previous feed. Because I am new to this I would like to ask a simple question. Could somebody tell me where do I... (3 Replies)
Discussion started by: chrisdegrote
3 Replies

7. Shell Programming and Scripting

read a file in shell and put result in a line

Hi All, I have a to read a file and put the result in one line. The i am reading from contain the data like below. 1 signifies the beging of the new line. (6 Replies)
Discussion started by: lottiem
6 Replies

8. Shell Programming and Scripting

Need to put a breakpoint in gdb using bash shell

I want a way to put a break point in gdb by runing a shell script. Actualy I wanted to do certain automisation of a long manual process, which includes starting of a process in background and then taking the process ID of that process and then attach the gdb to that process ID. Then finaly... (4 Replies)
Discussion started by: kapilkumawat
4 Replies

9. UNIX for Dummies Questions & Answers

How can I put wildcards in an if statement that uses variables?

With the if statement: if How can I make it so it accepts a wildcard after the ${CURR_DAY_MONTH} variable? Putting a -f /webtrends/SUN/mrw2/access.${CURR_DAY_DAY}${CURR_DAY_MONTH}* won't work, right? I think I need some kind of special character so it knows the wildcard is... (3 Replies)
Discussion started by: LordJezo
3 Replies

10. UNIX for Dummies Questions & Answers

where to put shell scripts?

This could be a really dumb question, but for a newbie trying to learn, some help would be appreciated. When you write a shell script, what extension should it have, and more importantly, where do you put it???? (7 Replies)
Discussion started by: ober5861
7 Replies
Login or Register to Ask a Question