bash substring of command instead of variable? - bash

I can do this in bash:
foo=bar
echo ${foo:0:2}
which prints 'ba' (the first two characters of 'bar').
Now I want to do the same with a script/command output instead of a variable, like so:
echo ${$(date):0:10}
But then I get an error: "bad substitution".
Of course I can use an intermediary variable:
foo=$(date)
echo ${foo:0:10}
But is there a way to do this directly?
P.S. The date command is just an example, this is not about generating some date string in a particular format. Just the general concept of taking a substring from an arbitrary shell command output.

No, BASH syntax doesn't allow any kind of nesting. You can do so using external utilities like cut:
date | cut -c 1-10
Wed Jun 13

To replace date:
$ date
Wed Jun 13 14:57:38 EEST 2018
you can use printf:
$ printf "%(%a %b %d%n)T"
Wed Jun 13
man strftime for more format modifiers.

If you want the output Wed Jun 13 for today's date (which this is), then
$ date +'%a %b %e'
Wed Jun 13
See the manual for date and/or for strftime on your system.

Related

How I can format a date in shell

I want to convert a custom date format DD-%%%-YYYY to a standard one: YYYYMMDD
Possible values of %%% are:
Jan Fev Mar Avr Mai Jun Jui Aou Sep Oct Nov Dec
Assuming the input is a bash variable, how do I transform it to the standard format?
Example:
$ fr_date='09-Aou-2018'
$ # [transformation]
# sql_date should now contain 20180809
$ echo "$sql_date"
20180809
You can use the date utility.
fr_date='09-Aug-2018'
sql_date="$(date --date=$fr_date +%Y%m%d)"
echo $sql_date
20180809
Please also refer to the date man page for more information.
Additionally, date does not support custom locales, format must be locale independent. Try to store dates as simple Unix epoch.
Solution 1: Rewrite the french months into english, then use date to read and format it:
Pure bash:
tmp=${fr_date/Fev/Feb} tmp=${tmp/Avr/Apr} tmp=${tmp/Mai/May}
tmp=${tmp/Jui/Jul} tmp=${tmp/Aou/Aug}
sql_date=$(date +%Y%m%d -d "$tmp")
With sed:
tmp=$(sed 's/Fev/Feb;s/Avr/Apr;s/Mai/May;s/Jui/Jul;s/Aou/Aug' <<<"$fr_date")
sql_date=$(date +%Y%m%d -d "$tmp")
Solution 2: Assign to each month its corresponding number:
#!/bin/bash
# Requires bash 4 for associative arrays
declare -A month_map=(
[Jan]=01 [Fev]=02 [Mar]=03 [Avr]=04 [Mai]=05 [Jun]=06
[Jui]=07 [Aou]=08 [Sep]=09 [Oct]=10 [Nov]=11[Dec]=12
)
IFS=- read -r day month year <<<"$fr_date"
sql_date=$year${month_map[$month]}$day

`Date` command formatting in Solaris Unix?

How to the below output using date formatting..!?
Date command output should look like:
Mon Oct 24 12:01:32 SGT 2016:
date +%a" "%b" "%d" "%T" "%Z" "%Y
This will provide the expected output..!

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

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?
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
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"

Eliminating time zone from date string in a Unix shell script

I have date formatted like below and I want to remove GMT+05:30 from the date using a shell script.
Tue Dec 4 17:30:51 GMT+05:30 2012
and output should be
Tue Dec 4 17:30:51 2012
I am new to shell script and still learning.
Use sed if you can't change the date output:
... | sed 's/GMT+5:30 //g' | ...
But a better solution is to use date formatting capabilities:
date +"%a %b %d %T %Y"
(for details, see man date)

Get date of some UNIX time

How can I convert UNIX time to date format?
Smth like
$> date ???? 1300000000
Mar 13 2011 07:06:40 GMT
Your date command might understand the # prefix. Try:
$ date -d #1300000000
Sun Mar 13 08:06:40 CET 2011
If -d doesn't work for you, thats probably because it only works like that for GNU date.
For BSD, OSX, etc. one would ordinarily use:
date -r 1300000000

Resources