Jamie Metzger
Jamie Metzger

Reputation: 48

Reference Local User's path in Bash

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

Upvotes: 2

Views: 664

Answers (1)

Ashutosh Jindal
Ashutosh Jindal

Reputation: 18869

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"

Upvotes: 2

Related Questions