0

I Have a bash .sh script and I am trying to zip up yesterday's log and archive in a AWS Bucket. I need some help with getting the format of the file name correct but not having luck with it. Here is the script:

#!/bin/bash

/jobs/copytS3.sh /var/log/appname_log_"date" --date="yesterday" +'%Y%m%d'.log

Output shows following:

zip warning: name not matched: /var/log/appname_log_date

Not sure what I am missing. The name of the file should be: appname_log_20210120.log.

I am sure it's something very basic, but I'm unable to resolve. Appreciate any help with the syntax. Thanks!

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
NetSystemAdmin
  • 505
  • 1
  • 6
  • 16
  • Note that `date` is not part of bash itself -- it's provided by your OS vendor. Thus, the correct syntax to use for `date` depends; are you on Linux? MacOS? BSD? ...? – Charles Duffy Jan 21 '21 at 23:41
  • ...now, _newer_ versions of bash _do_ have a built-in way to generate a string-formatted `date` with `printf`, but you need to be sure that you're only targeting systems with a quite new release (I think maybe 4.3 or later?) – Charles Duffy Jan 21 '21 at 23:42
  • That said, insofar as the question was immediately caused by failure to use `$(...)` around your `date` command, that makes it a duplicate of [How do I set a variable to the output of a command in bash?](https://stackoverflow.com/questions/4651437/how-do-i-set-a-variable-to-the-output-of-a-command-in-bash). – Charles Duffy Jan 21 '21 at 23:44
  • (As another aside, it's bad form to put `.sh` extensions on your bash scripts; see [Commandname extensions considered harmful](https://www.talisman.org/~erlkonig/documents/commandname-extensions-considered-harmful/), and the [history of the irc.freenode.org #bash factoid on the topic](http://wooledge.org/~greybot/meta/.sh)). – Charles Duffy Jan 21 '21 at 23:46
  • (...extensions are appropriate for shell _libraries_, and should be named according to which shell(s) those libraries are compatible with: `.sh` for libraries that work with all POSIX-compliant shells, `.bash` for bash-only ones, `.zsh` for ZSH-only ones, etc. By contrast, executable commands and scripts which aren't libraries shouldn't have extensions at all -- this is much like how Python _libraries_ end in `.py`, but Python _scripts_ don't have extensions -- you run `pip`, not `pip.py`). – Charles Duffy Jan 21 '21 at 23:47

1 Answers1

2

Try this:

/jobs/copytS3.sh /var/log/appname_log_$(date --date=yesterday +'%Y%m%d').log
brunoff
  • 4,161
  • 9
  • 10