How do I get a date of yesterday in expect - linux

I write a expect script 1.sh:
#!/usr/bin/expect
set yest [exec `date -d "yesterday" '+%Y%m%d'`]
send_user $yest
exit 1
And run in linux ,use expect -d ./1.sh
But I get this error:
expect version 5.44.1.15
executing commands from command file ./1.sh
invalid command name "/bin/date"
while executing
"date -d "yesterday" '+%Y%m%d'"
invoked from within
"set yest [date -d "yesterday" '+%Y%m%d']"
So,how do I fix this.I googled somethings and have no idea.
I find clock to instead date:
https://www.tcl.tk/man/tcl8.5/TclCmd/clock.htm
And I get a way to run expect in bash:
#!/bin/bash
expect <<!
## expect code ##
!
Finally use this way to get yesterday string in expect script:
set yest [clock scan "yesterday"]
set yest1 [clock format $yest -format {%Y%m%d}]

Try this
#!/usr/bin/expect
set yest [ exec /bin/date -d "yesterday" +%Y%m%d]
send_user $yest
exit 1

Try out this way:
For yesterday :
date -d '-1 day' '+%Y%d%m'
For day before yesterday :
date -d '-2 day' '+%Y%d%m'

Related

How to run a scrip if the time is matched with a time in another time zones

Currently, I am using cloud VMs to run my code and because of that I am assigned with a new VM that is in a different time zone. I want to run a bash script that runs a python script at 7:30 pm (Eastern time). From here I know how to run a bash script at a specific time, e.g., echo "ls -l" | at 07:00. From here I know how to get the current time of Eastern time, e.g., TZ=America/New_York date. Also, from here I know how to get only the time using date +%R.
I am a Python coder and tried my best to write a sudo code that shows what I am trying to accomplish as a bash script:
while true
do
Now=TZ=America/New_York date +%R
if [Now -eq 7:30pm]
then
python3 myfile.py
done
As you already know how to set the at command to execute a command
at the specified time, and how to convert the EST to the local time,
you can just combine them:
echo "python3 myfile.py" | at $(date -d "19:30 EST" +%R)
When you invoke the at command, it always warns
"commands will be executed using /bin/sh". It will matter only if we invoke
a bash specific command such as:
echo "shopt -s globstar; ls **" | at ..
which will fail.
In our case, the command python3 myfile.py will run with both
/bin/sh and /bin/bash then you do not worry about the warning.
date -d STRING interprets the STRING as a date/time representation
and prints the converted date/time in the specified format +%R.
If you want to send the output to a file, you can say:
echo "python3 myfile.py > /path/to/a/file" | at $(date -d "19:30 EST" +%R)
In order to output to the current terminal, first identify the terminal
with tty command:
$ tty
=> /dev/pts/0
Then redirect the output to the terminal with:
echo "python3 myfile.py > /dev/pts/0" | at $(date -d "19:30 EST" +%R)

Error when using shutdown command in Bash

read -p 'Want to use the timer (y/n) ? ' jwb2
if [[ $jwb2 =~ ^[Yy]$ ]]; then
echo -n "Now Days "
date +"%A and hour %T"
read -p 'How many hours [24hrs] :' jwb3
echo Process the command
sudo bash -c "Shutdown -h $jwb3"
fi
and running the script now,
me#linux:$./main.sh
Want to use the timer (y/n) ? y
Now Days Thursday and hour 16:09:49
How many hours [24hrs] :18:00
Process the command
bash: Shutdown: command not found
Why am I getting this error?
Use shutdown instead Shutdown.

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

Linux Bash for Monthly Archiving

I need help regarding my script on the 1st part is working but when adding more it shows unknown variables lastyear, month
#!/bin/bash
year=$(date +%Y)
lastyear=$(expr $year-1)
month=$(date +%m)
log=$lastyear$month
mkdir -p /root/temp/$(lastyear)
mkdir -p /root/temp/$(lastyear)/$(month)
mv -f *$log* $(archivefolder)/$(lastyear)/$(month)
Error prompt is
./logdate.sh: line 8: lastyear: command not found
./logdate.sh: line 9: lastyear: command not found
./logdate.sh: line 9: month: command not found
But When I include only till line 6 it's working
Do not put the variables in brackets, at the moment the shell is trying to execute the command lastyear and put it into a variable. The below should be fine:
year=$(date +%Y)
lastyear=$(( year-1 ))
month=$(date +%m)
log="$lastyear$month"
mkdir -p "/root/temp/$lastyear"
mkdir -p "/root/temp/$lastyear/$month"
mv -f "*$log*" "$archivefolder/$lastyear/$month"

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