0

I need to store the result filename of running gunzip file.gz into a variable. The only answer I've found is for when the name of the file is known ahead of time. However, in my case, the .gz file is going to be dynamically generated, so the accepted answer doesn't work.

I tried this:

cd /var/www/backup/db
DB_TAR=$( /usr/bin/ls site_name* -Rt | /usr/bin/head -1 )
DB=$( gunzip ${DB_TAR} )
echo "${DB}"

but got nothing in the output.

crash springfield
  • 1,072
  • 7
  • 17
  • 33
  • Do you know the gzip file's name once it has been generated? If so the linked question's accepted answer seems to fit your needs (the `$file` part is dynamic, you can just remove the `.fastq` that isn't relevant to you and it should work) – Aaron Jul 26 '18 at 16:24
  • 1
    please edit your Q to show an assignment, i.e. `DB_TAR=....` . Good luck. – shellter Jul 26 '18 at 16:25
  • `gunzip "$file"` doesn't emit anything to stdout, so your variable can't be expected to capture anything. You need to use `gunzip -c "$file"`. And note that [parsing `ls` is extremely error-prone](http://mywiki.wooledge.org/ParsingLs). – Charles Duffy Jul 26 '18 at 16:30
  • @Aaron it'll differ every time – crash springfield Jul 26 '18 at 16:32
  • @CharlesDuffy the -c flag prints out the entire database dump – crash springfield Jul 26 '18 at 16:32
  • @crashspringfield, yes, and that's what you said you wanted, right? The database dump stored in your variable? – Charles Duffy Jul 26 '18 at 16:34
  • No, I need the file name – crash springfield Jul 26 '18 at 16:34
  • Does your question say that anywhere? – Charles Duffy Jul 26 '18 at 16:34
  • @CharlesDuffy sorry. editted it to make it more clear – crash springfield Jul 26 '18 at 16:35
  • BTW, the use of lower-case variable names in my answer is deliberate; see http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html: *The name space of environment variable names containing lowercase letters is reserved for applications. Applications can define any environment variables with names from this name space without modifying the behavior of the standard utilities* -- keeping in mind that setting a shell variable overwrites any like-named preexisting environment variable, so the namespace guidelines necessarily apply to both categories. – Charles Duffy Jul 26 '18 at 16:40

1 Answers1

1
cd /var/www/backup/db || exit
{ IFS= read -r -d ' ' timestamp && IFS= read -r -d '' latest; } < <(
  find site_name* -type f -name '*.gz' -printf '%T@ %p\0' | sort -znr
)
gunzip -- "$latest" || exit
uncompressed_latest="${latest%.gz}"
echo "$uncompressed_latest"

  • Parsing ls is innately error-prone; BashFAQ #3 describes more reliable ways to find the newest/oldest/etc file, one of which (albeit requiring GNU find and sort) is used here.
  • Stripping a known suffix is best done with parameter expansion. ${var%suffix} returns the contents of var, with the shortest possible match for the pattern suffix removed.
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441