You are not logged in.

#1 2011-04-15 07:17:36

Markus00000
Member
Registered: 2011-03-27
Posts: 318

[SOLVED] Bash script: Line breaks in strings

I am using this script to receive an alarm with some text in x minutes:

#!/bin/sh
text="$2\n\n`date '+%T'`"
(sleep $[$1*60] && (zenity --info --text=$text &) && (aplay /home/m/alarm.wav &)) > /dev/null 2>&1 & disown

In the Zenity window \n is correctly converted to a line break. Great.

Then I have this similar script where \n becomes "n" without a line break:

#!/bin/sh
text="$2\n\n`date '+%T'`"
echo "DISPLAY=:0 zenity --info --text=$text" | at $1 > dev/null 2>&1 
echo "aplay /home/m/alarm.wav" | at $1 > /dev/null 2>&1 

Invoking the script with the arguments "now" and "hello!" will result in "hello!nn09:16:17".

How can I make it honor \n in this case?

Last edited by Markus00000 (2011-04-20 07:05:53)

Offline

#2 2011-04-15 08:57:11

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,224
Website

Re: [SOLVED] Bash script: Line breaks in strings

Markus00000 wrote:
echo "DISPLAY=:0 zenity --info --text=$text" | at $1 > dev/null 2>&1 

You need to pass the quotes to 'at' as well:

#!/bin/sh
text="$2\n\n`date '+%T'`"
echo "DISPLAY=:0 zenity --info --text=\"$text\"" | at $1 > dev/null 2>&1 
echo "aplay /home/m/alarm.wav" | at $1 > /dev/null 2>&1 

A better way to do this (IMHO) is:

#!/bin/sh
text="$2\n\n`date '+%T'`"
at $1 > dev/null 2>&1 <<EOT
aplay /home/m/alarm.wav &
DISPLAY=:0 zenity --info --text="$text"
EOT

Last edited by fukawi2 (2011-04-15 08:58:35)

Offline

#3 2011-04-20 07:05:33

Markus00000
Member
Registered: 2011-03-27
Posts: 318

Re: [SOLVED] Bash script: Line breaks in strings

Thanks a lot! Really appreciated.

Note: The notification window will unfortunately display the time when the alarm was scheduled, not the time when the alarm went off. smile

Last edited by Markus00000 (2011-04-20 07:20:19)

Offline

#4 2011-04-20 08:39:32

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,224
Website

Re: [SOLVED] Bash script: Line breaks in strings

This will show the timestamp of when the alarm activates, rather than when it was set smile

#!/bin/sh
text="$2"
at $1 > dev/null 2>&1 <<EOT
aplay /home/m/alarm.wav &
DISPLAY=:0 zenity --info --text="$text\n\n$(date '+%T')"
EOT

Offline

#5 2011-04-20 09:58:35

Markus00000
Member
Registered: 2011-03-27
Posts: 318

Re: [SOLVED] Bash script: Line breaks in strings

Excellent, thanks for saving me time and educating me. smile

Offline

Board footer

Powered by FluxBB