Visit Our UNIX and Linux User Community


Problem - script that parses $date data...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem - script that parses $date data...
# 1  
Old 10-24-2009
Problem - script that parses $date data...

This is probably archaic, but I'm new to unix and this is my first shell script. I'm writing this script to use in another script. All I am trying to do is make the parts of the output from date usable in my other script. I was wondering if you could stand looking at this and see if you notice why I can't get my last line to execute.

Code:
#! /bin/sh
wday=`date | head -c +3`
echo "$wday"
cyear=`date | tail -c -5`
echo "$cyear"
ahour=`date | head -c +13 | tail -c +12 -`
if [$ahour - 12 -ge 0]
then
thour=`expr $ahour - 12`
x=`PM`
else
thour=`12 - $ahour`
x=`AM`
fi

if [$wday == Mon]
then
wday= 'Monday'
else
if [$wday == Tue]
then
wday= 'Tuesday'
else
if [$wday == Wed]
then
wday= 'Wednesday'
else
if [$wday == Thu]
then
wday= 'Thursday'
else
if [$wday == Fri]
then
wday= 'Friday'
else
if [$wday == Sat]
then
wday= 'Saturday'
else
if [$wday == Sun]
then
wday= 'Sunday'
fi
echo "$wday"
echo "The time is $thour $x."
exit 0

# 2  
Old 10-24-2009
this date command will display the desired o/p
Code:
date +'%A %I:%M:%S %p'

Saturday 01:53:49 PM
# 3  
Old 10-24-2009
Thank you, that works very well. But I can't just grab the day of the week out as a variable. I also can't grab the year. I am trying to use my script to make the data available. Some of the commands (as the one you made so simple) are just there for troubleshooting.

Can you help me make my script work?
# 4  
Old 10-24-2009
you can grab them in any variable you want...
Code:
date +'%A %I:%M:%S %p %Y'|read wday thour x cyear
echo $wday $thour $x $cyear

# 5  
Old 10-24-2009
Hmm... I tried that, and when I put in $echo $wday, I get 'Sat'. For the other variables I get nothing...?

I then broke up the first bit you gave me, date +'%A' | read wday and this still gives me just Sat. How do I get it to display Saturday?

---------- Post updated at 03:59 AM ---------- Previous update was at 03:59 AM ----------

Hmm... I tried that, and when I put in $echo $wday, I get 'Sat'. For the other variables I get nothing...?

I then broke up the first bit you gave me, date +'%A' | read wday and this still gives me just Sat. How do I get it to display Saturday?
# 6  
Old 10-24-2009
Hi.

The solution from vidyadhar85 works fine in KSH.

A bash solution, using a here string:

Code:
read wday thour x cyear <<< $(date +'%A %I:%M:%S %p %Y')

This also works in newer Korn shells.

As for %A, from the date man page:
Quote:
%A locale's full weekday name (e.g., Sunday)
# 7  
Old 10-24-2009
Thanks, that works perfectly. Care to explain how that statement works? Can you use that sort of statement to parse any data or just the date statement?

Previous Thread | Next Thread
Test Your Knowledge in Computers #939
Difficulty: Easy
The author named the ping utility after the sound that a sonar makes.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with date on bash script

i am beginner in bash scripting and i am looking for a command that gives me one minute ago. i am working on unix ware7 and i cant use date -d or date --date command thanks for help !! (4 Replies)
Discussion started by: rezasadeghi
4 Replies

2. Homework & Coursework Questions

Need to write a script user.sh that parses name & password

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write a script called user.sh that parses the name and password of given in a file (user.txt) and creates the user... (1 Reply)
Discussion started by: Nygenesis
1 Replies

3. Shell Programming and Scripting

Script (ksh) to get data in every 30 mins interval for the given date

Hello, Since I m new to shell, I had a hard time to sought out this problem. I have a log file of a utility which tells that batch files are successful with timestamp. Given below is a part of the log file. 2013/03/07 00:13:50 Apache/1.3.29 (Unix) configured -- resuming normal operations... (12 Replies)
Discussion started by: rpm120
12 Replies

4. Shell Programming and Scripting

Script problem with deleting data

The 10.Delete data doesnt work at all, plesase anyone could help on that. When I choose options 10 to delete a record it only copy the selected data on the other file dbs1 but doesnt delete it from the database where other records are. #! /bin/bash i="y" echo "Enter name of database " read db... (6 Replies)
Discussion started by: Lina_14
6 Replies

5. Shell Programming and Scripting

Echo date variable from data input to a script

Hi, I'm trying to make a script which you type the year, select the month and day and then create the date in the format 2010-12-7. #!/bin/bash dia () { echo " Seleccione el dia:" select file in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Salir do... (6 Replies)
Discussion started by: iga3725
6 Replies

6. Shell Programming and Scripting

php date script problem

i want to store the output of date into a variable and be able to echo out that variable in a sentence. but the below isn't working. <?php #$date_time = date('Y-m-d H:i:s'); putenv("TZ=US/Eastern"); date_time = "date('h:i:s')."\n""; echo ("<li>Today's date is: ($date_time)</li>\n"); ... (1 Reply)
Discussion started by: SkySmart
1 Replies

7. Programming

Date time problem while executing perl script.

Hi All, This Monday 15th March 2010, i have faced a weired issue with my Perl script execution, this script is scheduled to run at 1 minute past midnight on daily basis ( 00:01 EST ) generally for fetching previous business date , say if it is Monday it should give last Friday date, for Tuesday... (0 Replies)
Discussion started by: ravimishra
0 Replies

8. UNIX for Dummies Questions & Answers

Date Script problem for year cross over

Hello, I'm very new to script writing - everything I have I got off the Internet. I'm pretty sure I stole this date script from this site. Anyway, the script works great until I try to obtain a date that falls into last year. I can get 'Dec 31, 2009' but nothing earlier. Below is the... (3 Replies)
Discussion started by: Colel2
3 Replies

9. Shell Programming and Scripting

Problem to get yesterday's date flatfile with shell script

hi, i was required to write a shell script to get yesterday's date flatfile. but i only know how to get today's date flatfile. Please observed my below scripting: Please help! Thanks ================================================= #!/bin/sh HOST='192.168.1.200' USER='ftp1'... (19 Replies)
Discussion started by: lifeseries
19 Replies

10. Shell Programming and Scripting

A newbie with a problem in A date Script

Hello everybody... I'm a Unix newbie and i just got this task at work to figure out what's wrong with a daily script my team is using. The idea behind the script is that it takes the day before in a yyyymmdd format, find files with that date in a specific directory and executes an (irrelavant)... (4 Replies)
Discussion started by: adija
4 Replies

Featured Tech Videos