Mayki
Posts: 27
Joined: Thu Jun 21, 2018 5:28 am
Location: Czech Republic

Bash script for read .txt

Sun Sep 09, 2018 7:50 am

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.
Last edited by Mayki on Sun Sep 09, 2018 8:23 am, edited 1 time in total.

User avatar
rpdom
Posts: 17546
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Bash script for read .txt

Sun Sep 09, 2018 8:20 am

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." ?

Mayki
Posts: 27
Joined: Thu Jun 21, 2018 5:28 am
Location: Czech Republic

Re: Bash script for read .txt

Sun Sep 09, 2018 8:23 am

Sorry, correct name is led.txt

Mayki
Posts: 27
Joined: Thu Jun 21, 2018 5:28 am
Location: Czech Republic

Re: Bash script for read .txt

Sun Sep 09, 2018 8:48 am

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

User avatar
DougieLawson
Posts: 39788
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Bash script for read .txt

Sun Sep 09, 2018 9:04 am

What's the hexadecimal content of your file?

Code: Select all

pi@mercury:/tmp $ echo "Hello World" >> led.txt
pi@mercury:/tmp $ cat led.txt
Hello World
pi@mercury:/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
pi@mercury:/tmp $
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

Mayki
Posts: 27
Joined: Thu Jun 21, 2018 5:28 am
Location: Czech Republic

Re: Bash script for read .txt

Sun Sep 09, 2018 9:16 am

Code: Select all

pi@retropie:~/rpi-fruitbox-master/skins/Cabinet $ cat led.txt
Hello World
pi@retropie:~/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
pi@retropie:~/rpi-fruitbox-master/skins/Cabinet $

User avatar
PeterO
Posts: 5958
Joined: Sun Jul 22, 2012 4:14 pm

Re: Bash script for read .txt

Sun Sep 09, 2018 9:19 am

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
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Mayki
Posts: 27
Joined: Thu Jun 21, 2018 5:28 am
Location: Czech Republic

Re: Bash script for read .txt

Sun Sep 09, 2018 9:30 am

Problem solved.... :D

It was this:

Code: Select all

pi@retropie:~ $ . ledmatrix.sh
-bash: $'\r': command not found
-bash: $'\r': command not found
CTRL-C for exit.
pi@retropie:~ $
Thank you for your help... 8-)

User avatar
DougieLawson
Posts: 39788
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Bash script for read .txt

Sun Sep 09, 2018 9:32 am

BTW why does your script use sudo, it's not needed.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

Mayki
Posts: 27
Joined: Thu Jun 21, 2018 5:28 am
Location: Czech Republic

Re: Bash script for read .txt

Sun Sep 09, 2018 10:37 am

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?

User avatar
DougieLawson
Posts: 39788
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Bash script for read .txt

Sun Sep 09, 2018 10:46 am

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.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

Mayki
Posts: 27
Joined: Thu Jun 21, 2018 5:28 am
Location: Czech Republic

Re: Bash script for read .txt

Sun Sep 09, 2018 12:17 pm

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

Return to “Other programming languages”