Shell script deletes folder before due
Hello :),
I was writing a script for a certain need of mine, it goes like this: Code:
if [ $# -eq 2 ]; then So my questions are: a) Why does the script delete the directory before it did everything precedes this line? b) How can I prevent this from happening, AKA make the script delete the directory only AFTER I don't need it anymore? Thank you, O.R. |
A script will only execute commands in order. Make sure you're not backgrounding commands in the "do something" area.
|
Here's the full script (it just wasn't finished when I wrote the last post):
Code:
#!/bin/sh Really, if I erase the line "rm -dRf temp" it works fine! I know that bash should run the commands in order, but maybe I'm doing something wrong here, I don't know... Help :( O.R. |
My guess would be a permissions problem on creating that temp directory. When it was working, that temp directory might have been previously existing. When it didn't work, the temp directory was not there and failed to be created. When you were commenting/uncommenting that rm command you were affecting the existance of the temp directory, but NOT for the current execution, ... for the NEXT execution. Can be confusing! Something along those lines anyway.
Did you get any error messages on stderr when running this script? Also, for scripting I never like to assume that a command worked. From your code, you're assuming lots of stuff (mkdir worked, cd worked, etc.) Never assume, especially if you're running as root (I think you are since you're using the -d option for rm, and that is only available for root). I'd recommend using fully qualified paths and checking for command success, e.g., Code:
mkdir -p /full/path/to/temp |
All times are GMT -5. The time now is 03:00 AM. |