1

I have a string in following format: YYYY-MM-DD

how can i convert it to date on UNIX and freeBSD? I know it is

date -d $VAR +'%Y-%m-%d' on GNU

and

date -jf $VAR +'%Y-%m-%d' on freeBSD

But what if my script (in sh, not in bash) have to work on both OS? How can I combine it?

WhoKnows
  • 317
  • 1
  • 12

1 Answers1

1

Because date command is different a kind of solution might be if detect correct platform:

#!/bin/sh

VAR='2014-03-15'
platform='uknown'
str="$(uname)"

if [ "$str" == 'Linux' ]; then
    platform='linux'
elif [ "$str" == 'FreeBSD' ]; then
    platform='freebsd'
fi

Then according to the platform you can do:

if [ "$platform" == 'linux' ]; then
    date -d "$VAR" +'%Y-%m-%d'
elif [ "$platform" == 'freebsd' ]; then
    date -jf "%Y-%m-%d" "$VAR" +'%Y-%m-%d'
fi

Also I think you are missing format for the command:

date -jf $VAR +'%Y-%m-%d'

I think i should be:

date -jf "format" $VAR +'%Y-%m-%d'
taliezin
  • 931
  • 1
  • 14
  • 20