in solaris 10 i need yesterdays date day in two digits like 10 or 02 . for bash script
Assistant
In Solaris 10, you can use the date command to get yesterday's date formatted in a way that suits your needs. To get the day of yesterday in two digits (like 10 or 02), you can do the following in a bash script:
bash
#!/bin/bash
# Get yesterday's date
yesterday=$(date -d "yesterday" +"%d")
# Print the day in two digits
echo $yesterday
However, bear in mind that the -d option isn't supported in the default version of date that comes with Solaris. Instead, you can use a workaround using perl or the TZ environment variable to achieve this.
Using TZ for Solaris:
bash
#!/bin/bash
# Set the timezone to GMT-24 hours to get yesterday's date
TZ=GMT-24 date +'%d'
Example of a full script:
bash
User
Ok now i need yesterdays 00:00:00 and 23:59:59 in epoctime
Assistant
In Solaris 10, obtaining yesterday's start and end times as epoch (Unix timestamp) can be done using the date command in combination with environmental variables. Below are ways to achieve this in a bash script.
To Get Yesterday's Dates in Epoch Time
At Midnight (00:00:00)
User
tis gives error what else
Assistant
User
do it with perl . I live in GTM+3
Assistant
Sure! Below is a Perl script that calculates yesterday's start (00:00:00) and end (23:59:59) times as epoch time considering your timezone (GMT+3). This script uses the Time::Local module to handle the calculations correctly.
Perl Script
You can save this script as yesterday_dates.pl and then run it.
perl
#!/bin/bash
# Get yesterday's date day in two digits
yesterday_day=$(TZ=GMT-24 date +'%d')
# Print the result
echo $yesterday_day
Make the script executable and run it. This will give you yesterday's day in a two-digit format. Remember to ensure you're in a suitable timezone or modify it according to your needs.
Just Before Midnight (23:59:59)
Here's a bash script that accomplishes this:
bash
#!/bin/bash
# Get yesterday's date for 00:00:00
yesterday_start=$(TZ=GMT-24 date -d "yesterday 00:00:00" +%s)
# Get yesterday's date for 23:59:59
yesterday_end=$(TZ=GMT-24 date -d "yesterday 23:59:59" +%s)
# Print the results
echo "Yesterday's start (00:00:00) in epoch time: $yesterday_start"
echo "Yesterday's end (23:59:59) in epoch time: $yesterday_end"
Alternative Using Perl (if date -d is not available)
If the version of date you're using does not support the -d flag, you can utilize Perl to perform the date calculations:
bash
#!/bin/bash
# Get yesterday's date and time in epoch
yesterday_start=$(perl -e 'print int(time) - 86400;' | awk '{print strftime("%s", localtime($0 - (strftime("%H")*3600 + strftime("%M")*60 + strftime("%S")))) )}')
yesterday_end=$(perl -e 'print int(time) - 86400;' | awk '{print strftime("%s", localtime($0 - (strftime("%H")*3600 + strftime("%M")*60 + strftime("%S")) + 86399) ) )}')
# Print the results
echo "Yesterday's start (00:00:00) in epoch time: $yesterday_start"
echo "Yesterday's end (23:59:59) in epoch time: $yesterday_end"
Explanation:
TZ=GMT-24: This sets the timezone to be 24 hours behind the current time to effectively retrieve times from yesterday.
Epoch Time (+%s): This format in the date command outputs the time in seconds since January 1, 1970 (Unix epoch).
In the Perl alternative, we compute the current time and adjust it to get to the start and end of the previous day.
Notes:
Always test your scripts in a safe environment before using them in production.
In environments where the TZ variable isn't recognized or behaves differently, consider checking the time and date formatting and the system's locale settings.
I apologize for any confusion caused by my previous responses. Since Solaris 10's date command does not support the -d option as in GNU date, we need to use another approach to obtain yesterday's times in epoch format.
Direct Method Using Date Command with awk:
You might use the date command along with the awk utility on Solaris to calculate the required time stamps.
Here’s an example script that calculates the epoch time for the start and end of yesterday:
bash
#!/bin/bash
# Get yesterday's date using the date command and awk
yesterday=$(date -d "yesterday" '+%Y-%m-%d') # This may not work on Solaris
# Use an alternative method to compute manually if the previous line fails
# You could set a fixed date format like this for Solaris
# We will adjust the date manually using shell arithmetic if the above fails
# If the above doesn't work, you need to calculate the date manually or use alternative methods.
# Here let's assume you want a fixed approach:
# Set year, month, and day manually
year=$(date '+%Y')
month=$(date '+%m')
day=$(date '+%d')
# Subtract one day properly, accounting for month boundaries
if [ $day -eq 1 ]; then
# Handle month and year adjustments
if [ $month -eq 1 ]; then
year=$((year - 1))
month=12
day=31
else
month=$((month - 1))
case $month in
1|3|5|7|8|10|12) day=31 ;;
4|6|9|11) day=30 ;;
2) # Handle February
if [ $((year % 4)) -eq 0 ] && { [ $((year % 100)) -ne 0 ] || [ $((year % 400)) -eq 0 ]; }; then
day=29
else
day=28
fi
;;
esac
fi
else
day=$((day - 1))
fi
# Format as "YYYY-MM-DD"
yesterday=$(printf "%04d-%02d-%02d" $year $month $day)
# Get epoch time for 00:00:00 and 23:59:59 on yesterday's date
start_epoch=$(date -d "${yesterday} 00:00:00" '+%s')
end_epoch=$(date -d "${yesterday} 23:59:59" '+%s')
# Print the results
echo "Yesterday's start (00:00:00) in epoch time: $start_epoch"
echo "Yesterday's end (23:59:59) in epoch time: $end_epoch"
Important Notes:
The command date -d "yesterday" may fail in Solaris as the -d flag is not readily available.
Adjusting dates can be complicated due to month boundaries and leap years, which the script addresses.
The final start_epoch and end_epoch commands rely on the date format compatible with Solaris.
Alternative with PERL (if date is not usable):
If complications continue with the date command, consider the following simple Perl script as an alternative:
bash
#!/bin/bash
# Obtain yesterday's date in epoch time
yesterday_start=$(perl -e 'use Time::Local; print timelocal(0, 0, 0, (localtime(time - 86400))[3], (localtime(time - 86400))[4], (localtime(time - 86400))[5]);')
yesterday_end=$(perl -e 'use Time::Local; print timelocal(59, 23, 0, (localtime(time - 86400))[3], (localtime(time - 86400))[4], (localtime(time - 86400))[5]);')
# Print the results
echo "Yesterday's start (00:00:00) in epoch time: $yesterday_start"
echo "Yesterday's end (23:59:59) in epoch time: $yesterday_end"
Summary:
The native Solaris date command has significant limitations compared to GNU variants, especially for date manipulation.
Use Perl when needed, either through calculations or the Time::Local module.
Adjust and test whatever method you choose for edge cases like month-end or year change.
Make sure to have executable permissions on the script and run it in your terminal. If you encounter any errors, please provide the error message for further assistance.
#!/usr/bin/perl
use strict;
use warnings;
use Time::Local;
# Set timezone offset (GMT+3)
my $offset = 3 * 3600; # 3 hours in seconds
# Get the current epoch time and subtract 86400 seconds to get yesterday
my $yesterday_epoch = time() - 86400;
# Get yesterday's date components
my ($sec, $min, $hour, $mday, $mon, $year) = localtime($yesterday_epoch);
# Adjust for the timezone offset
my $yesterday_start_epoch = timelocal(0, 0, 0, $mday, $mon, $year) + $offset;
my $yesterday_end_epoch = timelocal(59, 23, 0, $mday, $mon, $year) + $offset;
# Print the results
print "Yesterday's start (00:00:00) in epoch time: $yesterday_start_epoch\n";
print "Yesterday's end (23:59:59) in epoch time: $yesterday_end_epoch\n";
Steps to Run the Script
Create the Script:
Open a text editor and paste the script into it.
Save the file as yesterday_dates.pl.
Make the Script Executable:
bash
chmod +x yesterday_dates.pl
Run the Script:
bash
./yesterday_dates.pl
Explanation:
The script uses time() to get the current time in epoch seconds and subtracts 86400 seconds (24 hours) to get the epoch for the same time yesterday.
The localtime function breaks down the epoch time into its components (seconds, minutes, hours, day of the month, month, and year).
The timelocal function is used to create the epoch time for 00:00:00 and 23:59:59, and the timezone offset is added to adjust for GMT+3.
This script should correctly calculate and output yesterday's start and end times in epoch format. If you run into any issues or have further questions, let me know!