0

I'm working on automated scripts running on a Linux box (Debian). Those scripts are started from a scheduler and are manipulating files within their own directories.

At the end of the scripts, I need to delete temporary files.

Currently, I've something like this :

#check that some variables are not empty
if [[ -z $var1 || -z $var2 || -z $var3 ]]; then
  echo "Some var is empty"
  exit 1
fi
workingdir=$var1/$var2
#some checks on working directory
if [[ ! -d $workingdir ]]; then
  echo "Directory error"
  exit 1
fi
cd $workingdir
if [[ $(pwd) != $workingdir ]]; then
  echo "Change directory error"
  exit 1
fi
#...here goes the main part of the script
#and at the end, the "cleaning"
if [[ -d $workingdir/temp/$var3 ]]; then
  find $workingdir/temp/$var3 -delete;
fi

Do you have any idea I could use to reduce the risk of removing anything wrong?

Thank you.

Regards

  • 1
    First thing: "double-quote $variables". – Kamil Maciorowski Oct 15 '18 at 8:18
  • Thank you for this link. I've been searching for a while now and I'm not sure how to double-quote within a for / loop with wildcard? Should I use double-quotes around the variable only and leave the wildcard out like this ? for f in "$workingdir"*; do... – lennelei Oct 15 '18 at 14:31
  • I don't see such syntax in the question. You shouldn't ask new questions in comments. A quoted * won't be expanded. Why won't you try echo "$workingdir"*, echo "$workingdir*", echo "$workingdir"/* and echo "$workingdir/*" and see for yourself? – Kamil Maciorowski Oct 15 '18 at 14:45
  • I'm sorry for the question, I'll open a new post. – lennelei Oct 16 '18 at 6:59
1

Your script knows what files are created and where they are.

This general statement is an important point here. If you really want to delete only the files that you created, keep track of which files you create. So you would typically do:

TMP1=$(mktemp -t example.1.XXXXXX)
TMP2=$(mktemp -t example.2.XXXXXX)

to create your temp-files (see man mktemp for details), and do

rm -f "$TMP1" "$TMP2"

when you're finished with the temp-files. Alternatively, you can create a temp directory and delete that one.

Something you should not do is

rm -f /tmp/example.?.*

because it will remove the temp-files for other running instances of the script as well.

  • Actually, it's a bit more complicated: the script starts a java job which uses $workingdir and $var3 variables to store temporary data in the $workingdir/temp/$var3 folder. I don't want to remove that folder within the java job because I might need it (in case of failure for example). That's why I was looking at ideas to secure the script. – lennelei Oct 17 '18 at 11:18
  • The primary point is, that you must keep track of the temp-files that your script (or Java code) creates. You can only decide which files to delete if you know what they are. – Ljm Dullaart Oct 17 '18 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.