Problem with date in conjunction with cut?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with date in conjunction with cut?
# 1  
Old 11-02-2011
Problem with date in conjunction with cut?

I got rather bored so i decided to create a script that creates a countdown, and shows hours:minutes:seconds till that time.
It works fine until the seconds of the actual time reaches 8, then it tries to use it to work out the difference as in "SECONDDIFF=$[SECOND-ALARMSECOND]"
Here's my code where I get the problem :

Code:
ALARMHOUR=`echo "$ALARMTIME" | cut -d : -f 1 `
ALARMMINUTE=`echo "$ALARMTIME" | cut -d : -f 2 `
ALARMSECOND=`echo "$ALARMTIME" | cut -d : -f 3 `
...

TIME=`date "+%H:%M:%S"`

HOUR=`echo "$TIME" | cut -d : -f 1 `
MINUTE=`echo "$TIME" | cut -d : -f 2 `
SECOND=`echo "$TIME" | cut -d : -f 3 `

SECONDDIFF=$[SECOND-ALARMSECOND]
SECONDDIFF=$[60-SECONDDIFF]

The line i get the error at is SECONDDIFF=$[SECOND-ALARMSECOND] and
The error i'm getting is :
/bin/TermTime (unstable): line 35: 08: value too great for base (error token is "08")
I simply cannot even guess how to fix this, i simply have no idea, Help me please someone?
# 2  
Old 11-02-2011
What shell are you using?

There's several ways better than running cut 3 times to process one line.

A way that'll work in all shells:

Code:
VAR="12:34:56"

OLDIFS="$IFS"
IFS=":";   set -- $VAR;   IFS="$OLDIFS"
HH=$1
MM=$2
SS=$3

This will overwrite your $1,$2,$3 parameters.

Other ways may be available depending what your shell is.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 11-02-2011
I'm using BASH... On a mac running lion if that helps
# 4  
Old 11-02-2011
It does.

Try X=$((Y-Z)) instead of X=$[Y-Z].

BASH can also do this:

Code:
read A B C <<<$(date +"%H %M %S")

Hundreds of times faster than running three external utilities to process one line.
# 5  
Old 11-02-2011
or:
Code:
#!/bin/ksh
eval "$( date '+ h=%H =%H m=%M s=%S' )"
echo $h $m $s

# 6  
Old 11-02-2011
Thank you for the help speeding up the execution, however I only posted for the problem i was getting... Any idea what it is and how to sort it out?
# 7  
Old 11-02-2011
Check my last post. I answer your question directly and tell you how to fix it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Print/cut/grep/sed/ date yyyymmdd on the filename only.

I have this filename "RBD_EXTRACT_a3468_d20131118.tar.gz" and I would like print out the "yyyymmdd" only. I use this command below, but if different command like cut or print....etc. Thanks ls RBD_EXTRACT* | sed 's/.*\(........\).tar.gz$/\1/' > test.txt (9 Replies)
Discussion started by: dotran
9 Replies

2. UNIX for Dummies Questions & Answers

Nohup in conjunction with time

hi, if I exectute "nohup time ls -1" I get the following output $ nohup time ls -1 file_1 file_2 file_3 file_4 file_5 0.000u 0.001s 0:00.00 0.0% 0+0k 0+0io 0pf+0w This is all OK. But if I want to capture this whole output in to a text file I would want to use something like ... (1 Reply)
Discussion started by: BearCheese
1 Replies

3. Shell Programming and Scripting

Problem with cut and wc

Hi, I am facing issue with cut and wc. here is the sample. the data in file - tail -1 05_19_BT_TBL_LOAD_20120524064242.bad|cut -c9-58 WatsSaver - AGGREGATED PLAN1581 CALLS FOR 2872.6 tail -1 05_19_BT_TBL_LOAD_20120524064242.bad|cut -c9-58|wc -c 51 tail -1... (12 Replies)
Discussion started by: donadarsh
12 Replies

4. Shell Programming and Scripting

Missing conjunction

Hi Gurus, I have prepared a script to find the log file based on a date range defined in one of the environment files, archive the logs files and move them to a particular directory. Below is the script: . /home/.profile . /home/.inf_env logfile=$scripts_path/Logs/file_archive1.log... (17 Replies)
Discussion started by: svajhala
17 Replies

5. Shell Programming and Scripting

Problem with cut

I need to read in a file and output it without the comments or newlines. The problem is that is not outputting it properly. STUDENTSDETAILFILE="../data/studentDetails.txt" for getlines in `sed '/#/d' $STUDENTSDETAILFILE` do STUDENTID=`echo $getlines | cut -d: -f1` ... (1 Reply)
Discussion started by: nerdbot
1 Replies

6. Shell Programming and Scripting

Cut and WhiteSpace Problem

Hi, I have a shell script that reads a parameter file to set variables. I have an issue when the parameter I try to read contains whitespace. e.g File Contents Code The result is SUBJECT is set to and I want subject set to I've tried different variations but nothing seems to... (19 Replies)
Discussion started by: Greygor
19 Replies

7. Shell Programming and Scripting

cut problem

Hi, sample datas are : drwxr-xr-x 2 beewin abidev 96 Jun 13 2006 bwin drwxrwxr-x 2 blsmg01 smg 96 Jun 13 2006 blsmg01 drwxr-xr-x 2 ccmdummy ccm_root 8192 Jun 13 2006 ccmdum drwxr-xr-x 5 dipayan users 8192 Oct 29 09:05 dip I want to cut the last field. I use ls -ll | grep... (7 Replies)
Discussion started by: samir_standing
7 Replies

8. UNIX for Dummies Questions & Answers

'Cut'ting date time

My apache logs look like this... PHP Warning: Invalid argument supplied for foreach() in /usr/local/apache2/htdocs/myfile.php PHP Warning: Invalid argument supplied for foreach() in /usr/local/apache2/htdocs/myfile.php PHP Warning: Invalid argument supplied for foreach() in... (1 Reply)
Discussion started by: shantanuo
1 Replies

9. Shell Programming and Scripting

conjunction two files

I need to your help. I want write a script search for rows in file1 if exist in file2 it will print rows from file2 else it will print rows from file1 with out any duplicate ... (4 Replies)
Discussion started by: kmuqalled
4 Replies

10. UNIX for Advanced & Expert Users

Cut date using y hat as delimiter

QUESTION: How can I cut out the date from just the first line and reformat it to 31-Jul-2007? I'll restate the question at the bottom again... DESCRIPTION: I need to cut a date out of a file - an example of the date's format in the file is 2007-07-31. It's in the 5th field and is separated... (1 Reply)
Discussion started by: tekster757
1 Replies
Login or Register to Ask a Question