0

I am building meteor application and installing this application in client's system via bash script in windows.

In this script, i need to get current directory path stored in variable and then i need to write it in settings.json file.

I tried many solutions but later i found out that all those were for batch script.

mkdir upload
cd upload
DB_IMG_PATH=cd // I need to set path here in any variable and later use it below to write in settings.json file
echo %DB_IMG_PATH%
cd ..

echo '{"public":{"imgUploadUrl":"D:/mssqlempowervisi/upload","adminUser":"admin@mitch.org","adminPassword":"admin@123"}}' >> settings.json
Drew
  • 3,814
  • 2
  • 9
  • 28
ravi
  • 1,130
  • 16
  • 29
  • What exactly are you doing with PowerShell here? Because `$currDir = (get-location).Path` will get your current directory as a variable in PowerShell. – Drew Feb 05 '19 at 06:04
  • when i run script with $currDir = (get-location).Path in powershell, it gives me error that syntax error near unexpected token '(' – ravi Feb 05 '19 at 06:11
  • I created file called file.sh with content written in https://jsfiddle.net/9643bx0j/, and then i run that file in powershell using ./file.sh, so i get that error – ravi Feb 05 '19 at 06:14
  • Looks like you are trying to do this in `bash`, then `pwd` is what you are after. – Drew Feb 05 '19 at 06:21
  • when I write simple pwd in the script, It prints the path, but when I store it into any variable and then print it, it prints "pwd" (command itself) instead of the path. – ravi Feb 05 '19 at 06:42
  • Are you on bash or batch? because `mkdir` and `pwd` only work in bash and `echo %DB_IMG_PATH%` only works in batch... – Camusensei Feb 05 '19 at 07:33
  • Possible duplicate of [Getting current path in variable and using it](https://stackoverflow.com/q/1636363/608639), [Save current directory in variable using Bash?](https://stackoverflow.com/q/13275013/608639), [How to get a variable to have the current dir path?](https://stackoverflow.com/q/35189157/608639), etc. – jww Dec 22 '19 at 13:29

1 Answers1

2

Assuming you're really in bash:

mkdir upload
cd upload
DB_IMG_PATH=$PWD
echo "$DB_IMG_PATH"
cd ..

echo '{"public":{"imgUploadUrl":"'"$DB_IMAGE_PATH"'","adminUser":"admin@mitch.org","adminPassword":"admin@123"}}' >> settings.json

And the same thing without unnecessary cd steps:

mkdir upload
DB_IMG_PATH=$PWD/upload
echo "$DB_IMG_PATH"

Please be aware though:

  • By convention, we capitalize environment variables (PAGER, EDITOR, ..) and internal shell variables (SHELL, BASH_VERSION, ..). All other variable names should be lower case. Remember that variable names are case-sensitive; this convention avoids accidentally overriding environmental and internal variables.
  • Never change directories in a script unless you check if it failed! cd $foo is bad. cd "$foo" || exit is good.
Camusensei
  • 1,475
  • 12
  • 20
  • this works as this is returning me the path i want but it returns path like "/c/Users/abca/mitch" instead of "C:/Users/abca/mitch". Returned path by your solution isn't being accepted by system. – ravi Feb 05 '19 at 09:07
  • What are you using to run bash? I know cygwin has `cygpath` to transform the path into a windows one, maybe what you are using can also do that? If not, use `tmp=$DB_IMG_PATH drive=${tmp:1:1} tmp=${drive^}:${tmp//\//\\} windows_path=$tmp; echo "$DB_IMG_PATH $windows_path"` – Camusensei Feb 06 '19 at 14:36
  • I'm using git bash to run it. – ravi Feb 07 '19 at 05:17
  • I meant to write `in my previous comment` – Camusensei Feb 08 '19 at 13:20