CodeMed
CodeMed

Reputation: 9191

No such file or directory when populating variable in Bash

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.

Upvotes: 5

Views: 8344

Answers (2)

chepner
chepner

Reputation: 531165

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 /.

Upvotes: 0

Cyrus
Cyrus

Reputation: 88636

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.

Upvotes: 6

Related Questions