In my mysql restore script I use a mysqldump file previously created for input. This filename is provided on the command line as an argument and may or may not be gzipped. I want to use the file as input to the mysql statement but not change/gunzip the file, just read the contents.
I use the following code for input to the mysql command (which I like):
mysql -u $user -p"$pass" $dbname <( [[ "${mysqldumpfile: -3}" = ".gz" ]] && gzip -d -c "${mysqldumpfile}" || cat "${mysqldumpfile}" )
So mysql dump file name ends in ".gz" then I gunzip to stdout, otherwise I cat to stdout. stdout from either is used as stdin to the mysql command.
Is this a UUOC? Is there a way to NOT use cat?
Is there another way to do this (on one line)? And to be clear I don't want to check in an if/then/else/ statement and do two different mysql commands. I like one command line that can use two different inputs.
EDIT:
Since zcat -f
will both uncompress and cat or just cat if the data isn't compressed, this isn't the best example. I'd still like to know if there is another way to <( test && do_this_command_as_input || cat this_is_the_input )
and not use cat.
And before someone mentions it ... you can't input to a mysql command line using <( )
.