• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Best approach on this sh file

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Enablingwolf

Senior Member overclocking at t
Joined
Jun 14, 2004
I've made a dinky sh to: cd && unrar && chmod . Works great. :thup:

I am missing one thing. rm'Ing the directories I've extracted from. After the sh does what I want done.

I would prefer it to not rm on error, but do so on completion. Any input on how I could do this and not rm the directories on error after unrar runs?


shebang
cd /go/to/dir && unrar e -r *.rar && su chmod 777 -R /go/to/dir
 
You can get the last error code by using the variable $?. Note that it changes if any commands are run, so it is a good idea to store it in a variable. You could use it like this:

Code:
if [ $? != "0" ]; then
   echo "There was an error doing xxxxxxxx."
else
   rm $THEWORLD
   echo "Cleaning up"
fi
 
You can get the last error code by using the variable $?. Note that it changes if any commands are run, so it is a good idea to store it in a variable. You could use it like this:

Code:
if [ $? != "0" ]; then
   echo "There was an error doing xxxxxxxx."
else
   rm $THEWORLD
   echo "Cleaning up"
fi

This is good advice, the only thing I would do would be to add "-rf" to the rm command as long as you are sure you have selected the correct content to remove
 
The folders I want to remove, vary what is in the parent directory.. It's a downloads directory via NFS on my home server. Files come in from lsftp, I manage the files. Then let users go at them. Once I get this script going to my needs. I will fully automate it.
 
You also need to quote the arguments to the 'rm' command to make sure things don't get misinterpreted due to spaces or other things that could result in confusion of the arguments. Think about removing something that has a directory structure that looks like ".../somewhere /etc/..." [notice the space following 'somewhere']. Since I'm weak with quoting within scripts I'd need to explore and test this thoroughly if there is any possibility that this situation could arise. It is also important if the script runs as root (which it should not!)

I'm not sure I understand the full environment this is running in but since you mention users, you also need to consider security. Apologies if you've already got this covered.
 
cd /go/to/dir && unrar e -r *.rar && su chmod 777 -R /go/to/dir

Another option: If you add "&& rm ...." to the end, it will do what you wish.

Chaining commands with:
Code:
&& -- will only run if the previous command is successful (0 exit status)
|| -- will only run if the previous command is unsuccessful (non-0 exit status)
;  -- will always run the command, semicolon is the same as a line return on the command line.

Also, some related humour: http://uncyclopedia.wikia.com/wiki/Rm (Note for newbies: This is a joke)
 
I used the double ampersand, for that specific reason. It simplfies success.
 
Back