Bash: Formatting date [duplicate] - bash

This question already has answers here:
YYYY-MM-DD format date in shell script
(17 answers)
Closed 2 years ago.
I would like to use date in the following format: yyyyMM.dd.HHmm. i.e. 202008.15.1742
This is the Bash file:
#!/usr/bin/env bash
VERSION_CODE=$((($(date +%s%N)/1000000))
plutil -replace CFBundleVersion -string "$VERSION_CODE"
How do I set VERSION_CODE such that the current time appears in this format: yyyyMM.dd.HHmm?

A pure Bash solution without spawning a sub-process:
printf -v VERSION_CODE '%(%Y%m.%d.%H%M)T'

You can do it like this:
VERSION_CODE=$(date +"%Y%m.%d.%H%M")

Related

Get current time (and date) WITHOUT opening a subshell [duplicate]

This question already has answers here:
Getting the current date in bash without spawning a sub-process
(2 answers)
YYYY-MM-DD format date in shell script
(17 answers)
Closed 2 years ago.
Is it possible to get current time ( and possibly date ) without doing it via a subshell?
because if I'm not mistaken, this command do open a subshell?
d=$(date)
With Bash≥4.2 you can use printf with the %(datefmt)T format:
printf '%(%c)T\n' -1
-1 means now.
See The Bash reference at the printf entry.
To put it in a variable (and hence not use a subshell):
printf -v d '%(%c)T' -1
echo "$d"

GET Dynamic Date file using SFTP? [duplicate]

This question already has answers here:
Using variables inside a bash heredoc
(3 answers)
Closed 3 years ago.
I am creating a bash file which will download file daily but file name is dynamic it generates file on the basis of date something like '20190819.log' My code is as follows :
export SSHPASS=$SFTP_PASS
DATE=`$DATE -d "-1 day" +"%Y%m%d"`
sshpass -e sftp -oBatchMode=no -b - $SFTP_USER#$SFTP_HOST<<-'EOF'
mget $DATE.log
EOF
But in line mget $DATE.log it is treating $DATE as string.
Need help...Thanks in advance.
When you put quotes around the here-doc end delimiter (<<-'EOF' in this case), you are asking the shell to not expand parameters in the here-doc. If you want shell variables to be expanded, lose the quotes (<<-EOF).
But in this particular case, you might find that scp is easier to use.

How to name a file with current time? [duplicate]

This question already has answers here:
Redirect output to filename given by result of command
(3 answers)
Closed 4 years ago.
I'm trying to create a file using bash that has the current time as it's name. This is how I'm trying to do it:
echo 'hello' > date +"%T".txt
What am I missing?
Use command substitution to capture date's output as a string.
echo 'hello' > "$(date +%T)".txt

How should I sort one word using shell? [duplicate]

This question already has answers here:
How to sort characters in a string?
(5 answers)
Closed 4 years ago.
I can use every language to realize it, but I want to use shell.
Use shell command or shell script.
For example, I have one word: sport.
I want to get another word: oprst.
Now I want to use shell to bring about it, how to do? Thanks!
Use the following Shell Script:-
#!/bin/sh
sortedWord=`echo $1 | grep -o . | sort |tr -d "\n"`;
echo $sortedWord;

storing 'du' result in a variable [duplicate]

This question already has answers here:
How to return the output of program in a variable?
(4 answers)
Closed 7 years ago.
I am using Putty with bash-4.2. Therein, I am outputting file size with:
du -m myfile.csv
which returns:
1.25 myfile.csv
How do i store this line in a variable so I can later parse out the filesize?
Thanks in advance
Like so
FOO="$(du -m myfile.csv)"
echo "$FOO"
Output
1.25 myfile.csv

Resources