You can not set a bash variable directly from awk. But you can have bash execute awk and assign
its output to a variable with $(command).
Like this:
Code:
#!/bin/bash
SOURCE_PATH=file.txt
VERSION=$(awk 'BEGIN {
file="'$SOURCE_PATH'"
while ( (getline aline < file) > 0 ) {
if (aline ~ /Id=[0-9]*.[0-9]*/) {
print aline
break
}
}
}')
echo "Version line: $VERSION"
But in your case (just print 1 matching line) I'd use grep instead of awk:
Code:
#!/bin/bash
SOURCE_PATH=file.txt
VERSION=$(grep 'Id=[0-9]*.[0-9]*' "$SOURCE_PATH")
echo "Version line: $VERSION"
Or at least simplify the awk script:
Code:
#!/bin/bash
SOURCE_PATH=file.txt
VERSION=$(awk '/Id=[0-9]*.[0-9]*/ {print $0}' "$SOURCE_PATH")
echo "Version line : $VERSION"