[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: OT: A question about bash scripting



On 29/10/12 20:31, Ralf Mardorf wrote:
(trimmed)

I want

  ((seconds=(done-started)-(((done-started)/60)*60)+100))
  min_sec=$(((done-started)/60))":"${seconds: -2}

in one line, instead of two lines.

I don't understand your reply.

Even if I would add ${min_sec: 2} to each "echo" command (there will be
a second output to a log file, it wouldn't be formatted as needed.

FOR YOUR EXAMPLE, IIUC IT SHOULD BE? ...

  ### Killall and Restore session
  started=$(date +%s)
  sleep 2

  ### Time
  month=$(date +%B)
  mon=$(date +%b)
  d_y_t=$(date '+/%d/%Y %T')
  done=$(date +%s)
  #((seconds=(done-started)-(((done-started)/60)*60)+100))
  #min_sec=$(((done-started)/60))":"${seconds: -2}
  min_sec=$(((done-started)/60))":"$(((done-started)-(((done-started)/60)*60)+100))
  echo
  echo    "Attended time to restore session:         $min_sec"
  echo -n "Session restored at " ; printf %9.9s $month ; echo $d_y_t
  echo

... RESULT ...

Attended time to restore session:         0:102
Session restored at   October/29/2012 21:11:43

... RESP. ...

  ### Killall and Restore session
  started=$(date +%s)
  sleep 2

  ### Time
  month=$(date +%B)
  mon=$(date +%b)
  d_y_t=$(date '+/%d/%Y %T')
  done=$(date +%s)
  #((seconds=(done-started)-(((done-started)/60)*60)+100))
  #min_sec=$(((done-started)/60))":"${seconds: -2}
  min_sec=$(((done-started)/60))":"$(((done-started)-(((done-started)/60)*60)+100))
  min_sec=${min_sec: 2}
  echo
  echo    "Attended time to restore session:         $min_sec"
  echo -n "Session restored at " ; printf %9.9s $month ; echo $d_y_t
  echo

... RESULT ...

Attended time to restore session:         102
Session restored at   October/29/2012 21:17:26

BUT I NEED ...

  ### Killall and Restore session
  started=$(date +%s)
  sleep 2

  ### Time
  month=$(date +%B)
  mon=$(date +%b)
  d_y_t=$(date '+/%d/%Y %T')
  done=$(date +%s)
  ((seconds=(done-started)-(((done-started)/60)*60)+100))
  min_sec=$(((done-started)/60))":"${seconds: -2}
  echo
  echo    "Attended time to restore session:         $min_sec"
  echo -n "Session restored at " ; printf %9.9s $month ; echo $d_y_t
  echo

... THIS RESULT ...

Attended time to restore session:         0:02
Session restored at   October/29/2012 21:21:32

... WHILE I WONT THIS 2 lines, AS ONE LINE, INCLUDING THE FORMATTING:

  ((seconds=(done-started)-(((done-started)/60)*60)+100))
  min_sec=$(((done-started)/60))":"${seconds: -2}


Would this do what you are after?

### Killall and Restore session
started=$(date +%s)
sleep 2

### Time
month=$(date +%B)
mon=$(date +%b)
d_y_t=$(date '+/%d/%Y %T')
done=$(date +%s)
echo
printf "Attended time to restore session: %4d:%02d\n" $(((done-started)/60)) $(((done-started)%60))
printf "Session restored at %9.9s%s\n" "$month" "$d_y_t"
echo

(The first "printf" is a long line that will probably get spilt by email. It should all be on one line).

Also the SECONDS shell counter variable is useful for this sort of thing.

For example:
### Killall and Restore session
SECONDS=0

...

done=$SECONDS

--
Dom


Reply to: