[opensuse] script question
Hello: I have a program that can send its output to a script. I can setup the script command in the program as: <script-name> option1 option2 ..., eg: myscript.sh name time. I know that I can get the options in the script using $1 and $2 etc. But how can I get the program's output that is sent to the script? I would like to write to output to a file. I also would like to use the program's output to different purposes. Can I store it in a variable and use it later, or do I have to save it in a temporary file? Thanks in advance, Istvan -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
Le 06/04/2020 à 13:18, Istvan Gabor a écrit :
But how can I get the program's output that is sent to the script? I would like to write to output to a file.
AFAIR I use echo $a > file or something similar, but there may be something better :-) jdd -- http://dodin.org -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
Disclaimer | Use of IBA e-communication<https://iba-worldwide.com/disclaimer> The contents of this e-mail message and any attachments are intended solely for the recipient (s) named above. This communication is intended to be and to remain confidential and may be protected by intellectual property rights. Any use of the information contained herein (including but not limited to, total or partial reproduction, communication or distribution of any form) by persons other than the designated recipient(s) is prohibited. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. E-mail transmission cannot be guaranteed to be secure or error-free. Ion Beam Applications does not accept liability for any such errors. Thank you for your cooperation.
06.04.2020 14:18, Istvan Gabor пишет:
Hello:
I have a program that can send its output to a script. I can setup the script command in the program as: <script-name> option1 option2 ..., eg: myscript.sh name time.
I know that I can get the options in the script using $1 and $2 etc.
But how can I get the program's output that is sent to the script?
And how exactly is "program output sent to the script"?
I would like to write to output to a file.
I also would like to use the program's output to different purposes. Can I store it in a variable and use it later, or do I have to save it in a temporary file?
Thanks in advance,
Istvan
-- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 06/04/2020 13.18, Istvan Gabor wrote:
Hello:
I have a program that can send its output to a script. I can setup the script command in the program as: <script-name> option1 option2 ..., eg: myscript.sh name time.
I know that I can get the options in the script using $1 and $2 etc.
But how can I get the program's output that is sent to the script? I would like to write to output to a file.
I also would like to use the program's output to different purposes. Can I store it in a variable and use it later, or do I have to save it in a temporary file?
The problem is that the phrase "I have a program that can send its output to a script." is so vague as to be useless. We can not know how does the program sends its output. For example, the program might call the script and pass the data in a pipe. Or it might call the script and pass the data in a file, passing the file name as a parameter. Or it might be as big enough parameters. You really have to look up that program documentation. -- Cheers / Saludos, Carlos E. R. (from 15.1 x86_64 at Telcontar)
Istvan Gabor wrote:
Hello:
I have a program that can send its output to a script. I can setup the script command in the program as: <script-name> option1 option2 ..., eg: myscript.sh name time.
Aha, you have to provide a script and your program will then run that ?
I know that I can get the options in the script using $1 and $2 etc.
But how can I get the program's output that is sent to the script? I would like to write to output to a file.
myscript.sh: #!/bin/sh cat >file
I also would like to use the program's output to different purposes. Can I store it in a variable and use it later, or do I have to save it in a temporary file?
That all depends on what 'later' means. myscript.sh while read x do do whatever you want to x done -- Per Jessen, Zürich (20.3°C) http://www.hostsuisse.com/ - dedicated server rental in Switzerland. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
Thank you all for you fast replies. On Mon, 06 Apr 2020 18:09:01 +0200, Per Jessen wrote:
Istvan Gabor wrote:
Hello:
I have a program that can send its output to a script. I can setup the script command in the program as: <script-name> option1 option2 ..., eg: myscript.sh name time.
Aha, you have to provide a script and your program will then run that ?
Yes. It seems you are the only one who understood / found out what I meant. For the clarity: I have a script. The program will call/run that script. The program provides the script with input data. What I did not know how I refer in the script to the input data. In other words how can I "tell" the script that your input you should work on is provided by the program that called you. I guessed it should be some input/output redirection but all google results gave only command line examples where the input (standard input) was either a file or the terminal input. In the meantime I found that the program sends postscript data for printing. I want to save the original postscript file, modify postscript data and print it.
I know that I can get the options in the script using $1 and $2 etc.
But how can I get the program's output that is sent to the script? I would like to write to output to a file.
myscript.sh:
#!/bin/sh cat >file
Yes, this works. Based on this and Philippe's response I found I can do: cat > file cat - > file # - indicating standard input cat /dev/stdin > file Are these 3 equivalent?
I also would like to use the program's output to different purposes. Can I store it in a variable and use it later, or do I have to save it in a temporary file?
This is what I meant: if I use in my script "cat > file", will the standard input be emptied or remains for a next call? I tried to do this: cat > file cat > file2 file is the normal expected file but file2 is only an empty file. My question is how I can "store" the input data in the script for using it again. I want to modify the data (eg by pstops). Can I save it in a variable or do I have to write it to a temporary file? Thanks again, Istvan -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 06/04/2020 18:22, Istvan Gabor wrote:
I guessed it should be some input/output redirection but all google results gave only command line examples where the input (standard input) was either a file or the terminal input.
Yes, becuase this is Linix and that's what redirection is. A 'filter' takes data to STDIN and provides output on its STDOUT. So I can run program and it takes terminal data from my keyboard and sends out to my terminal screen. I can run program <file1 >file2 and it takes input from file1 and sends output to file2 I can run generator | program | grep significant Along the way there may be arguments supplied to the program. So yes, all this emerges from the 'pipes and filters' model. The man page for the shell shows this and more, different ways of doing co-processing IO based on the STDIN/STDOUT. -- A: Yes. > Q: Are you sure? >> A: Because it reverses the logical flow of conversation. >>> Q: Why is top posting frowned upon? -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
07.04.2020 01:22, Istvan Gabor пишет:
I know that I can get the options in the script using $1 and $2 etc.
But how can I get the program's output that is sent to the script? I would like to write to output to a file.
myscript.sh:
#!/bin/sh cat >file
Yes, this works. Based on this and Philippe's response I found I can do:
cat > file cat - > file # - indicating standard input cat /dev/stdin > file
Are these 3 equivalent?
The first two are. The third form is Linux specific (although some shells emulate it internally) and is better be avoided unless absolutely necessary. Normally it is only used when program itself cannot be told to read from standard input and must be given input file name.
I also would like to use the program's output to different purposes. Can I store it in a variable and use it later, or do I have to save it in a temporary file?
This is what I meant: if I use in my script "cat > file", will the standard input be emptied or remains for a next call? I tried to do this:
cat > file cat > file2
file is the normal expected file but file2 is only an empty file.
The first cat will continue to run until it gets EOF (End Of File) on input. And if it reads from pipe connected to standard input EOF means program writing to pipe closed this pipe on its side. Which means second cat won't have anything to read at all, as standard input is now closed. Redirection creates empty file and nothing is written into it.
My question is how I can "store" the input data in the script for using it again. I want to modify the data (eg by pstops). Can I save it in a variable or do I have to write it to a temporary file?
You can of course do FOO=$(cat) in the beginning which will store input, but note that it will strip at least final newline - shell is not designed to work with binary data. Also pstops works as filter which means you can simply do your-program | pstops with-needed-agruments to modify program output. No shell scripting is needed. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
Istvan Gabor wrote:
Thank you all for you fast replies.
On Mon, 06 Apr 2020 18:09:01 +0200, Per Jessen wrote:
Istvan Gabor wrote:
Hello:
I have a program that can send its output to a script. I can setup the script command in the program as: <script-name> option1 option2 ..., eg: myscript.sh name time.
Aha, you have to provide a script and your program will then run that ?
Yes. It seems you are the only one who understood / found out what I meant. For the clarity: I have a script. The program will call/run that script. The program provides the script with input data. What I did not know how I refer in the script to the input data. In other words how can I "tell" the script that your input you should work on is provided by the program that called you.
In such a setup, the only way is to pipe the output from 'program' to 'script', the output sent to your script is then available as stdin.
In the meantime I found that the program sends postscript data for printing. I want to save the original postscript file, modify postscript data and print it.
that could look something like this: myscript.sh: tee original.ps | ( modify the data modify the data some more ) | lp -
This is what I meant: if I use in my script "cat > file", will the standard input be emptied or remains for a next call?
Ah I see. That is what 'tee' is for - it writes the output to a file and to stdout.
My question is how I can "store" the input data in the script for using it again. I want to modify the data (eg by pstops). Can I save it in a variable or do I have to write it to a temporary file?
Personally, I would write it to a file, as in my example above. I prefer to only keep relatively small amounts of data in variables. -- Per Jessen, Zürich (10.2°C) http://www.hostsuisse.com/ - virtual servers, made in Switzerland. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 06/04/2020 07:18, Istvan Gabor wrote:
Hello:
I have a program that can send its output to a script.
Now that's an ambiguous phrasing if ever I met one! As an old UNIX hacker the idea of a set of pipes&filters is natural enough ... find ... -print0 | xargs -0 ... | grep <parameters> | awk '{format ..}' comes naturally. But the kernel support for that as copocesses doesn't exist in MS-DOS or VMS even if the CLI permits that syntax. Is that what is meant by "can send its output"? Because as far as UNIX/Linux is concerned a script is just a program. There are many 'system' utilities that are implemented as perl or python scripts. All of YaST is implemented using Ruby scripts. Or perhaps the script itself makes use of other programs for file in $(find ... | grep <parameters> ) do ...
I can setup the script command in the program as: <script-name> option1 option2 ..., eg: myscript.sh name time.
I know that I can get the options in the script using $1 and $2 etc.
But how can I get the program's output that is sent to the script? I would like to write to output to a file.
I also would like to use the program's output to different purposes. Can I store it in a variable and use it later, or do I have to save it in a temporary file?
Possibly what you are looking for is program | tee savefile | script If you want a pipe that 'splits' then you need to set up the branch and there are ways to do that with the shell. q.v. man page But you can also cajole the above .. program | tee >(script1 > output.txt) >(script2 | grep ... | script3) Ain't the shell's ability to spawn coprocess and the syntax to pipeline wonderful? -- A: Yes. > Q: Are you sure? >> A: Because it reverses the logical flow of conversation. >>> Q: Why is top posting frowned upon? -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On Mon, 6 Apr 2020 17:19:58 -0400, Anton Aylward wrote:
On 06/04/2020 07:18, Istvan Gabor wrote:
Hello:
I have a program that can send its output to a script.
Now that's an ambiguous phrasing if ever I met one!
Looking it at again, I guess you're right. That's because I did know what terms to use to describe it. I could have written my example like this: Program calls myscript.sh -option -option An myscript.sh runs as: myscript.sh -option -option <the-data-provided-by-the-program>
As an old UNIX hacker the idea of a set of pipes&filters is natural enough ... find ... -print0 | xargs -0 ... | grep <parameters> | awk '{format ..}' comes naturally. But the kernel support for that as copocesses doesn't exist in MS-DOS or VMS even if the CLI permits that syntax.
I even can't guess what the above is about.
Is that what is meant by "can send its output"?
No. What I meant was that the program sends/provides data (output) to the script it calls. The script should work with that data. That is the program's output is "piped" to the script's input. Thanks! Istvan -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 07/04/2020 00.35, Istvan Gabor wrote:
On Mon, 6 Apr 2020 17:19:58 -0400, Anton Aylward wrote:
On 06/04/2020 07:18, Istvan Gabor wrote:
Hello:
I have a program that can send its output to a script.
Now that's an ambiguous phrasing if ever I met one!
Looking it at again, I guess you're right. That's because I did know what terms to use to describe it. I could have written my example like this:
Program calls myscript.sh -option -option
An myscript.sh runs as:
myscript.sh -option -option <the-data-provided-by-the-program>
Bu this . . . . . . . . . . . ********************************** Is not clear at all. You do not say how or where is that data. Now, when you say that myscript.sh: #!/bin/sh cat >file works, it means that the programs provides the data to the script in its standard input "file". And that crucial information you did not say. Now, it is important to know if the program expects the data again in the standard output stream of the script or not. In other words, the program pipes the output to the script it calls, and maybe expects it back. Or not. -- Cheers / Saludos, Carlos E. R. (from 15.1 x86_64 at Telcontar)
On Tue, 7 Apr 2020 01:20:22 +0200, Carlos E. R. wrote:
On 07/04/2020 00.35, Istvan Gabor wrote:
On Mon, 6 Apr 2020 17:19:58 -0400, Anton Aylward wrote:
On 06/04/2020 07:18, Istvan Gabor wrote:
Hello:
I have a program that can send its output to a script.
Now that's an ambiguous phrasing if ever I met one! Looking it at again, I guess you're right. That's because I did know what terms to use to describe it. I could have written my example like this: Program calls myscript.sh -option -option An myscript.sh runs as: myscript.sh -option -option <the-data-provided-by-the-program>
Bu this . . . . . . . . . . . **********************************
Is not clear at all. You do not say how or where is that data.
Because I do not know where that data is or how to refer to it. That was my problem.
Now, when you say that
myscript.sh:
#!/bin/sh cat >file
works, it means that the programs provides the data to the script in its standard input "file". And that crucial information you did not say.
I did not say that because I did not know it. If I had known it, I wouldn't have to start this thread. Why is it so difficult to understand this? And why could Per understand right away what I meant?
Now, it is important to know if the program expects the data again in the standard output stream of the script or not.
In other words, the program pipes the output to the script it calls, and maybe expects it back. Or not.
No, the program does not expects data from the script. Thanks, Istvan -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 07/04/2020 01.38, Istvan Gabor wrote:
On Tue, 7 Apr 2020 01:20:22 +0200, Carlos E. R. wrote:
On 07/04/2020 00.35, Istvan Gabor wrote:
On Mon, 6 Apr 2020 17:19:58 -0400, Anton Aylward wrote:
On 06/04/2020 07:18, Istvan Gabor wrote:
Hello:
I have a program that can send its output to a script.
Now that's an ambiguous phrasing if ever I met one! Looking it at again, I guess you're right. That's because I did know what terms to use to describe it. I could have written my example like this: Program calls myscript.sh -option -option An myscript.sh runs as: myscript.sh -option -option <the-data-provided-by-the-program>
Bu this . . . . . . . . . . . **********************************
Is not clear at all. You do not say how or where is that data.
Because I do not know where that data is or how to refer to it. That was my problem.
Documentation ;-)
Now, when you say that
myscript.sh:
#!/bin/sh cat >file
works, it means that the programs provides the data to the script in its standard input "file". And that crucial information you did not say.
I did not say that because I did not know it. If I had known it, I wouldn't have to start this thread. Why is it so difficult to understand this? And why could Per understand right away what I meant?
He guessed right. He guessed right what the program was doing, not what you meant ;-) Others actually had the same idea and told you, but you did not understand. -- Cheers / Saludos, Carlos E. R. (from 15.1 x86_64 at Telcontar)
On 06/04/2020 18:35, Istvan Gabor wrote:
On Mon, 6 Apr 2020 17:19:58 -0400, Anton Aylward wrote:
On 06/04/2020 07:18, Istvan Gabor wrote:
Hello:
I have a program that can send its output to a script.
Now that's an ambiguous phrasing if ever I met one!
Looking it at again, I guess you're right. That's because I did know what terms to use to describe it. I could have written my example like this:
Program calls myscript.sh -option -option
What program? Direct from the command line or what? Embeded in another script?
An myscript.sh runs as:
myscript.sh -option -option <the-data-provided-by-the-program>
Is this the 'program' mentioned above? What is thiss data on the command line? Is it quoted? How? Why not supply it to the STDIN? via a pipeline?
As an old UNIX hacker the idea of a set of pipes&filters is natural enough ... find ... -print0 | xargs -0 ... | grep <parameters> | awk '{format ..}' comes naturally. But the kernel support for that as copocesses doesn't exist in MS-DOS or VMS even if the CLI permits that syntax.
I even can't guess what the above is about.
OMG! But pipes are so fundamental to UNIX/Linux; they are what makes it so different from all that went before! (There's even a white book on them!) Coprocessing, the parent/child fork model rather than the 'transient program area' model, is another thing that made UNIX distinct. That you can write those sort of scripts at all is an emergent property off this.
Is that what is meant by "can send its output"?
No. What I meant was that the program sends/provides data (output) to the script it calls.
You are ambiguous again. There are a couple of ways that a program can send (backwards) to its parent. Which is the literal interpretation if your above sentence. It would mean that you script has a line with something like ... $(program ...) somewhere in it. If that's NOT the case, then you had better try again describing what's going on. It may be simpler to just show us this script.
The script should work with that data. That is the program's output is "piped" to the script's input.
The Pipe is a mechanism that lets you use two or more commands such that output of one command serves as input to the next. In short, the output of each process directly as input to the next one like a pipeline. Please read the shell man page which describes this and coprocesses and redirection along with some examples. -- A: Yes. > Q: Are you sure? >> A: Because it reverses the logical flow of conversation. >>> Q: Why is top posting frowned upon? -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
Op maandag 6 april 2020 13:18:50 CEST schreef Istvan Gabor:
Hello:
I have a program that can send its output to a script. I can setup the script command in the program as: <script-name> option1 option2 ..., eg: myscript.sh name time.
I know that I can get the options in the script using $1 and $2 etc.
But how can I get the program's output that is sent to the script? I would like to write to output to a file.
I also would like to use the program's output to different purposes. Can I store it in a variable and use it later, or do I have to save it in a temporary file?
Thanks in advance,
Istvan Completely depends on the language your program is writen in. You could write to a file and use some 'while true ... ' script that looks for the existence of such (a) file(s). Most languages can also call scripts where the full command executed would be incl. defined options, i.e name = Blah; time = now(); shellexec ('path/to/script.sh $name $time);
-- Gertjan Lettink a.k.a. Knurpht openSUSE Forums Team -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
participants (8)
-
Andrei Borzenkov
-
Anton Aylward
-
Carlos E. R.
-
Istvan Gabor
-
jdd@dodin.org
-
Knurpht-openSUSE
-
Per Jessen
-
Philippe Andersson