4

When I run the following command in a bash script:

urlOfServer="$($repoURL_StripLastFour | awk -F'/' '{print $1}')"  

I get the following error:

192.168.1.12:7999/pcfpt/scriptsforexamples: No such file or directory

You can see that the value of the repoURL_StripLastFour variable is 192.168.1.12:7999/pcfpt/scriptsforexamples at the time when the script is run. This value is auto-created at runtime by other elements of the script, so I cannot simply pass it as a literal.

What specific syntax is required to resolve this error, so that the urlOfServer variable can be successfully populated?

I have tried many variations of moving quotes and parentheses already.

jww
  • 97,681
  • 90
  • 411
  • 885
CodeMed
  • 9,527
  • 70
  • 212
  • 364
  • With bash's Parameter Expansion: `urlOfServer="${repoURL_StripLastFour%%/*}"` – Cyrus Apr 04 '18 at 20:08
  • [How to remove last part of a path in bash?](https://unix.stackexchange.com/q/28771/56041), [How to delete part of a path in an interactive shell?](https://unix.stackexchange.com/q/21788/56041), [Remove part of path on Unix](https://stackoverflow.com/q/10986794/608639), [How to remove end folder name from a path in Linux script?](https://stackoverflow.com/q/29329093/608639), etc. – jww Apr 04 '18 at 21:05
  • @jww You left evidence that you drove through here and downvoted both excellent answers and the OP in one rushed slashing event. I do not care if you downvote my question, but please consider not treating the answerers so harshly. – CodeMed Apr 04 '18 at 21:20

2 Answers2

6

Replace

$repoURL_StripLastFour

with

echo "$repoURL_StripLastFour"

to feed awk from stdin or replace

$repoURL_StripLastFour | awk -F'/' '{print $1}'

with

awk -F'/' '{print $1}' <<< "$repoURL_StripLastFour"

to use a here string.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
0

Don't use awk; just use parameter expansion:

urlOfServer=${repoURL_StripLastFour%%/*}

%%/* strips the longest suffix that matches /*, namely the first / and everything after it, leaving only the text preceding the first /.

chepner
  • 497,756
  • 71
  • 530
  • 681