3

I have a ruby file that I would normally run in command line as follows:

ruby file.rb YYYY-MM-DD YYYY_MM_DD

I want to write a bash script to run this file where both YYYY-MM-DD are strings for yesterday's date.

How would I do that?

DigitalRoss
  • 143,651
  • 25
  • 248
  • 329
delisdeli
  • 839
  • 9
  • 20

2 Answers2

3
ruby file.rb $(date -d yesterday +'%Y-%m-%d') $(date -d yesterday +'%Y_%m_%d')

Note that this will only work in bash. Other Bourne-like shells will work if you use backticks instead of $().

Taymon
  • 24,950
  • 9
  • 62
  • 84
  • The caveat is wrong; `$(command)` should work in any POSIX shell. If you're on something truly ancient like SunOS 3, then maybe. – tripleee Jul 13 '13 at 08:20
2

See "YYYY-MM-DD format date in shell script" to figure out how to get the date in whatever format you want.

Yesterday's date can be found as:

date -d '1 day ago' +'%Y/%m/%d'

from "How To Get Yesterday’s Date using BASH Shell Scripting".

Replace the / with - or _ and then pass them in to the Ruby statement.

Edit: Vote for the other guy. Their answer actually has code.

Community
  • 1
  • 1
Paarth
  • 9,687
  • 4
  • 27
  • 36