2

I have directory

MainProject/src/

My script which I am calling test.sh run at /MainProject/ and here is some part of the sript:

dotnet restore src/*.sln
dotnet msbuild -t:publish src/*.sln -p:Configuration=Release

For this command, I want MainProject.Test as variable VAR:

dotnet vstest VAR/bin/Release/netcoreapp1.1/VAR.dll

or something like this:

dotnet vstest {src/*.Test}/bin/Release/netcoreapp1.1/{src/*Test}.dll

Which contains these files and folders:

Files:

project.sln

somescript.sh

Folders:

MainProject.Test

MainProject.Host

What I need to do is fetch MainProject.Test folder name and set it to a variable, but I also need it to be templatized where I can set it to a variable using something like *.Test

The reason for this is that I need the script parametrized because there are

MainProject2

MainProject3

MainProjectx

using the same naming convention.

Ali Shahbaz
  • 81
  • 1
  • 2
  • 5
  • Is the path to `MainProject.Test` where your script is run? Where the source to the script itself lives? An argument? Something else? – Charles Duffy Aug 03 '17 at 20:32
  • Perhaps this is a duplicate of https://stackoverflow.com/questions/59895/getting-the-source-directory-of-a-bash-script-from-within -- right now, the question is too vague to be certain. – Charles Duffy Aug 03 '17 at 20:33
  • (when you say "fetch `MainProject.Test`" -- do you mean fetch it *from the command line*? Or fetch it from where?) – Charles Duffy Aug 03 '17 at 20:38
  • I updated the question. I just want `MainProject.Test` foldername as a string to pass as a variable – Ali Shahbaz Aug 03 '17 at 20:46
  • 1
    Yes, but where does it (do you want it to) come from in the first place? Does it come from a command-line argument to your script? Does it come from looking up what the current directory was when your script was started? – Charles Duffy Aug 03 '17 at 21:00
  • I want it to come from the same script. As in when the shell script runs the script knows the current directory which is /MainProject/ – Ali Shahbaz Aug 03 '17 at 21:09
  • Sorry if I am being confusing, I am very new to shell scripting. – Ali Shahbaz Aug 03 '17 at 21:10

1 Answers1

3

The current directory is in $PWD. That's fully-qualified; to remove everything from the beginning up to the last / would be ${PWD##*/}, using a parameter expansion.

Thus, to extract the last piece of the current working directory and add .Test as a suffix:

result=${PWD##*/}.Test
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • How would you extract the second to last directory, i.e. move up one dir at a time, only capturing that individual directory? – Juno Sprite Mar 14 '23 at 18:21
  • UPDATE: I figured out what I want, but it's not really scalable: `PWD="/system/dir/path/project/client"` `APP_NAME=${PWD%/*}` `APP_NAME=${APP_NAME##*/}` `echo ${APP_NAME}` --> `project` – Juno Sprite Mar 14 '23 at 19:01
  • @JunoSprite, what do you mean "not really scaleable"? Yes, it's three lines, but they're all shell builtins, so far faster than running a single external command. The only part of the code in your comment above that has anything wrong with it is the [`echo`](https://stackoverflow.com/questions/29378566); and I suppose I could also quibble about the all-caps `APP_NAME` variable name being out of compliance with [relevant POSIX recommendations](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html), which reserve lower-case variable names for application use. – Charles Duffy Mar 14 '23 at 19:40
  • Not scalable, meaning it doesn't work for getting an arbitrary dir's name from an arbitrary path length. Say I wanted to get the third-to-last dir name into a variable? or forth-to-last? I'm not good enough with RegEx to solve that quite yet. Ideally, I'd have a RegEx with '1' or '2' or '3', etc, written somewhere in the expression, which would allow me to grab the first, second, third, etc, dir name from either the beginning or the end of a given path, and store that name into a variable. I'd be able to easily swap out the number corresponding to the depth of the dir name I seek – Juno Sprite Mar 22 '23 at 19:41
  • `IFS=/ read -r -a pieces <<<"$APP_NAME"` then, and you can pick out the piece you want from the array. – Charles Duffy Mar 23 '23 at 10:35