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

Re: OT: A question about bash scripting



On Monday, October 29, 2012 04:31:03 PM Ralf Mardorf wrote:

 

> 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

 

What's the '+100' supposed to do? Add 100 to the remaining seconds? Or subtract 100 from it? (That is, increase or decrease the number of seconds?) The way it is now, the number of seconds will never be less than 100 and your ': -2' tweak will never trigger anyway.

 

What you are asking cannot be done. You cannot nest substitutions in the manner you wish, and getting the leading zero on the seconds is problematic using only bash.

 

I don't think you can put that many conditionals on a single line and have it remain comprehendable.

 

However, you might be able to do it using awk:

 

### 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)

min_sec=$(((done-started)/60))":"$(echo $done $started | awk '{s=($1-$2)%60; if (s==0) {s=2;} printf ("%2.2d", s);}')

echo

echo "Attended time to restore session: $min_sec"

echo -n "Session restored at " ; printf %9.9s $month ; echo $d_y_t

echo

 

But regardless, the line is going to be rather long. Unless you use a shell function:

 

getSeconds () {

echo $done $started | \

awk '{

s=($1-$2)%60;

if (s==0) {s=2;}

printf ("%2.2d", s);

}'

 

 

Then use:

min_sec=$(((done-started)/60))":"$(getSeconds)

 

But I still don't see what the '+100" is supposed to do.


Reply to: