LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 12-23-2010, 06:23 AM   #1
billy72j
LQ Newbie
 
Registered: Dec 2010
Posts: 2

Rep: Reputation: 0
How do I grab variables specified in a data file and use them in the script?


I have a script which uses a Menu file (containing several different Menus). I start out with
Code:
Menu=Main
and grep my VRMenuData file for
Code:
$Menu\:code:$txtring
, where txtring contains the value entered from the keyboard, and cut that line for the code I want executed. So my data looks like:
Quote:
Main:code:1:declare Menu=Download
Main:code:2:soffice -calc VRSched.ods
Main:code:3:echo Load Refresh Menu here
My code acting on the data looks like:

Code:
`grep $Menu\:code:$txtring VRMenuData | cut -d: -f4`
When I chose options 1 thru 3, that executes with no problem (Menu=Download does not work). The program sees into field four, but field four is not able to see outside itself. For instance:

Code:
Download:chan:
Download:pitm:1:echo $numpi\. Return to Main Menu
Download:pitm:2:  
Download:pitm:3:  
Download:pitm:4:echo Enter Menu number (q quits):
Above is more of the VRMenuData file. The
Code:
Download:chan
is used to generate a numbered list of channels. It could be four or 40. Whatever the number is, I want the next line to read, for example, 32. Return to Main Menu.
Code:
num
is the variable in the script I use to number the list of channels. The number following Download: pitm: is numpi, which should be added to num. How do I take
Code:
Download:pitm:1:echo $numpi\. Return to Main Menu
from my VRMenuData file and generate
Code:
32. Return to Main Menu
?
Code:
$ declare -p Menu
declare -- Menu="Download"
$ declare -p numpi
declare -- numpi="1"
$ grep $Menu\:pitm:$numpi VRMenuData | cut -d: -f4
echo $numpi. Return to Main Menu
 
Old 12-23-2010, 06:45 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well I would be lying if I didn't say I am struggling to understand exactly, but let's see if i am on the right track:

In the file VRMenuData is (for example):
Code:
Download:chan:
Download:pitm:1:echo $numpi\. Return to Main Menu
Download:pitm:2:  
Download:pitm:3:  
Download:pitm:4:echo Enter Menu number (q quits):
You then have the following variables set in a script:
Code:
Menu="Download"
numpi="1"
Then you submit the following grep and cut using the above variables:
Code:
grep $Menu\:pitm:$numpi VRMenuData | cut -d: -f4
This then correctly returns:
Code:
echo $numpi\. Return to Main Menu
Assuming all the above is correct?? You would like the result of the grep to be executed??

Well firstly I have no idea how 32 would suddenly come into it seeing numpi is equal to 1, but this would give the output with this value:
Code:
eval $(grep $Menu\:pitm:$numpi VRMenuData | cut -d: -f4)
I would urge you to strongly consider if this is what you need as eval can have a lot of complications.

If I am completely off track, please advise?
 
Old 12-23-2010, 09:37 AM   #3
billy72j
LQ Newbie
 
Registered: Dec 2010
Posts: 2

Original Poster
Rep: Reputation: 0
eval $( ... ) is one key bit I'm after, just one other, if possible

The Download:chan: line triggers a process in the script that goes to my channel format file and loads the list of channels in the Menu list I have as an array. This list can vary in length. So if I have, for example, 31 channels
1. Fox News
2. Fox Sports
3. Vox
.
.
.
30. CNN
31. Discovery
Well, the next line I want is in my VRMenuData file which triggers on Download: pitm:1, where Menu=Download and numpi=1 (':' and 'p' without a space in between gives an unwanted emoticon, hence the space between them). However, I want that listed as item 32 on my Menu list, so they know how to get back to the Main Menu. After that, numpi gets incremented to insert two blank lines and then when numpi is four, it will add to the Menu list
Code:
Enter Menu number (q quits):
I had success with the following in a terminal window:
Code:
 $ declare -p num
declare -- num="5"
$ declare -p numpi
declare -- numpi="1"
$ echo `expr $num + $numpi + 1`. Return to Main Menu
7. Return to Main Menu
I tried putting that into my VRMenuData file and it seems to be working. When it executed
Code:
pval=`eval $(grep $Menu\:pitm:$numpi VRMenuData | cut -d: -f4)`
echo $pval
What came out was
Code:
7. Return to Main Menu
Is there a good way to have that added from within the script? This would help keep the VRMenuData file simpler.

Thanks for your help, Grail.

Last edited by billy72j; 12-23-2010 at 09:49 AM. Reason: clarification on the rest of the lines from the data file
 
Old 12-23-2010, 10:18 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Have you looked at the select statement? It can generate all the numbers for the inputted data and then acts on the number returned once entered.

I would again warn about eval giving sometimes unreliable results. Now you have added more process substitution and expr ... just a caution.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to grab certain data from trace file? yida Linux - Newbie 16 08-03-2010 04:49 PM
How to grab certain data from trace file? yida Linux - Networking 2 07-31-2010 06:20 AM
[SOLVED] HTML Form that sends data entered to a bash script as variables simplified Programming 2 12-01-2009 03:35 PM
Help with a UNIX script: Need to grab information based on time from a file dba_guy Programming 13 05-28-2008 10:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:52 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration