2

I wrote a bash script that works for my machine, but I would like to share it with a colleague. In doing so, I want him to be able to change only the paths in my variables that fit his system. I assume that I should add a part of my script that checks for existing directories and creates them if they do not exist?

Also is there a way for me to use "~/" to direct the paths to his home directory?

Basically trying to make the handoff of this script as easy as possible, where the local user would change only the paths in the variable.

Thank you!

attached is a snippet of my script - I would like the "me" to reflect the local user.

dbreportspath="/Users/me/Dropbox/NDT_RED_REPORTS/"$survivor"/"    #Path to Local dropbox Silverstack Reports - Make sure the directory exists!
dbpath="/Users/me/Dropbox/Drive_Contents/"                        #Path to Local dropbox Drive Contents - Make sure the directory exists!
localdc="/Users/me/Documents/Drive_Contents/Jobs/"$survivor/""    #Path to Local directory for Drive Contents

1 Answer 1

2

I would change your script to

dbreportspath="$HOME/Dropbox/NDT_RED_REPORTS/$survivor/"
dbpath="$HOME/Dropbox/Drive_Contents/"
localdc="$HOME/Documents/Drive_Contents/Jobs/$survivor/"

(Notice that I've removed the quotes around $survivor. I am uncertain of their need unless you can elaborate?)

To answer the other part of your question about creating directories if they do not exist:

if [ ! -d "$dbreportspath" ]; then
  mkdir -p "$dbreportspath"
fi

Or if you need to do it multiple times:

function createDirsIfDoNotExist() {
    for dir in "$@"; do
        if [ ! -d "$dir" ]; then
            mkdir -p "$dir"
        fi
    done
}

createDirsIfDoNotExist "$dbreportspath" "$dbpath" "$localdc"
Sign up to request clarification or add additional context in comments.

2 Comments

It's best to double-quote all (well, almost all) variable references, e.g. if [ ! -d "$dbreportspath" ], mkdir -p "$dbreportspath", for dir in "$@", etc.
$HOME was what I was looking for. Beginner Derp moment for me. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.