1

This question already has an answer here:

I have a script like,

#/bin/bash -x
LASTBUILD=' 174254491  2018-08-08T11:04:40Z  gs://abc/kishor/5.4.0.61/xyz-5.4.0-61.tgz
 TOTAL: 46 objects, 7325896651 bytes (6.82 GiB)'

echo "this is the LASTBUILD ============== $LASTBUILD"

LATESTBUILD=echo $LASTBUILD | cut -d ' ' -f 3

echo "this is the LATESTBUILD ############### $LATESTBUILD"

After execution shows result as below,

[root@root ~]# echo $LATESTBUILD

[root@root ~]#

My expected result is as below,

[root@root ~]# echo $LATESTBUILD
gs://abc/kishor/5.4.0.61/xyz-5.4.0-61.tgz
[root@root ~]#

The above code is not working.

PS: Also tried using awk but not worked,

LATESTBUILD=$LASTBUILD | awk -F '/' '{print $5}'

marked as duplicate by Jesse_b, msp9011, Archemar, Romeo Ninov, Shadur Aug 10 '18 at 6:54

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • What output do you get from your script? I can already see that you're failing to use export so any variable set inside the script are not going to be recognized by the shell after the script exits... – Shadur Aug 10 '18 at 6:50
1

There are a few issues with your script:

  1. Your hash bang is invalid. You have #/bin/bash -x and it should be #!/bin/bash -x
  2. Your LASTBUILD variable is weird. Each column is separated by double spaces which will cause cut to act oddly, and you also don't want a newline in it with cut.
  3. LATESTBUILD=echo $LASTBUILD | cut -d ' ' -f 3 is nothing. This should be in command substitution, but even then it wont work because of the double spacing. It should be: LATESTBUILD=$(cut -d ' ' -f5 <<<"$LASTBUILD")
  4. The variables set inside your script will not be available outside of it, so doing echo $LATESTBIULD on the command line after the script executes will not and should not work.

Here is a working version of your script:

#!/bin/bash

LASTBUILD='174254491   2018-08-08T11:04:40Z  gs://abc/kishor/5.4.0.61/xyz-5.4.0-61.tgz TOTAL: 46 objects, 7325896651 bytes (6.82 GiB)'

echo "this is the LASTBUILD ============== $LASTBUILD"

LATESTBUILD=$(cut -d ' ' -f5 <<<"$LASTBUILD")

echo "this is the LATESTBUILD ############### $LATESTBUILD"
2
awk '{ print $3 }' <<< $LASTBUILD

The default field separator in awk in a space and so print the 3rd space delimited field.

1

You could do it (carefully) with the shell:

LASTBUILD=' 174254491  2018-08-08T11:04:40Z  gs://abc/kishor/5.4.0.61/xyz-5.4.0-61.tgz TOTAL: 46 objects, 7325896651 bytes (6.82 GiB)'
set -f               # disable filename generation (globbing)
set -- $LASTBUILD    # specifically un-quoted, to allow splitting
LATESTBUILD=$3
0

How about

read _ _ LATESTBUILD _ <<< $LASTBUILD
  • you'd also want to disable filename generation with set -f, just in case there were any wildcards in the contents of the variable. – Jeff Schaller Aug 9 '18 at 14:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.