0

I need to create a folder in the current directoy of the shell script im executing. This piece of code work very well when I execute the script myself but when it get executed by crontab, it always create the folder in the top layer of the user profile.

#Where $log_folder is a certain path
if [ ! -d "$log_folder" ]; then            
      mkdir -m a=rwx $log_folder
fi

So if I execute the script from the following path: /export/home/nz/tmp/test/GenerateScript.sh , the folder will be created in /export/home/nz/ (Where nz is my user) instead of being in /export/home/nz/tmp/test/.

Is there another option I need to add to mkdir or is it something I need to add to crontab? I already linked the user profile in crontab with the following:

$HOME/.bash_profile

Thank you.

EDIT:

Ive been able to do what I wanted by adding those 2 line just before creating the folder

 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
 cd $DIR

If there is a better way, feel free to show me!

Jmercier13
  • 13
  • 7
  • You may want to take a look at this question: [What is the 'working directory' when cron executes a job?](http://unix.stackexchange.com/q/38951/165677) – Mr. Llama Feb 20 '17 at 19:05
  • I though about doing that, but the problem is that I dont know in which directory the script will reside, and for all i know it could be in another one next week... So i'm unsure about using cd. – Jmercier13 Feb 20 '17 at 20:10
  • Ahhh, found a way with this http://stackoverflow.com/questions/59895/getting-the-source-directory-of-a-bash-script-from-within . Thanks for your help Mr. Llama. – Jmercier13 Feb 20 '17 at 20:49

0 Answers0