Mayki
Posts: 13
Joined: Thu Jun 21, 2018 5:28 am

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: 12817
Joined: Sun May 06, 2012 5:17 am
Location: Ankh-Morpork

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: 13
Joined: Thu Jun 21, 2018 5:28 am

Re: Bash script for read .txt

Sun Sep 09, 2018 8:23 am

Sorry, correct name is led.txt

Mayki
Posts: 13
Joined: Thu Jun 21, 2018 5:28 am

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: 33857
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Bash script for read .txt

Sun Sep 09, 2018 9:04 am

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 $
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

Mayki
Posts: 13
Joined: Thu Jun 21, 2018 5:28 am

Re: Bash script for read .txt

Sun Sep 09, 2018 9:16 am

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 $

User avatar
PeterO
Posts: 4246
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),Aeromodelling,1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Mayki
Posts: 13
Joined: Thu Jun 21, 2018 5:28 am

Re: Bash script for read .txt

Sun Sep 09, 2018 9:30 am

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

User avatar
DougieLawson
Posts: 33857
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Bash script for read .txt

Sun Sep 09, 2018 9:32 am

BTW why does your script use sudo, it's not needed.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

Mayki
Posts: 13
Joined: Thu Jun 21, 2018 5:28 am

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: 33857
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

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.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

Mayki
Posts: 13
Joined: Thu Jun 21, 2018 5:28 am

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”

Who is online

Users browsing this forum: Baidu [Spider] and 6 guests