0

I'm not familiar with bash scripting. In my app, I have three folders. Frontend, backend, and script are the three folders. My bash script is contained within the script. This is the structure of my app Image. I'd like to retrieve absolute path variables (frontend and backend) from my script. I'm not sure what to do. I was successful in obtaining the script path folder. My objective is to obtain an absolute route from a script.

#!/bin/sh

root_path="$( cd  "$(dirname "$0")" >/dev/null 2>&1 || exit ; pwd -P )"
#frontend_path=
#backend_path=

echo "Getting absolute path ${root_path}"
kvantour
  • 25,269
  • 4
  • 47
  • 72
Krisna
  • 2,854
  • 2
  • 24
  • 66
  • Hi, your shebang read `/bin/sh` so do you mean bash or posix sh? – kvantour Jan 26 '22 at 09:15
  • Sorry, may be it's called shell script. – Krisna Jan 26 '22 at 09:17
  • But did you understand my question @kvantour – Krisna Jan 26 '22 at 09:18
  • 1
    @Krisna: Please always be precise what kind of shell you are refering to, i.e. whether you are going to use bash, zsh, ksh, POSIX shell or whatever. Your script seems to be intended as POSIX shell script, but we can't know for sure unless you show us **how** you invoke the script. If you don't make this clear, it could be that you will receive an otherwise valid answer, which for your script language does not make. If you want to be on the safe side, also specify the version of your shell, and the platform. – user1934428 Jan 26 '22 at 13:19
  • Thank you. I never used script. Could you please help me one thing. – Krisna Jan 26 '22 at 15:29
  • Have see you anything wrong in this script: https://codeshare.io/YLBKkR. In my cd/ci pipeline: I am getting this error: ./script/generate-graphql.sh: line 9: syntax error: unexpected "(" – Krisna Jan 26 '22 at 15:32

1 Answers1

1

Adding /.. after the output of dirname should do the trick to get you the root path:

root_path="$( cd  "$(dirname "$0")/.." >/dev/null 2>&1 || exit ; pwd -P )"

From that on, you can just add /frontend / /backend to get the other paths:

frontend_path="$root_path/frontend"
backend_path="$root_path/backend"
Elias Dorneles
  • 22,556
  • 11
  • 85
  • 107