Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

OT: A question about bash scripting

30 views
Skip to first unread message

Ralf Mardorf

unread,
Oct 29, 2012, 7:10:02 AM10/29/12
to
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


--
To UNSUBSCRIBE, email to debian-us...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listm...@lists.debian.org
Archive: http://lists.debian.org/1351508274.1...@localhost.localdomain

Wolf Halton

unread,
Oct 29, 2012, 3:10:02 PM10/29/12
to
On Mon, Oct 29, 2012 at 6:57 AM, Ralf Mardorf
<ralf.m...@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 Apt Has Super Cow Powers - http://sourcefreedom.com
Open-Source Software in Libraries - http://FOSS4Lib.org
Advancing Libraries Together - http://LYRASIS.org
Apache Open Office Developer wolfh...@apache.org


--
To UNSUBSCRIBE, email to debian-us...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listm...@lists.debian.org
Archive: http://lists.debian.org/CALRLYE=_L+bX-unW-ki4Z8AJqJse...@mail.gmail.com

Ralf Mardorf

unread,
Oct 29, 2012, 3:30:02 PM10/29/12
to
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.

Regards,
Ralf

PS: In German:
Das "${seconds: -2}" formatiert den String und statt "seconds" soll dort
((done-started)-(((done-started)/60)*60)+100)) eingefügt werden. Doch
wie?







--
To UNSUBSCRIBE, email to debian-us...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listm...@lists.debian.org
Archive: http://lists.debian.org/1351538780.1...@localhost.localdomain

Neal Murphy

unread,
Oct 29, 2012, 4:00:02 PM10/29/12
to

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

 

 

Ralf Mardorf

unread,
Oct 29, 2012, 4:40:02 PM10/29/12
to
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


--
To UNSUBSCRIBE, email to debian-us...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listm...@lists.debian.org
Archive: http://lists.debian.org/1351542663.1...@localhost.localdomain

Ralf Mardorf

unread,
Oct 29, 2012, 4:40:02 PM10/29/12
to

> ... WHILE I WONT THIS 2 lines, AS ONE LINE, INCLUDING THE FORMATTING:
Oops, an evil typo ;), it should be "... while I want". The capital
letters aren't for shouting, just to distinguish the mail's text from
the script.


--
To UNSUBSCRIBE, email to debian-us...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listm...@lists.debian.org
Archive: http://lists.debian.org/1351543007.1...@localhost.localdomain

Dom

unread,
Oct 29, 2012, 5:40:04 PM10/29/12
to
On 29/10/12 20:31, Ralf Mardorf wrote:
(trimmed)
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


--
To UNSUBSCRIBE, email to debian-us...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listm...@lists.debian.org
Archive: http://lists.debian.org/508EF5CD...@rpdom.net

Neal Murphy

unread,
Oct 29, 2012, 7:00:02 PM10/29/12
to

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.

Ralf Mardorf

unread,
Oct 29, 2012, 7:30:01 PM10/29/12
to
On Mon, 2012-10-29 at 21:31 +0000, Dom wrote:
> 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

Thank you :)

that's it.

started=$(date +%s)
SECONDS=0
sleep 72

### Time
month=$(date +%B)
mon=$(date +%b)
d_y_t=$(date '+/%d/%Y %T')
done=$(date +%s)
done_2=$SECONDS

((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

((min=(done_2/60))) ; ((sec=(done_2%60)))
echo
printf "Attended time to restore session: %9d:%02d\n" $min $sec
printf "Session restored at %9.9s%s\n" "$month" "$d_y_t"
echo

### Log file
grep "Write to log file enabled." $log_file > /dev/null
if [ $? -eq 0 ] ; then
echo " Restore: $min_sec $mon$d_y_t" >> $log_file
printf " Restore: %s:%02d %s%s\n" "$min" $sec "$mon" "$d_y_t" >> $log_file
fi

Regards,
Ralf


--
To UNSUBSCRIBE, email to debian-us...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listm...@lists.debian.org
Archive: http://lists.debian.org/1351552967.1...@localhost.localdomain

Ralf Mardorf

unread,
Oct 29, 2012, 7:50:02 PM10/29/12
to
On Mon, 2012-10-29 at 18:52 -0400, Neal Murphy wrote:
> What's the '+100' supposed to do?

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

### Time
month=$(date +%B)
mon=$(date +%b)
d_y_t=$(date '+/%d/%Y %T')
done=$(date +%s)
done_2=$SECONDS

((seconds=(done-started)-(((done-started)/60)*60)))
seconds="0$seconds" ### THIS EXPLAINS AND REPLACES THE +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 IS WHAT I LIKE THE BEST
((min=(done_2/60))) ; ((sec=(done_2%60)))
echo
printf "Attended time to restore session: %9d:%02d\n" $min $sec
printf "Session restored at %9.9s%s\n" "$month" "$d_y_t"
echo

http://lists.debian.org/debian-user/2012/10/msg01286.html

Regards,
Ralf


--
To UNSUBSCRIBE, email to debian-us...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listm...@lists.debian.org
Archive: http://lists.debian.org/1351554170.1...@localhost.localdomain

Chris Davies

unread,
Oct 30, 2012, 4:30:02 AM10/30/12
to
Ralf Mardorf <ralf.m...@alice-dsl.net> wrote:
> month=$(date +%B)
> mon=$(date +%b)
> d_y_t=$(date '+/%d/%Y %T')
> done=$(date +%s)

You've got a horrible race condition in there just waiting to bite
you. Try this instead:

done=$(date +%s)
month=$(date --date @$done +%B)
mon=$(date --date @$done +%b)
d_y_t=$(date --date @$done +'/%d/%Y %T')

Chris


--
To UNSUBSCRIBE, email to debian-us...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listm...@lists.debian.org
Archive: http://lists.debian.org/7ud4m9x...@news.roaima.co.uk

Ralf Mardorf

unread,
Oct 30, 2012, 9:20:02 AM10/30/12
to
On Tue, 30 Oct 2012 08:58:31 +0100, Chris Davies
<chris-...@roaima.co.uk> wrote:

> done=$(date +%s)
> month=$(date --date @$done +%B)
> mon=$(date --date @$done +%b)
> d_y_t=$(date --date @$done +'/%d/%Y %T')

I agree, good idea.


--
To UNSUBSCRIBE, email to debian-us...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listm...@lists.debian.org
Archive: http://lists.debian.org/op.wmzth9s8qhadp0@suse11-2
0 new messages