0

I am trying to set a variable to a value returned by a program (lynx). I cannot get past which parenthesis or bracket I use to accomplish my goal.

I have this:

DEBFILE=${(lynx -listonly -dump https://gridtracker.org/release.html) | grep .deb | grep nwjs | awk '{ print $2 }'}

Where am I going off the rails. Be easy on me. I haven't messed with this in a while.

I simplified the statement to include the URL. Ultimately I would like to use a variable for the URL in that code block as well.

such as...

DEBFILE=${(lynx -listonly -dump ${GT_DOWNLOAD_LINK} ) | grep .deb | grep nwjs | awk '{ print $2 }'}

as it is defined already near the beginning of my script.

I know it is a syntax problem because I broke it down into a few more lines, echo'd the command to a file, and read it back into a variable that way.

THIS works

echo $(lynx -listonly -dump https://gridtracker.org/release.html | grep .deb | grep nwjs) | awk '{ print $2 }' >> temp.txt
        DEBFILE=$(cat temp.txt)
        echo $DEBFILE
1
  • You probably want something like debfile=$(lynx -listonly -dump https://gridtracker.org/release.html | grep -Pom1 '\S*nwjs\S*\.deb$') || die "Can't determine nwjs URL" Nov 3, 2022 at 8:51

1 Answer 1

3

You indeed have nested syntax errors, probabily arising from a confusion about variable expansion and command substitution.

What you want to achieve is command substitution, i.e. you want to assign to the variable DEBFILE the output of a command. This is done as follows:

DEBFILE=$( your command )

The syntax ${ ... } you used in your original attempt is instead parameter/variable expansion, where you "dereference" the shell variable whose name is placed in the curly braces. Note that ${VARIABLE} is basically the same as $VARIABLE, so for the first command you showed, your shell would try to look up a variable whose name is (lynx -listonly -dump https://gridtracker.org/release.html) | grep .deb | grep nwjs | awk '{ print $2 }' and assign its value to DEBFILE.(1)

The correct way to achieve your goal should simply be

DEBFILE=$(lynx -listonly -dump https://gridtracker.org/release.html | grep .deb | grep nwjs | awk '{ print $2 }')

and you could probably replace the grep-grep-awk pipeline by a single awk command, which would probably look like

DEBFILE=$(lynx -listonly -dump https://gridtracker.org/release.html | awk '/\.grep/ && /nwjs/ { print $2 }')

or

DEBFILE=$(lynx -listonly -dump https://gridtracker.org/release.html | awk '(index($0,".deb") && index($0,"nwjs")) { print $2 }')

Note that the code you labelled as "This works" basically does exactly what I proposed, just with an unnecessary echo and the intermediate step of writing the output to a file from which it is read back to the variable $DEBFILE:

  • echo $(lynx ... | grep .deb | grep nwjs) has the same effect as lynx ... | grep .deb | grep nwjs (but could possibly introduce unwanted reformatting of whitespace) because it basically makes the console output of the lynx ... | grep ... | grep ... pipeline an argument to echo, which will simply print it to the console (again).
  • So, echo $(lynx ... | grep .deb | grep nwjs) | awk '{ print $2 }' does the same as lynx ... | grep .deb | grep nwjs | awk '{ print $2 }'

(1) As noted by Stéphane Chazelas, ksh93 actually does support command substitution in a ${ cmd; } form, but it requires the command to end in a ;, it is not POSIX-compatible, and Bash doesn't support it.

4
  • Thank you for your response. I will test it out and see where I went wrong. I could have sworn I tried it like you posted. I cat'd it out to a file and got it to work, which is strange.
    – Joe Burden
    Nov 1, 2022 at 22:57
  • @JoeBurden I would be interested to know how your test turned out. Also, if you found the answer useful, you may consider accepting it so that others facing a similar issue may find it more easily.
    – AdminBee
    Nov 4, 2022 at 13:15
  • Thanks for all the help. I have been temporarily torn away from this project unexpectedly. Ultimately, this is what I went with for now. ``` echo $(lynx -listonly -dump $GT_DOWNLOAD_LINK | grep .deb | grep nwjs) | awk '{ print $2 }' > NWJS_LINK.txt ```
    – Joe Burden
    Nov 5, 2022 at 19:21
  • When I revisit the issue I will make sure everyone gets appropriate credit and acceptance. I did have a problem with ending a loop that exacerbated my problem.
    – Joe Burden
    Nov 5, 2022 at 19:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .