I'm a newbie here, today I've got a problem. Here's the shell:
b.sh
#!/bin/bash
rm -rf $1
a.sh
#!/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!
Remove the single quotation marks and have it look like:
#!/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".
That will fail if there are any spaces in $1. Use:
rm -rf "$1"
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.