Convert date String to number on Solaris shell script gives No such file or directory

人盡茶涼 提交于 2019-12-12 04:40:09

问题


I have a date in the format "Thu Sep 22 3:50 2016", and I want to convert it to format: "2016-09-22"

I tried the following shell script, which works fine for 'date', but gives error for user specified string: (I am working on Solaris platform). Any inputs will be helpful.

Input:

 #!/usr/bin/sh  
mydate="Thu Sep 22  3:50 2016"  
echo `date  +"%Y-%m-%d"`  
echo `$mydate  +"%Y-%m-%d"`

Output

./testShell.sh  
**2016-09-22**  

./testShell.sh[6]: Thu: not found **[No such file or directory]**

Any pointers please?


回答1:


Under Solaris 11, many GNU utilities are available under the /usr/gnu/bin directory so you just need to slightly modify your script to either use the full path the the GNU variant :

#!/bin/sh  
mydate="Thu Sep 22 3:50 2016"  
date  +"%Y-%m-%d"
/usr/gnu/bin/date -d "$mydate"  +"%Y-%m-%d"

or use the already existing symlink prefixed by g (for GNU):

gdate -d "$mydate"  +"%Y-%m-%d"

or set your PATH to look at /usr/gnu/bin first and keep your script unchanged.

PATH=/usr/gnu/bin:$PATH



回答2:


You can try something like this;

#!/bin/bash
mydate="Thu Sep 22  3:50 2016"  
date  +"%Y-%m-%d"
date -d "$mydate"  "+%Y-%m-%d"


来源:https://stackoverflow.com/questions/39654352/convert-date-string-to-number-on-solaris-shell-script-gives-no-such-file-or-dire

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!