How to create simple BASH Scripting, echo time and date - linux

how to create a bash script
I want to echo good morning with time and date

#!/bin/bash
echo "Good Morning, today's date is $( date )"
echo "Quote of the Day"
echo "Today I will try to be less lazy than I was yesterday"

Related

Print the path of a file a day before and a day after in Shell script

I am having a shell script like below
#!/bin/bash
TIMESTAMP=`date "+%Y-%m-%d"`
path=/home/$USER/logging/${TIMESTAMP}/status/${TIMESTAMP}.fail_log
echo filePath=$path
In this script I want to print the path of the failed logs for that particular timestamp.
Now I am able to get the echo to print the path.
How do I print a day before and day after the timestamp? Is it possible to do that?
How Can I do that in a single line of code? Can we do that?
To get tomorrow's data, you can do:
date -d '+1 day' "+%Y-%m-%d"
To get yesterday's data, you can do:
date -d '-1 day' "+%Y-%m-%d"
To use it in script:
#!/bin/bash
nextDate=$(date -d '+1 day' "+%Y-%m-%d")
prevDate=$(date -d '-1 day' "+%Y-%m-%d")
nextDatePath=/home/$USER/logging/${TIMESTAMP}/status/${nextDate}.fail_log
prevDatePath=/home/$USER/logging/${TIMESTAMP}/status/${prevDate}.fail_log

Issue echoing variables in bash script

I was working on a script to echo the date upon login in a different format but once I get down to echoing the result, it gives me a jumbled up output. I've been searching online to see if I'm calling the variables wrong or using wrong ticks somewhere but no luck. I even have echoed each individual variable before and after the problem echo and they echo the proper date/month/day of week. As my script is right now, it only puts out ". which is a Thu" when run. Also, I've been executing it with "sh ./datescript.sh" Any help/additional resources would be appreciated. Thanks!
My Script:
#!/usr/bin/env bash
date=`date` #NOTE: date being used in two different ways
day=`echo ${date} | cut -f1 -d' '`
month=`echo ${date} | cut -f2 -d' '`
date=`echo ${date} | cut -f3 -d' '`
echo "Today is the ${date}th day of ${month}, which is a ${day}."
echo $day
echo $month
echo $date
I can replicate the behaviour if I add $'\r' at the end of month and date assignment lines. Seems like Win/*nix line ending issue.
Run dos2unix or fromdos on the script to fix it.
#!/usr/bin/env bash
day=`date +%A`
month=`date +%B`
date=`date +%-d`
echo "Today is the ${date}th day of ${month}, which is a ${day}."
echo $day
echo $month
echo $date

How to read date from user in linux and use that date in svn log

I am new to shell scripting.
I want to write a shell script to get the date from user from the terminal in format yyyy-mm-dd and use that date as start date to get the revision changes made in svn repository for a particular date range.
My script is:
echo "Date"
echo "Enter year"
read Y
echo "Enter month"
read M
echo "Enter Date"
read D
D1=`expr $D + 3`
svn log -r {$Y-$M-$D}:{$Y-$M-$D1} http://svn.abc.com/svn/trunk.
This is the script I have written.
I know I should use date function not sperately Y M D.
And also I want to list only revision numbers not full log messages as svn log command shows.
I would use the date utility for verification:
while true; do
read -p "Enter a date (YYYY-mm-dd): " user_date
if date=$(date -d "$user_date" +%F); then
# user date was ok
break
fi
done
date3=$(date -d "$date + 3 days" +%F)
svn log -r "$date:$date3" ...
You really need to use date, especially to add 3 days: you don't want to end up with "2014-02-30"

How to get time since file was last modified in seconds with bash?

I need to get the time in seconds since a file was last modified. ls -l doesn't show it.
There is no simple command to get the time in seconds since a file was modified, but you can compute it from two pieces:
date +%s: the current time in seconds since the Epoch
date -r path/to/file +%s: the last modification time of the specified file in seconds since the Epoch
Use these values, you can apply simple Bash arithmetic:
lastModificationSeconds=$(date -r path/to/file +%s)
currentSeconds=$(date +%s)
((elapsedSeconds = currentSeconds - lastModificationSeconds))
You could also compute and print the elapsed seconds directly without temporary variables:
echo $(($(date +%s) - $(date -r path/to/file +%s)))
In BASH, use this for seconds since last modified:
expr `date +%s` - `stat -c %Y /home/user/my_file`
I know the tag is Linux, but the stat -c syntax doesn't work for me on OSX. This does work...
echo $(( $(date +%s) - $(stat -f%c myfile.txt) ))
And as a function to be called with the file name:
lastmod(){
echo "Last modified" $(( $(date +%s) - $(stat -f%c "$1") )) "seconds ago"
}

Better quote to execute command on shell script

I'm in doubt of the diference and which one is the better quote to execute a command in shell script.
For example, I have this two examples:
echo "The name of the computer is `uname -n`"
echo "The name of the computer is $(uname -n)"
Which one is better? Or there is no diference?
The $(...) one is generally recommended because it nests easier. Compare:
date -d "1970-01-01 $(echo "$(date +%s)-3600"|bc) sec UTC"
date -d "1970-01-01 `echo \"\`date +%s\`-3600\"|bc` sec UTC "

Resources