2

I have a script that kicks off a database query dependent on dates. Right now, the script defaults to yesterday's date:

function startDate() {
  date --date="yesterday" "+%Y-%m-%d";
}

START= "`startDate`"

What I want to do is pass an argument to the script, so that cron (or whatnot) can have configurable dates. I am unable to get the right syntax for neither the function nor the function call:

function startDate() {
  if [ -z "$1" ]
  then 
    date --date="yesterday" "+%Y-%m-%d";
  else
    "$1"
  fi
}

START= "`startDate \"$1\"`"

$ sh shTest.sh 2014-05-19
shTest.sh: line 6: 2014-05-19: command not found
shTest.sh: line 10: : command not found

What am I missing here?

1 Answer 1

1

Using BASH you can do:

START="$(startDate "$1")"

Also your function needs echo $1:

function startDate() {
  if [ -z "$1" ]
  then 
    date --date="yesterday" "+%Y-%m-%d";
  else
    echo "$1"
  fi
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.