Trying to make a bash script (on mac) that creates a folder then runs a bunch of scripts in that folder. I want to echo the absolute path to folder and prompt user to confirm before taking action.
The script takes a relative path as an argument, how do I convert to an absolute path before creating the folder?
!/bin/bash
if [[ $# -eq 0 ]] ; then
echo 'Whoa! You dont want to do that! you need to tell me where to put it'
exit 0
fi
ORG_DIR=$(pwd)
#here I want to get path before making directory, I've only figured out how to do after.
mkdir ../$1
cd ../$1
NEW_DIR=$(pwd)
echo "Running scripts in"
echo $NEW_DIR
read -p "Are you sure? (y/n) " -n 1 -r
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
# run a bunch of scripts
fi
How do I get the absolute path of NEW_DIR before making it?
Note: Platform is OSX, I don't need to support other 'nix variants.
EDIT: Sat, 29
I realized that in an effort to trim the question down to essential details only, I trimmed to much, my apologies.
This script exists in (for example sake)
/Documents/git/ProjectGenerator
I intend the script to generate a project in (or whatever path is entered)
/Documents/git/NewProject
It runs a combination of npm scripts, and copying files from
/Documents/git/ProjectGenerator
The rest of the script works fine now, however I recently accidentally hit enter on the wrong dir. Since this script is intended for my use only (and not sudo), I'm trying to add a sanity check, not a security check to the beginning of the script. Must type Y and enter after looking at the dir.
I'm trying to get an absolute path and what I'm getting is
/Documents/git/ProjectGenerator/../NewProject
I'm trying to get it to echo
/Documents/git/NewProject
prior to creating the directory. (sometimes the path is far more complex)
In node we have path.resolve(path)
In php or c its realpath(path)
I cant seem to find an equivalent command in osx bash, is there one? or do I need to parse the string?
$1
is../../../../../System
orfoo bar
? – doneal24 Sep 27 '18 at 2:21