Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Sat Sep 10 10:00:07 2005
Sat Sep 10 16:03:02 2005
Sat Sep 10 16:40:09 2005
Sat Sep 10 17:05:44 2005
Sun Sep 11 09:06:00 2005Thanks,
KM.

Repost.
Hi guru's,
Please help in writing a script for calculating the time difference for the 1st and last row.
Sat Sep 10 10:00:07 2005
Sat Sep 10 16:03:02 2005
Sat Sep 10 16:40:09 2005
Sat Sep 10 17:05:44 2005
Sun Sep 11 09:06:00 2005Thanks,
KM.

Not forgetting to mention it should be in csh, found another script that is written in ksh. Doesnt seem to work for me though. any difference?

There's no easy way to do it using Unix shell script. But, there are some on the web.
Other simple ways to do it are:
1. If you have access to Oracle, use months_between function (the returned value * 31 days * 24 hours * 60 minutes * 60 seconds).
2. If you're good at C. Writing a C program is much easier.
Luke Chi

Why does it have to be in csh for Heaven's sake? It is not a shell for any kind of programming.
http://www.faqs.org/faqs/unix-faq/shell/csh-whynot
Also, if you have a script written in another shell, or awk or perl, you could call that from your csh script.

An example to use different programing languages, Unix shells, in one shell script:
Shell script:
#!/bin/sh
TIME_DIFF=123
echo "In sh, TIME_DIFF is assigned $TIME_DIFF"
#!/bin/csh
echo "In csh, TIME_DIFF = $TIME_DIFF"
#!/bin/ksh
echo "In ksh, TIME_DIFF = $TIME_DIFF"
#!/bin/sh
echo "In sh, TIME_DIFF = $TIME_DIFF"Output:
In sh, TIME_DIFF is assigned 123
In csh, TIME_DIFF = 123
In ksh, TIME_DIFF = 123
In sh, TIME_DIFF = 123
Luke Chi

Hi, thanks for the feedback. The script supplied by one of the guru's here below is able to calculate the difference but i dont understand the while loop portion, can anyone explain? What does the statement "while read time remainder" mean?
example:
21:20:46 102349 Audit Number Commencing
21:44:11 DAYEND Completed Check Logfiles
21:51:22 DAYEND Completed Check Logfiles
21:52:10 DAYEND Completed Check Logfiles
21:52:52 DAYEND Completed Check Logfiles
21:53:36 DAYEND Completed Check LogfilesI need to calculate the difference between
21:53:36 to 21:20:46#!/bin/ksh
typeset -Z2 h1 m1 s1
typeset -Z2 h2 m2 s2
typeset -Z2 hh mm ssgrep '^..:..:.. ' file.in |
while read time remainder
doh2=$(expr "$time" : "\(..\):..:..")
m2=$(expr "$time" : "..:\(..\):..")
s2=$(expr "$time" : "..:..:\(..\)")if [ ! "$h1" ] ; then
h1=$h2
m1=$m2
s1=$s2
fidone
echo "from=$h1:$m1:$s1 to=$h2:$m2:$s2"
seconds=$(echo "$h2*3600+$m2*60+$s2-($h1*3600+$m1*60+$s1)" | bc)
if [ "$seconds" -lt 0 ] ; then
((seconds=seconds+86400))
fihh=$(expr $seconds / 3600)
mm=$(expr \( $seconds - $hh \* 3600 \) / 60)
ss=$(expr $seconds - $hh \* 3600 - $mm \* 60)
echo "elapsed: $hh:$mm:$ss ($seconds seconds)"from=21:20:46 to=21:53:36
elapsed: 00:32:50 (1970 seconds)

The read command reads one line of input into one or more variables, but the last variable gets all remaining words:
echo 'a b c d' | read w1 w2 w3
echo $w3
c dIn the case of while read time remainder, for each line of input, it reads the first word into $time and all remaining words into $remainder. So that's a good way to capture just a single word from a line.

Hi Thanks for enlightening me. I need to proceed further to do something like that but I couldnt get it to work in ksh. Is there a different way of writing foreach loop in ksh compare to csh? Thanks for the help.
example: cat /u/path/km/a
1
2
3
4#!/bin/ksh
foreach a (`cat /u/path/km/a`)
grep $a | ....
endThanks and Best Regards,
KM.

To process on a line-by-line basis, regardless of how many words on a line, I would use:
while read a b c
do
echo $a $b $c
done < infile
To process each individual word in a list or a file, I would use the for command:for fname in $(ls *.txt)
do
echo "Processing $fname ..."
done
# !/bin/ksh
for w in h e l l o
do
echo $w
done
./hello.sh
h
e
l
l
o
Quotes or double-quotes will allow embedded spaces in the "words":# !/bin/ksh
for w in "h e l l o" "w o r l d"
do
echo $w
done
./hello2.sh
h e l l o
w o r l d

Thanks alot Jim. I need to ask a really newbie question.
How do I do something like that in ksh?
echo "enter the ID:"
set a $<echo $a
this doesnt work in ksh, i need user of the script to enter some inputs.
Thanks and Best Regards,
KM.

The \c tells echo to suppress the carriage return. This allows the user to enter the data on the same line as the prompt. But it is not required.
echo "enter the ID: \c"
read a
echo "The user entered: $a"
echo "The user entered ${#a} characters"

Hi guru's, another question because I cant get the below to work.
if (( "$mth1" == "Sep" && "$mth2" == "Aug" )) ; then
......
filets say $mth1 and $mth2 are string, so how do I compare?
Thanks.

if [ "$mth1" = Sep -a "$mth2" = Aug ] ; then
...
fiUse = for string compare.
Use -eq -ne -gt -lt -ge for numeric compare.
Use -a to join compares with "and".
Use -o to join compares with "or".To control your and/or groupings, you can use parentheses, but you need to escape the parentheses with backslashes and have spaces before and after:
if [ \( $a -eq 1 -o $b -gt 5 \) -a $c -lt 9 ]

*** I would like to show my respects to Jim Boothe for spending so much time to help people. ***
Luke Chi

That's quite nice of you Luke. You are a great contributor yourself.
The effort of helping others fine tunes our own skills, right?

Hi, dont worry I am not asking question this time. Would just like to say a big Thank You for you guys for helping me along the way. This is really a great help for people who need to write simple scripts to automate some data extraction but do not have a strong UNIX background. By the way are you guys working as system administrator in your profession or just interested in UNIX. I myself is a Engineer in QA department for a US MNC.

As for myself, I am not a unix administrator, but rather an Oracle Apps DBA.
Although this is just my observation, many unix administrators are not so strong in scripting simply because the need is not there as much. They know all about hardware, OS installing, upgrading, patching, networking, firewalls, domains, internet, and a lot of other things.
On the other hand, a DBA works a lot more within unix, managing the database and the applications and the hundreds of thousands of files and logs, and lots of cron jobs, etc, so there is a huge need for scripting.

Hi Guru's,
here comes another question.
Input:
CBN/C0/68/004NB FSLT Sep 11 19:32:14
CBN/C0/68/004NB FSLT Sep 11 19:32:14
CBN/C0/68/004NB FSLT Sep 11 19:32:14
CBN/C0/68/004NB SLT Sep 11 17:26:31
CBN/C0/68/004NB SLT Sep 11 17:54:40
CBN/C0/68/004NB SLT Sep 11 18:24:37
CBN/C0/68/004NB SLT Sep 11 19:09:54
CBN/C0/68/004NB SLT Sep 11 19:32:14
FA0/91/11/940NB SLT Sep 11 20:13:51
FA0/91/12/025NB SLT Sep 11 20:56:11
FA1/01/02/115NB SLT Oct 10 21:47:14Output:
CBN/C0/68/004NB SLT Sep 11 19:32:14
FA0/91/11/940NB SLT Sep 11 20:13:51
FA0/91/12/025NB SLT Sep 11 20:56:11
FA1/01/02/115NB SLT Oct 10 21:47:14In I have a input like that how can I convert to the output I want. I need all the lines starting with FA and only the lastest line starting with CBN assuming I can already sort the whole thing in order.

Just replying to a post way up above:
> An example to use different programing
> languages, Unix shells, in one shell script:
>
> Shell script:
>
> #!/bin/sh
> TIME_DIFF=123
> echo "In sh, TIME_DIFF is assigned $TIME_DIFF"
> #!/bin/csh
> echo "In csh, TIME_DIFF = $TIME_DIFF"
> #!/bin/ksh
> echo "In ksh, TIME_DIFF = $TIME_DIFF"
> #!/bin/sh
> echo "In sh, TIME_DIFF = $TIME_DIFF"
>
> Output:
>
> In sh, TIME_DIFF is assigned 123
> In csh, TIME_DIFF = 123
> In ksh, TIME_DIFF = 123
> In sh, TIME_DIFF = 123The example as posted is a Bourne shell script with three comment lines. It does not switch shells after it has started, but just ignores lines beginning with '#'.
Only the first "#!" path is used as the interpreter for executing the script. I suspect the rule will depend on platform, but AFAIK it needs to appear as the top line. The OS then starts a process using the specified executable, as you can see from entering "ps" while the script is running.
You can confirm this by attempting to switch to a csh interpreter and execute a csh-specific command such as "setenv x 1".

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |