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

Re: OT: A question about bash scripting



On Mon, 2012-10-29 at 15:59 -0400, Neal Murphy wrote:
> On Monday, October 29, 2012 03:26:20 PM Ralf Mardorf wrote:
> 
> > On Mon, 2012-10-29 at 15:00 -0400, Wolf Halton wrote:
> 
> > > On Mon, Oct 29, 2012 at 6:57 AM, Ralf Mardorf
> 
> > > 
> 
> > > <ralf.mardorf@alice-dsl.net> wrote:
> 
> > > > Hi :)
> 
> > > > 
> 
> > > > how can I get rid of the variable "seconds"?
> 
> > > > 
> 
> > > > ((seconds=(done-started)-(((done-started)/60)*60)+100))
> 
> > > > min_sec=$(((done-started)/60))":"${seconds: -2}
> 
> > > > 
> 
> > > > ^^^^^^^ the math should replace the
> 
> > > > 
> 
> > > > variable.
> 
> > > > 
> 
> > > > ### Killall and Restore session
> 
> > > > 
> 
> > > > started=$(date +%s)
> 
> > > > bash "$song_path/session/start-session-$startversion"
> 
> > > > 
> 
> > > > ### 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
> 
> > > > 
> 
> > > > TIA,
> 
> > > > Ralf
> 
> > > 
> 
> > > Ralf,
> 
> > > I don't understand.
> 
> > > What are you wanting to do with the script?
> 
> > > If you don't like the name of the variable, make up another one.
> 
> > > 
> 
> > > Wolf
> 
> > 
> 
> > This part of a larger script is a stopwatch and I want the math
> 
> > ((done-started)-(((done-started)/60)*60)+100)) inside the formatted
> 
> > string ${seconds: -2} , instead of a variable.
> 
> > 
> 
> > Something similar to
> 
> > ${((done-started)-(((done-started)/60)*60)+100)): -2}
> 
> > 
> 
> > Similar, because I'm missing something.
> 
>  
> 
> You missed the $((...)) syntax as exemplified by the
> '(done-started)/60' just before it:
> 
> min_sec=$(((done-started)/60))":"
> 
> min_sec=${min_sec}$(((done-started)-(((done-started)/60)*60)+100))

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}

Regards,
Ralf


Reply to: