Page 1 of 1

Bash script for read .txt

Posted: Sun Sep 09, 2018 7:50 am
by Mayki
Hi, I need to advise creating a bash script to read a .txt file. I need to extract the text and use it as a parameter to the command.

Code: Select all

#!/bin/bash

text="$(read "/home/pi/rpi-fruitbox-master/skins/Cabinet/led.txt" -print -quit)"

sudo	/home/pi/rpi-rgb-led-matrix/examples-api-use/scrolling-text-example -f /home/pi/rpi-rgb-led-matrix/fonts/10x20.bdf -s 3 -C 0,0,255 -l 1 -y 5 --led-rows=32 --led-cols=64 --led-chain=3 "$text"
This does not want me to work, can you advise me? Thank you for your help.

Re: Bash script for read .txt

Posted: Sun Sep 09, 2018 8:20 am
by rpdom
Try this

Code: Select all

#!/bin/bash

text="$(cat /home/pi/rpi-fruitbox-master/skins/Cabinet/led.txt.)"

sudo	/home/pi/rpi-rgb-led-matrix/examples-api-use/scrolling-text-example -f /home/pi/rpi-rgb-led-matrix/fonts/10x20.bdf -s 3 -C 0,0,255 -l 1 -y 5 --led-rows=32 --led-cols=64 --led-chain=3 "$text"
Does your filename really end with a dot? "led.txt." ?

Re: Bash script for read .txt

Posted: Sun Sep 09, 2018 8:23 am
by Mayki
Sorry, correct name is led.txt

Re: Bash script for read .txt

Posted: Sun Sep 09, 2018 8:48 am
by Mayki
Thanks for your help, it works perfectly. I only have a non-standard character at the end of the sentence. Is there something to do with it?
Image

The content of the led.txt file is:

Code: Select all

Hello World

Re: Bash script for read .txt

Posted: Sun Sep 09, 2018 9:04 am
by DougieLawson
What's the hexadecimal content of your file?

Code: Select all

[email protected]:/tmp $ echo "Hello World" >> led.txt
[email protected]:/tmp $ cat led.txt
Hello World
[email protected]:/tmp $ od -A x -t x1z -v led.txt
000000 48 65 6c 6c 6f 20 57 6f 72 6c 64 0a              >Hello World.<
00000c
[email protected]:/tmp $

Re: Bash script for read .txt

Posted: Sun Sep 09, 2018 9:16 am
by Mayki

Code: Select all

[email protected]:~/rpi-fruitbox-master/skins/Cabinet $ cat led.txt
Hello World
[email protected]:~/rpi-fruitbox-master/skins/Cabinet $ od -A x -t x1z -v led.txt
000000 48 65 6c 6c 6f 20 57 6f 72 6c 64 0a              >Hello World.<
00000c
[email protected]:~/rpi-fruitbox-master/skins/Cabinet $

Re: Bash script for read .txt

Posted: Sun Sep 09, 2018 9:19 am
by PeterO
Try

Code: Select all

echo -n "Hello World" > led.txt
The "-n" will remove the new line character from the end of the string.

PeterO

Re: Bash script for read .txt

Posted: Sun Sep 09, 2018 9:30 am
by Mayki
Problem solved.... :D

It was this:

Code: Select all

[email protected]:~ $ . ledmatrix.sh
-bash: $'\r': command not found
-bash: $'\r': command not found
CTRL-C for exit.
[email protected]:~ $
Thank you for your help... 8-)

Re: Bash script for read .txt

Posted: Sun Sep 09, 2018 9:32 am
by DougieLawson
BTW why does your script use sudo, it's not needed.

Re: Bash script for read .txt

Posted: Sun Sep 09, 2018 10:37 am
by Mayki
Sudo is needed to access the GPIO.

I still need help, I would like to run the script ledmatrix.sh every 20 seconds only if a program (Fruitbox) is running. How to do it?

Re: Bash script for read .txt

Posted: Sun Sep 09, 2018 10:46 am
by DougieLawson
Mayki wrote:
Sun Sep 09, 2018 10:37 am
Sudo is needed to access the GPIO.
No it's not. If your userid is in the gpio group you get access as an unprivileged user. Running as sudo always opens a security hole.

Re: Bash script for read .txt

Posted: Sun Sep 09, 2018 12:17 pm
by Mayki
Mayki wrote:
Sun Sep 09, 2018 10:37 am
I still need help, I would like to run the script ledmatrix.sh every 20 seconds only if a program (Fruitbox) is running. How to do it?
Please help...