bash / put a text file content in a variable

Having issues with your DietPi installation, or, found a bug? Post it here.
Post Reply
pakikje
Posts: 124
Joined: Fri Nov 11, 2016 3:04 pm

bash / put a text file content in a variable

Post by pakikje »

Hello guys,

I have a txt-file that has a (changing) number as content in it. ex: "12345"

I need to put this (changing) number in a variable. This number is filtered out of a html-page (with a sed-command).

Who can give me a tip?



(this is the script)


#!/bin/bash

wget nos.nl/uitzending/nos-journaal
grep 'data-id=.*\"' nos-journaal > dataid.txt #get the data-id number
sed -i 's/data-id=\"//g' dataid.txt #clean up the number, remove quotes and spaces and junk
sed -i 's/\"//g' dataid.txt #
sed -i 's/ //g' dataid.txt #

#put the content of dataid.txt in a variable!
#replace year/month/day with variables!
#wget https://download.omroep.nl/nos/content/ ... _web03.mp4

At the end i want to wget a mp4-file. I want to put the "number" in a variable ($dataid) and in the download path.
User avatar
Fourdee
Site Admin
Posts: 2788
Joined: Tue Feb 06, 2007 1:36 pm

Re: bash / put a text file content in a variable

Post by Fourdee »

pakikje wrote:#!/bin/bash

wget nos.nl/uitzending/nos-journaal
grep 'data-id=.*\"' nos-journaal > dataid.txt #get the data-id number
sed -i 's/data-id=\"//g' dataid.txt #clean up the number, remove quotes and spaces and junk
sed -i 's/\"//g' dataid.txt #
sed -i 's/ //g' dataid.txt #

At the end i want to wget a mp4-file. I want to put the "number" in a variable ($dataid) and in the download path.
This will read dataid.txt to a variable MY_VAR:

Code: Select all

#!/bin/bash

wget nos.nl/uitzending/nos-journaal
grep 'data-id=.*\"' nos-journaal > dataid.txt #get the data-id number
sed -i 's/data-id=\"//g' dataid.txt #clean up the number, remove quotes and spaces and junk
sed -i 's/\"//g' dataid.txt
sed -i 's/ //g' dataid.txt

MY_VAR=$(cat dataid.txt) # Load into variable

echo $MY_VAR # Print variable to screen
If you find our project or support useful, then we’d really appreciate it if you’d consider contributing to the project however you can.
Donating is the easiest – you can use PayPal or become a DietPi patron.
pakikje
Posts: 124
Joined: Fri Nov 11, 2016 3:04 pm

Re: bash / put a text file content in a variable

Post by pakikje »

THNX!! Works...

My script:

#!/bin/bash
rm *txt
rm nos*
wget nos.nl/uitzending/nos-journaal
grep 'data-id=.*\"' nos-journaal > dataid.txt
sed -i 's/data-id=\"//g' dataid.txt
sed -i 's/\"//g' dataid.txt
sed -i 's/ //g' dataid.txt
MY_VAR=$(cat dataid.txt) # Load into variable
MY_YEAR=$(date +'%Y')
MY_MONTH=$(date +'%m')
MY_DAY=$(date +'%d')
clear
wget https://download.omroep.nl/nos/content/ ... MY_DAY/$MY_$
wget https://download.omroep.nl/nos/content/ ... MY_DAY/$MY_$
wget https://download.omroep.nl/nos/content/ ... MY_DAY/$MY_$
mv mp4_web01.mp4 nos.mp4
mv mp4_web02.mp4 nos.mp4
mv mp4_web03.mp4 nos.mp4
rm nos-journaal
rm *txt
mv nos.mp4 /mnt/dietpi_userdata/Video/nos.avi
service minidlna restart

ps: i have to add a logic in the script that will check id web03 is present (and then wget it) or web02 or web01 (these are the same video's but then in decreasing sharpness-order)
Post Reply