LinuxQuestions.org
Help answer threads with 0 replies.
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 06-17-2009, 07:25 AM   #1
Oakems
Member
 
Registered: Nov 2008
Posts: 43

Rep: Reputation: 16
Make a script that uses the output from another script as the variables?


Hi all, I'm really interested in programming and scripting, but I'm very new to it. I've been reading This, and have been playing with bash and some of the scripts.

What I'm trying to do is modify this script;
Code:
#!/bin/bash
PS3="Choose (1-5):"
echo "Choose from the list below."
select name in red green blue yellow magenta
do
	break
done
echo "You chose $name."
so that the options; red, green, blue, yellow, and magenta, are taken from an output of another operation.

I'll explain what I mean, I use a modified version of the script above, like this one;
Code:
#!/bin/bash
PS3=
#echo "Choose from the list below."
select name in red/green/blue/yellow/magenta green/red/blue/yellow/magenta blue/red/green/yellow/magenta yellow/red/green/blue/magenta magenta/red/green/blue/yellow
do
	break
done
echo "$name"
but instead of answering it manually I have it point to a file with the number "1" saved in it, and I use this like so;
Code:
./names < ./answer
This then gives me the desired answer of;
red/green/blue/yellow/magenta

So I've created another script like this;
Code:
#!/bin/bash
tr "/" "\n"
that sorts the colours into new lines, but keeps the order they appear. To use this script I type;
Code:
./names < ./answer | ./translate
So now I have the output of;
red
green
blue
yellow
magenta

and I'd like to know how I can use this as the input for the options to choose from for the first script(if you get my meaning). So it will ask you to choose again and depending on your first answer, the order of the colours for you to choose from will be different.

I'm not sure if any of this makes sense, so I'll try to sum-it-up, the first question gets asked and that determines the order of the colours (the input could be a file?) next it will ask you what colour you actually want (pointless I know, but it's good for learning). So if you wanted red to be at position 5, then 5 would be your first answer, it would then allow you to pick red at position 5.

Thank you for reading, this is obviously just out of pure curiosity, I'm not 100% it's possible, and there might be a better way of doing it, but I like to learn, and think this will help in understanding a few things.
Regards
Oakems
 
Old 06-17-2009, 07:59 AM   #2
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
If you have a script that outputs the options in a single line, then you can simply use an eval to get the output inserted into the 'select' command.

For instance, if the first script, called 'color_gen', is simply:
Code:
#!/bin/bash
echo red green blue yellow magenta
Then you can modify the original script to this:
Code:
#!/bin/bash
PS3="Choose (1-5):"
echo "Choose from the list below."
select name in $(./color_gen)
do
        break
done
echo "You chose $name."
 
Old 06-17-2009, 09:14 AM   #3
Oakems
Member
 
Registered: Nov 2008
Posts: 43

Original Poster
Rep: Reputation: 16
Thanks for your reply, that's almost what I'm thinking of, but instead of getting the information from the script 'color_gen' it gets the information from the output of the 'translation' script.

I've now modified the 'translation' script so it now outputs "echo red green blue yellow magenta" I thought I could send this output to a file, and then use that file for the script with the modification you suggested (named stage2).

I tried the command like this;

./names < ./answer | ./translation > file | ./stage2 < file

I got an error saying 'text file busy'. I cleared the konsole and tried again and it kinda worked, but if I change the 'answer' script to something different I get 'text file busy' again.

Is there anyway of getting around this? I think it's pretty close to a solution.
 
Old 06-17-2009, 09:27 AM   #4
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
Having to write the colors to disk and then read them back in is inefficient and potentially insecure (the file can be modified before it's read back in). Piping it from execution is much faster and cannot be modified in transit by another process.
 
Old 06-17-2009, 10:27 AM   #5
Oakems
Member
 
Registered: Nov 2008
Posts: 43

Original Poster
Rep: Reputation: 16
Okay, but how can I do it directly? The output I'm now getting is "echo red green blue yellow magenta" which is the information I'd like to use. This information is not always the same, it depends on which answer script I use 1, 2, 3, 4, or 5. Each one will give me a different order for the colours, the end result being an output of "echo the colour order you chose"

The 'stage2' script you suggested looks for a script for this information, but I'd like the information to come from that output. How do I make that script use the output that would be "echo colour order of your choice" instead of looking for the information in a script? (that's why I used a file, I thought if the script looks for another script, then I might be able to make a file on the fly and use that instead, but if it's not a good idea, I won't continue, it's needless learning bad habits and I don't intend to do it).
 
Old 06-17-2009, 12:41 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
Quote:
Originally Posted by Oakems View Post
./names < ./answer | ./translation > file | ./stage2 < file

Is there anyway of getting around this? I think it's pretty close to a solution.
Almost right:
Code:
./names < ./answer | ./translation | ./stage2
 
Old 06-17-2009, 04:33 PM   #7
aspire1
Member
 
Registered: Dec 2008
Distribution: Ubuntu
Posts: 62

Rep: Reputation: 23
Is something like below what you're aiming to do? Of course, I may not have understood correctly what you're trying to do.

Code:
#!/bin/bash
colors="red green blue yellow magenta"
quit=false

while( ! $quit )
do
	PS3="Choose (1-5):"
	
	echo "Choose from the list below."
	
	select name in $colors
	do
		case "$REPLY" in
		1)
		colors="red green blue yellow magenta"
		;;
		2)
		colors="green blue yellow magenta red"
		;;
		3)
		colors="blue yellow magenta red green"
		;;
		4)
		colors="yellow magenta red green blue"
		;;
		5)
		colors="magenta red green blue yellow"
		;;
		6)
		quit=true
		;;
		esac

		break
	done

done
 
Old 06-17-2009, 05:59 PM   #8
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
Quote:
Originally Posted by aspire1 View Post
Is something like below what you're aiming to do? Of course, I may not have understood correctly what you're trying to do.
Agree. If this is not what you want, then how about a storyboard from the user's perspective to clarify what you're trying to accomplish - What exactly do you want the user to see as they run this script? Example inputs and outputs would be useful too.
 
Old 06-18-2009, 05:58 AM   #9
Oakems
Member
 
Registered: Nov 2008
Posts: 43

Original Poster
Rep: Reputation: 16
ntubski, the script ./stage2 looks for a file for information and this file doesn't exist, so I can't use;
Code:
./names < ./answer | ./translation | ./stage2
my (bad) idea was to make that file from the output then feed it into stage2 all in one go, which didn't work, I get file busy. I've abandoned that idea now because it isn't a good one and it's not very secure (I don't wish to pick up bad habits).


Okay, it's pretty much two questions back to back.

The first question is to choose from 1-5 below.

1.red/green/blue/yellow/magenta
2.magenta/red/green/blue/yellow
3.yellow/magenta/red/green/blue
4.blue/yellow/magenta/red/green
5.green/blue/yellow/magenta/red

The answer from this will give you a specific order for the colours, if you pick '1' the answer would be;

red/green/blue/yellow/magenta

Now for the next question, I'd like that selection of colours with the order they're in, to be used as the next question. So stage2 would look like this;
1.red
2.green
3.blue
4.yellow
5.magenta

but if you'd chosen 5 as your first answer, stage2 would look like this to you;
1.green
2.blue
3.yellow
4.magenta
5.red


Here's how I have it setup,

Code:
#!/bin/bash
PS3=
#echo "Choose from the list below."
select name in red/green/blue/yellow/magenta green/red/blue/yellow/magenta blue/red/green/yellow/magenta yellow/red/green/blue/magenta magenta/red/green/blue/yellow
do
	break
done
echo "echo $name"
This is the first script, when this is invoked from the command line it'll ask you the first question and list the options available to you. Once you've chosen 1-5, it then prints your anwser on screen. I have the input for this fed to it from a file named answer (which I can change from 1 to 5). So I use the code; ./names < ./answer and it prints the question and the answer (the answer also being the output).

This is the bit that I'm stuck on, how to make this output the next question. Because the output so far is just one line, I've been feeding it to a script named ./transl which can break-up /red/green/blue/yellow/magenta into;
red
green
blue
yellow
magenta

So I now have all the colours separated, but don't know how to use them for the next question. My idea was to make another script to handle this, so that it takes those five colours which ever order they're in and gives you them as the stage2 question. But how I make a script that handles this I'm unsure.

I thought Admiral Beotch was close with this modified script, but it relies on the information coming from another script or a file.
#!/bin/bash
PS3="Choose (1-5):"
echo "Choose from the list below."
select name in $(./color_gen)
do
break
done
echo "You chose $name."

(./color_gen) is the script/file that's looked for, for this information, is there a way of changing it so that instead it looks for the output
 
Old 06-18-2009, 06:41 AM   #10
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
Here you are, sir...

Code:
#!/bin/bash
PS3="Choose (1-5):"
echo "Choose from the list below."
select name in red/green/blue/yellow/magenta green/red/blue/yellow/magenta blue/red/green/yellow/magenta yellow/red/green/blue/magenta magenta/red/green/blue/yellow
do
        break
done
echo "You chose $name."

COLORLIST=$(echo $name | tr "/" " ")

echo "Choose from the list below."
select color in ${COLORLIST}
do
        break
done
echo "You chose $color."

Last edited by JulianTosh; 06-18-2009 at 06:49 AM.
 
Old 06-18-2009, 03:51 PM   #11
Oakems
Member
 
Registered: Nov 2008
Posts: 43

Original Poster
Rep: Reputation: 16
That's it! Thank you so much, I get it. Looking at the script I can clearly see how it works and I can modify my ./answer script to get whatever result I'd like. It was the two part process that was really puzzling me, but I understand now. I was trying to write a separate script for each process, and I wasn't sure how to connect them together, of course you don't get that if it's all part of the same script. I've got a lot to learn, and I've definitely had an epiphany with this, "ah, I see."

Thank you.
 
Old 06-18-2009, 05:21 PM   #12
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
Cool! Keep on Mother Truckin'!

8D
 
  


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
ssh - using variables in call to start remote script from local script babag Linux - Networking 2 06-03-2008 04:50 PM
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 11:33 AM
grep and assign it's output to variables inside script itself problem xxx_anuj_xxx Programming 3 09-22-2007 11:24 PM
How do I make PHP display output from my shell script? farmerjoe Programming 2 04-03-2005 01:15 PM
Scripting : Dual line output into next script as variables facets Programming 6 06-10-2004 02:28 PM

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

All times are GMT -5. The time now is 07:16 PM.

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