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?
# 8  
Old 11-02-2011
Oh ok thank you, I didn't see that bit... somehow.. Thank you Smilie

---------- Post updated at 06:41 PM ---------- Previous update was at 06:36 PM ----------

So @Corona, Whats the difference between '[' and '(('?

Just tested it, it hasn't fixed the problem... And i thought it had

Last edited by DuskFall; 11-02-2011 at 03:43 PM.. Reason: Correction
# 9  
Old 11-02-2011
$(( is BASH arithmetic syntax, $[ isn't. I don't regognize this $[ syntax at all, for any language I know.

If it doesn't work, please post what you tried and what happened.
# 10  
Old 11-02-2011
This is the body of the code i use (been updated with the '((' that you recomended)
Code:
ALARMHOUR=`echo "$ALARMTIME" | cut -d : -f 1 `
ALARMMINUTE=`echo "$ALARMTIME" | cut -d : -f 2 `
ALARMSECOND=`echo "$ALARMTIME" | cut -d : -f 3 `

for (( ;; )) do
  clear
  TIME=`date "+%H:%M:%S"`

read HOUR MINUTE SECOND <<<$(date +"%H %M %S")

  SECONDDIFF=$((SECOND-ALARMSECOND))
  SECONDDIFF=$((60-SECONDDIFF))

  if [ $SECONDDIFF -lt 10 ]; then
    SECONDDIFF="0$SECONDDIFF"
  fi

  MINUTEDIFF=$(($MINUTE-$ALARMMINUTE))
  MINUTEDIFF=$((59-MINUTEDIFF))

  if [ $MINUTEDIFF -lt 10 ]; then
    MINUTEDIFF ="0$MINUTEDIFF"
  fi

  HOURDIFF=$(($HOUR-$ALARMHOUR))
  if [ $HOURDIFF -lt 0 ]; then
    HOURDIFF=$((HOURDIFF-HOURDIFF-HOURDIFF))
  elif [ $HOURDIFF -gt 0 ]; then
    HOURDIFF=$((24-HOURDIFF))
  fi

  if [ $HOURDIFF -lt 10 ]; then
    HOURDIFF="0$HOURDIFF"
  fi

  echo "Current time : $HOUR:$MINUTE:$SECOND"
  echo ""
  echo "  Alarm time : $ALARMTIME"
  echo ""
  echo "  Difference : $HOURDIFF:$MINUTEDIFF:$SECONDDIFF"
  ...
  sleep 1
done

It cycles through that, updating every second, but when SECONDS reaches 08, it exits with the following error :
/bin/TermTime (unstable): line 33: 08: value too great for base (error token is "08")

I have tried setting the SECONDS variable in the form :

if [ $SECONDS = 8 ]; then
SECONDS=8
fi

Didn't expect it to work and it didn't, but i thought i would try it
The strange thing is that i don't think it does this with MINUTES or HOURS, just SECONDS...

What do you suggest?
# 11  
Old 11-02-2011
Ah -- BASH assumes numbers beginning with 0 are octal.

Do
Code:
STRING="${STRING#0}"

to strip off the leading zero when present. Do this for hours, minutes, and seconds.

Also:
Code:
IFS=":" read ALARMHOUR ALARMMINUTE ALARMSECOND <<<"$ALARMTIME"

will be hundreds of times faster than running cut three times to process one string...

---------- Post updated at 01:34 PM ---------- Previous update was at 01:30 PM ----------

Also: Your TIME variable doesn't appear to do anything anymore.
This User Gave Thanks to Corona688 For This Post:
# 12  
Old 11-02-2011
Thank you, just waiting for it to come round and...It works. Thank you so much, this has been bugging me since last night sometime...
My final question is : How can i stop it from saying "Current time : 19:44:1"
and make it say "Current time : 19:44:01" without screwing it all up?
Never mind, Thank you so much Corona, you've been a great help
# 13  
Old 11-02-2011
Code:
printf "Current time:  %02d:%02d:%02d\n" $HH $MM $SS

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