LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 04-22-2010, 02:12 PM   #1
Dimitriy
Member
 
Registered: Oct 2005
Distribution: Ubuntu Dapper (6.06)
Posts: 92

Rep: Reputation: 15
Simple parse txt into variables, 4 option menu, select and execute?


[Solved] See last post for solution. THANK YOU grail!

Language: BASH
OS: FreeNAS 0.7.1 (or later)
Goal: Create a small script that could download a file based on text pulled from another file and give user options on what version.

There is a file that is on the web:

http://sabnzbdplus.sourceforge.net/version/latest
Structure looks like this (just simple basic text):
Code:
0.5.0
http://sourceforge.net/projects/sabnzbdplus/files/
0.5.2RC1
http://sourceforge.net/projects/sabnzbdplus/files/
The download links for each individual file look like this:
http://downloads.sourceforge.net/project/sabnzbdplus/sabnzbdplus/sabnzbd-0.5.0/SABnzbd-0.5.0-src.tar.gz
http://downloads.sourceforge.net/project/sabnzbdplus/sabnzbdplus-beta/0.5.2RC1/SABnzbd-0.5.2RC1-src.tar.gz

I am creating a script to install the package with the 4 options:
1) Upgrade/Install SABnzbd 0.5.0 (test good)
2) Upgrade/Install latest STABLE SABnzbd %variable1%
3) Upgrade/Install lastest BETA SABnzbd %variable2%
4) Exit

The script currently looks like this:
Code:
#!/bin/bash
echo "*******start*******"
>>>snipped code - just does some prep work
echo "*******3. Downloading upgraded version of SABnzbd (0.5.0) and extracting*******"
cd /root/
echo "--Downloading upgraded version of SABnzbd (0.5.0)--"
#Edit the line below to get a newer version and be sure to update the cosmetic lines above
fetch "http://downloads.sourceforge.net/project/sabnzbdplus/sabnzbdplus/sabnzbd-0.5.0/SABnzbd-0.5.0-src.tar.gz"
echo "--Extracting upgrade--"
tar -xzf SABnzbd*
echo "--Extracted upgrade--"
>>>snipped code
echo "*******Done*******"
Here is what I am trying to do:
1. Have the script fetch the text file from the web.
2. Pull the #s from line 1 and line 3
3. Assign #s from line 1 to a X variable and line 3 assign to Y variable.
4. Present the user with 4 options, with two of the options displaying the variables (among other static text).
5. User selects an option that go through the script downloading the version based on the variable and option selected.
6. If option 2 is selected use this url+variableX to downlaod. If option 2 is select use differenturl+variableY to download.

Any one willing to give me a hang with syntax? I have attached my current script and will be posting my progress in some more posts.

D
Attached Files
File Type: txt sab.txt (2.1 KB, 1 views)

Last edited by Dimitriy; 04-26-2010 at 11:03 PM.
 
Old 04-22-2010, 02:25 PM   #2
Dimitriy
Member
 
Registered: Oct 2005
Distribution: Ubuntu Dapper (6.06)
Posts: 92

Original Poster
Rep: Reputation: 15
Doing multiple posts in thread to help break up each concept/area of script.

I am thinking beginning of the script will look so:
Code:
#!/bin/bash
cd /root
fetch -o latest.txt  "http://sabnzbdplus.sourceforge.net/version/latest"
>>>>Area for code that pulls the text from line 1 and line 3.<<<<<<<<<
#Assign parsed line 1 and line 3 to variables
stable=>>>>>>line one value<<<<<
beta=>>>>line three value<<<<<<
#get rid of the latest.txt
rm -rf /root/latest.txt

>>>>Next is show options (see next post)<<<<
Any idea how to fill in those areas?

thanks, D

Last edited by Dimitriy; 04-22-2010 at 02:39 PM.
 
Old 04-22-2010, 02:38 PM   #3
Dimitriy
Member
 
Registered: Oct 2005
Distribution: Ubuntu Dapper (6.06)
Posts: 92

Original Poster
Rep: Reputation: 15
The next section would be presenting the options to look like this (to user):

1) Upgrade/Install SABnzbd 0.5.0 (tested good)
2) Upgrade/Install latest STABLE SABnzbd (echo $stable variable here)
3) Upgrade/Install lastest BETA SABnzbd (echo $beta variable here)
4) Exit

The script portion would look like so?
Code:
menu="\"1) Upgrade/Install SABnzbd 0.5.0 (tested good)\" \"2) Upgrade/Install latest STABLE SABnzbd echo $stable\" \"3) Upgrade/Install lastest BETA SABnzbd echo $beta\" \"Exit\""

echo menu $menu

#Not sure if the below should be here of in the middle of the script when its downloading the package
eval set $menu
select menuchoice in "$@"
do
echo $menuchoice
if [ "$opt"="Exit" ]; then
		exit
How does this code look?

Using this as a reference:
http://www.linuxquestions.org/questi...on-1-o-317671/thanks -D

Last edited by Dimitriy; 04-22-2010 at 04:38 PM.
 
Old 04-22-2010, 04:46 PM   #4
Dimitriy
Member
 
Registered: Oct 2005
Distribution: Ubuntu Dapper (6.06)
Posts: 92

Original Poster
Rep: Reputation: 15
The next final part is where the magic should happen:

At this point of the script the follow should have already happened:
1. Latest version #s of package (stable/beta) obtained and assigned to variables $stable and $beta.
2. A menu has been presented to the user with 4 options with options utilizing $stable and $beta variables.
3. User has selected option 1-3 (doesn't matter)

The script then goes to following:
Code:
echo "******* Doing bunch of prep work *******"
>> prep work code here - this spot is taken care of <<<<

# Being the area where it downloads the package
echo "*******Downloading upgraded version of SABnzbd*******"
if [ "$opt"="1) Upgrade/Install SABnzbd 0.5.0 (tested good)" ]; then
fetch "http://downloads.sourceforge.net/project/sabnzbdplus/sabnzbdplus/sabnzbd-0.5.0/SABnzbd-0.5.0-src.tar.gz" # this one stays hardcoded
if [ "$opt"="2) Upgrade/Install latest STABLE SABnzbd echo $stable" ]; then
fetch "http://downloads.sourceforge.net/project/sabnzbdplus/sabnzbdplus/sabnzbd-$stable/SABnzbd-$stable-src.tar.gz" # notice the stable variable in there?
if [ "$opt"="3) Upgrade/Install lastest BETA SABnzbd echo $beta" ]; then
fetch "http://downloads.sourceforge.net/project/sabnzbdplus/sabnzbdplus-beta/$beta/SABnzbd-$beta-src.tar.gz" #notice the beta variable in there?
# Is there anything that needs to be done to continue this script after it downloads??

>>> script continues to extract and install package <<<<<
echo "*******Done - SABnzbd should be installed*******"
fi
done
How does this look? I know its not pretty.

Any help appreciated.
Thanks-D
 
Old 04-22-2010, 09:15 PM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,193

Rep: Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785
Am I correct that you are the one hosting the text file on the site? If so, why not remove the redundant addresses, as first they are the same
and second, you already have the full path in your fetch.
Either way, this should get the info but you will need to pick what you want awk to look for:

Code:
input=($(wget -q -O - http://sabnzbdplus.sourceforge.net/version/latest | dos2unix | awk '/0/'))
input is now an array to be used as you wish.
 
Old 04-23-2010, 04:42 PM   #6
Dimitriy
Member
 
Registered: Oct 2005
Distribution: Ubuntu Dapper (6.06)
Posts: 92

Original Poster
Rep: Reputation: 15
Hi Grail,

Thanks for the reply.

I actually do not host the txt file. It is maintain by the package developers (their package check against that file for latest version).

So it would look like this (assigning line 1 value to variable stable and line 3 value to beta)?
From POST 2 (modified):
Code:
#!/bin/bash
stable=($(fetch -q -o - http://sabnzbdplus.sourceforge.net/version/latest | dos2unix | awk 'NR==1{print;exit}'))
beta=($(fetch -q -o - http://sabnzbdplus.sourceforge.net/version/latest | dos2unix | awk 'NR==3{print;exit}'))
FreeNAS doesnt have wget but uses fetch. Syntax for fetch:
Quote:
-q Quiet mode.
-o file Set the output file name to file. By default, a ``pathname'' is extracted from the specified URI, and its basename is used as the name of the output file. A file argument of `-' indicates that results are to be directed to the standard output.
thank you,
d
 
Old 04-23-2010, 11:03 PM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,193

Rep: Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785
I would just add that unless the "latest" file end up with thousands of lines, unlikely due to what it represents, the exit is not really required in awk.

If you are able to progress with the rest of your script please mark as SOLVED.
If not, let us know which part you might be stuck on?
 
Old 04-26-2010, 05:28 PM   #8
Dimitriy
Member
 
Registered: Oct 2005
Distribution: Ubuntu Dapper (6.06)
Posts: 92

Original Poster
Rep: Reputation: 15
Hi Grail,

Thank you for your reply.

FreeNAS doesnt have dos2unix however I was able to remove it from the command it seems to be outputting nicely:

This code:
Code:
#!/bin/bash

#Define the architeture of the box
arch=`uname -m`
echo "*******Archeticture of Machine is" $arch "*******"

#Pull latest version number and assign to variable
stable=($(fetch -q -o - http://sabnzbdplus.sourceforge.net/version/latest | awk 'NR==1{print;exit}'))
beta=($(fetch -q -o - http://sabnzbdplus.sourceforge.net/version/latest | awk 'NR==3{print;exit}'))
echo "******* Latest SABnzbd Stable version is:" $stable "*******"
echo "******* Latest SABnzbd Beta version is:" $beta "*******"
Gives out this:
Code:
freenas:~# ./test.sh
*******Archeticture of Machine is i386 *******
 *******Latest SABnzbd Stable version is: 0.5.0
 *******Latest SABnzbd Beta version is: 0.5.2RC1
I dont understand why the echo seems to skip ahead on the $stable and $beta lines (they are identical comments except for different variable).

I am having trouble getting to work with fetch:
Code:
#!/bin/bash

#Pull latest version number and assign to variable
stable=($(fetch -q -o - http://sabnzbdplus.sourceforge.net/version/latest | awk 'NR==1{print;exit}'))
beta=($(fetch -q -o - http://sabnzbdplus.sourceforge.net/version/latest | awk 'NR==3{print;exit}'))
echo "******* Latest SABnzbd Stable version is:" $stable "*******"
echo "******* Latest SABnzbd Beta version is:" $beta "*******"

echo "Downloading latest STABLE SABnzbd $stable"
fetch "http://downloads.sourceforge.net/project/sabnzbdplus/sabnzbdplus/sabnzbd-$stable.tar.gz"
It keeps on saying it cannot locate the file. Any idea? I tried just the $stable without the extra quotes as well.

Is there some garbage data being included in the variable?

thank you,
D

Last edited by Dimitriy; 04-26-2010 at 05:46 PM.
 
Old 04-26-2010, 06:08 PM   #9
Dimitriy
Member
 
Registered: Oct 2005
Distribution: Ubuntu Dapper (6.06)
Posts: 92

Original Poster
Rep: Reputation: 15
Is it possible that the missing dos2unix is what is killing it?

d
 
Old 04-26-2010, 08:46 PM   #10
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,193

Rep: Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785
Yes part of your issue is the dos2unix not being present.
You will notice in your initial output that not only is the echo stepped forward by one character but your asterixes (*)
are missing from the end of each line with $stable and $beta. I believe this is because the latest file is read in as a dos
file and has some funky garbage on the end

A few points on what you have written so far:

1. As before, awk does not require the "exit" command
2. In bash you can place variables inside the quotes:
Quote:
echo "******* Latest SABnzbd Stable version is:" $stable "*******"

#can be

echo "******* Latest SABnzbd Stable version is: $stable *******"
When using a variable inside a concatenated string you need to isolate the actual variable as bash will use all consecutive characters as variable name:
Quote:
fetch "http://downloads.sourceforge.net/project/sabnzbdplus/sabnzbdplus/sabnzbd-$stable.tar.gz"

#should be

fetch "http://downloads.sourceforge.net/project/sabnzbdplus/sabnzbdplus/sabnzbd-${stable}.tar.gz"
Note: I found two alternatives for dos2unix:
1. program called flip apparently does the same type of thing, not sure if this is on freeNAS
2. Add the following to end of awk statement - tr -d '\015' or you can change the awk to - awk '/0/{gsub(/\015/,"");print}'
 
1 members found this post helpful.
Old 04-26-2010, 10:59 PM   #11
Dimitriy
Member
 
Registered: Oct 2005
Distribution: Ubuntu Dapper (6.06)
Posts: 92

Original Poster
Rep: Reputation: 15
Thank you grail.

Everything seems to be taken care of. There may be a few bugs but those should be easier to chase down. Here is the completed product:
Code:
#!/bin/bash

#Define the architeture of the box
arch=`uname -m`
echo "      *******Archeticture of Machine is" $arch "*******"

echo
#Pull latest version number and assign to variable and then display it
stable=($(fetch -q -o - http://sabnzbdplus.sourceforge.net/version/latest | awk '/0/{gsub(/\015/,"");print}'))
beta=($(fetch -q -o - http://sabnzbdplus.sourceforge.net/version/latest | awk '/2/{gsub(/\015/,"");print}'))
echo "         Latest SABnzbd Stable version is: $stable"
echo "         Latest SABnzbd Beta version is: $beta"

#Menu
echo ""
echo "          Which Version do you wish to Install?"
echo "          ----- ------- -- --- ---- -- --------"
echo
echo "[1] Upgrade/Install SABnzbd 0.5.0 (tested good)"
echo "[2] Upgrade/Install latest STABLE SABnzbd $stable"
echo "[3] Upgrade/Install lastest BETA SABnzbd $beta"
echo "[4] Exit"
echo

#Reads the input user puts in assigns to "option"
read option

#Acts upon what user entered
case "$option" in
  "1")
echo 
echo "*******Selected Option 1: SABnzbd 0.5.0 (tested good)*******"
echo
echo "Downloading SABnzbd 0.5.0"
fetch "http://downloads.sourceforge.net/project/sabnzbdplus/sabnzbdplus/sabnzbd-0.5.0/SABnzbd-0.5.0-src.tar.gz"
;;

  "2")
echo 
echo "*******Selected Option 2: Latest STABLE SABnzbd $stable*******"
echo
echo "Downloading latest STABLE SABnzbd $stable"
fetch http://downloads.sourceforge.net/project/sabnzbdplus/sabnzbdplus/sabnzbd-${stable}/SABnzbd-${stable}-src.tar.gz
;;
  
  "3")
echo 
echo "*******Selected Option 2: Latest BETA SABnzbd $beta*******"
echo
echo "Downloading latest BETA SABnzbd ($beta)"
fetch http://downloads.sourceforge.net/project/sabnzbdplus/sabnzbdplus-beta/${beta}/SABnzbd-${beta}-src.tar.gz
;;
	"4")
echo
echo "Exiting"
exit
;;
    * )
echo
echo "Wrong selection nothing selected"
echo "Exiting"
exit
;;
esac

echo
echo
#Continues with script
echo "*******1. Installing the required prerequisites*******"
echo "--Installing SABnzb (0.4.8)--"
pkg_add -r sabnzbdplus
echo "--Installing py25-yenc--"
pkg_add -r py25-yenc
echo "--Installing unzip--"
pkg_add -r unzip
echo "--Installing py25-pysqlite-2.3.5--"
pkg_add -r "ftp://ftp.freebsd.org/pub/FreeBSD/ports/$arch/packages-7.2-release/All/py25-pysqlite-2.3.5.tbz"
echo "--Starting SABnzbd (to generate config file)--"
/usr/local/bin/SABnzbd.py -d -f /root/.sabnzbd/sabnzbd.ini
echo "--Shutting down SABnzb--"
/usr/bin/fetch "http://localhost:8080/sabnzbd/api?mode=shutdown"
echo "*******2. Removing outdated files/folders*******"
rm -rf /usr/local/lib/python2.5/site-packages/cherrypy
rm -rf /usr/local/lib/python2.5/site-packages/sabnzbd
rm -rf /usr/local/share/sabnzbdplus/interfaces
rm -rf /usr/local/share/sabnzbdplus/language
rm -rf /usr/local/bin/SABnzbd.py
echo "--done--"
echo "*******3. Extracting updated version of SABnzbd*******"
cd /root/
echo "--Extracting upgrade--"
tar -xzf SABnzbd*
echo "--Extracted upgrade--"
cd SABnzbd*
echo "--done--"
echo "*******4. Moving/Installing newer version of SABnzbd*******"
mv cherrypy /usr/local/lib/python2.5/site-packages/cherrypy
mv sabnzbd /usr/local/lib/python2.5/site-packages/sabnzbd
mv interfaces /usr/local/share/sabnzbdplus/interfaces
mv language /usr/local/share/sabnzbdplus/language
mv SABnzbd.py /usr/local/bin/SABnzbd.py
chmod 0555 /usr/local/bin/SABnzbd.py
ln -s /usr/local/bin/python2.5 /usr/bin/python
ln -s /usr/local/share/sabnzbdplus/interfaces /usr/local/bin/interfaces
ln -s /usr/local/share/sabnzbdplus/language /usr/local/bin/language
echo "--done--"
echo "*******5. Deleting extra files*******"
cd /root/
rm  SABnzbd*.gz
rm -rf SABnzbd*
echo "--done--"
echo "*******Done - SABnzbd should be installed*******"
 
Old 04-26-2010, 11:27 PM   #12
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,193

Rep: Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785
Looks good but I thought I would give some suggestions (which of course you can ignore ):

1. In case the web address were to change for the different sections, I would use variable at the top so you only have to change in one place in the future
2. The beta version I would think always have the letters "RC" in it but maybe not the number "2". So I would change the awk to reflect this.
3. Maybe have a look at the select command, would save the echoes and case statement
4. This is one I have from a package manager i am building, to future proof your tar statement, you can use - tar axf. This will find the necessary compression
program to use and should the extension change in the future you will be covered, assuming you have the right compression agent installed, ie gzip, bzip, etc

Please do not take any of these as criticism of your code. I am just offering alternative
 
Old 04-27-2010, 02:21 PM   #13
Dimitriy
Member
 
Registered: Oct 2005
Distribution: Ubuntu Dapper (6.06)
Posts: 92

Original Poster
Rep: Reputation: 15
Hi grail,

Not out all. I really appreciate your suggestions.

1. Not a bad idea. Its quick to add. I may add it on a later update.
2. This is one part where i uncertain about. So my logic was the first 0 in awk was line was line 0 and the 015 was upto 15 characters. So for the beta I put in 2 for line two. Looking back it seems that this incorrect and the 2 was just coincidence. So it would make good sense to change the 2 in RC? Any insight appreciated - having a hard time grasping awk.
3. Not sure what you mean by this? It looks like an alternate way of doing the menu?
4. I am assuming tar -axf is same effect as tar -xzf (except that axf is a little more robust)? If so then I would be happy to put it in.

thank you,
d
 
Old 04-27-2010, 07:19 PM   #14
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,193

Rep: Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785
2. I thought I would use the following to illustrate - awk '/0/{gsub(/\015/,"");print}'

/0/ - means only do what is in the following braces if you find a line(s) with a 0 in them
gsub(/\015/,"") - \015 is an octal representation of funky MS text which needs to be removed so we do not get strange quirks in the echoed text
print - print the now converted line as gsub above works on the entire line by default

So with this explained, you can see that /2/ means all line(s) with a 2 in them, so I thought the beta would always have the letters "RC", so /RC/ would be a better choice.

3. Yes "select" is an alternative menu creator, but the nice part about it is that it works the numbering out for you and automatically returns the choice (ie done in your case). By default it also uses the PS3 prompt variable, so you can try something like:

Actually, after reviewing further I have noticed how different the beta and stable download addresses are (very inconvenient )
In that case I would stay with your case, but I would suggest removing the first entry as stable is exactly the same.


Sorry about the last one.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script: parse active process stderr, strip, dump into variables, use variables TimeFade Programming 1 02-13-2010 06:09 AM
script to parse variables to curl script and execute morphix Programming 17 11-30-2007 01:27 AM
how to parse info from a txt file using c++ code abhisheknayak Programming 8 11-23-2007 01:33 AM
Help w/ script to read file and parse variables cslink23 Linux - General 18 11-26-2006 02:22 AM
Using Select to make a simple directory menu why doesn't it work ctrimble Programming 5 06-07-2004 01:21 AM


All times are GMT -5. The time now is 04:02 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration