process the output of one shell script in another one
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
process the output of one shell script in another one
Hi dear users,
I just write a shell script that the output of that is something like "danger=0" or "danger=1" for each input that it gives. now i want to get this output of shell script and pass it to other shell script and then regards to the value of "danger" flush a rule from iptable. The problem is that how can i use this output and then do further processing on it. i did something like this but it seems there are some mistakes:
Code:
sh myscript // run the first shell script
while [$1=danger]
if danger = 1
iptables -A INPUT -i lo -j ACCEPT
if danger = 2
iptables -P OUTPUT ACCEPT
how could it be? I mean how could it be possible to get the output of first shell script and do further processing (flush iptable rules regards to each output of shell script) on the output of first shell script regards to each output that it produce?
I wonder if this can't be done more elegant (one script?), but without knowing the full context of what you are trying to accomplish I can't give you any advise on that.
About your question, have a look at this (script number 2)
Code:
#!/bin/bash
for THIS in $@ # $@ contains all arguments from input (script 1)
do
case $THIS in
danger=0) echo "iptables rule for 0" ;;
danger=1) echo "iptables rule for 1" ;;
danger=2) echo "iptables rule for 2" ;;
esac
done
You can now do something like this:
Code:
script_2.sh $( script_1.sh )
This command first executes the green part, which will output danger=0 danger=1 etc. This output is then given to the blue part.
Here's a simple example run:
Code:
$ script_1.sh
danger=0
danger=1
danger=0
danger=2
$ script_2.sh $( script_1.sh )
iptables rule for 0
iptables rule for 1
iptables rule for 0
iptables rule for 2
I just write a shell script that the output of that is something like "danger=0" or "danger=1" for each input that it gives. now i want to get this output of shell script and pass it to other shell script and then regards to the value of "danger" flush a rule from iptable. The problem is that how can i use this output and then do further processing on it. i did something like this but it seems there are some mistakes:
Code:
sh myscript // run the first shell script
while [$1=danger]
if danger = 1
iptables -A INPUT -i lo -j ACCEPT
if danger = 2
iptables -P OUTPUT ACCEPT
how could it be? I mean how could it be possible to get the output of first shell script and do further processing (flush iptable rules regards to each output of shell script) on the output of first shell script regards to each output that it produce?
Thanks a lot
Hi!
Can't you just simply add some exit codes to your script, and then check the exit status from the other script?
Like so:
Code:
sh myscript // run the first shell script
while [$1=danger]
if danger = 1
iptables -A INPUT -i lo -j ACCEPT
exit 10
if danger = 2
iptables -P OUTPUT ACCEPT
exit 20
And then in your other script something like (pseudocode):
Code:
if [ $? == 10 ]; then # $? = check exit status from former command
do stuff
elif [ $? == 20 ]; then
do some other stuff
fi
I wonder if this can't be done more elegant (one script?), but without knowing the full context of what you are trying to accomplish I can't give you any advise on that.
About your question, have a look at this (script number 2)
Code:
#!/bin/bash
for THIS in $@ # $@ contains all arguments from input (script 1)
do
case $THIS in
danger=0) echo "iptables rule for 0" ;;
danger=1) echo "iptables rule for 1" ;;
danger=2) echo "iptables rule for 2" ;;
esac
done
You can now do something like this:
Code:
script_2.sh $( script_1.sh )
This command first executes the green part, which will output danger=0 danger=1 etc. This output is then given to the blue part.
Here's a simple example run:
Code:
$ script_1.sh
danger=0
danger=1
danger=0
danger=2
$ script_2.sh $( script_1.sh )
iptables rule for 0
iptables rule for 1
iptables rule for 0
iptables rule for 2
Hi dear user,
Thanks for your reply. My problem is that i do not know exactly if i echo some firewall rules does it really apply these rules? I mean when i say that danger=0) echo
Quote:
"iptables -A INPUT -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j REJECT"
does it really block the port 80? Or for example when i say danger=1) echo "halt"(system commands not iptable rules) does it really shut down the machine?Do you know i want these commands to be executed regards to the value of danger?
Really thanks for your kind and help
My problem is that i do not know exactly if i echo some firewall rules does it really apply these rules? I mean when i say that
Code:
danger=0) echo"iptables -A INPUT -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j REJECT"
does it really block the port 80? Or for example when i say danger=1) echo "halt"(system commands not iptable rules) does it really shut down the machine?Do you know i want these commands to be executed regards to the value of danger?
The echo "iptables rule for X" parts I used are just an example that is safe to use for testing. It does need to be replaced with the real command.
Here's another example that:
- writes to a logfile if danger=0
- adds an iptables rule if danger=1
- halts the system if danger=2
Code:
#!/bin/bash
for THIS in $@
do
case $THIS in
danger=0) echo "some message" >> /var/log/danger.log ;;
danger=1) /sbin/iptables -A INPUT -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j REJECT ;;
danger=2) /sbin/halt ;;
esac
done
Don't blindly copy and paste the above script, only run it when you understand what it does!
I'm still thinking you are trying to do something the hard way. Why do you need 2 scripts? Can't you combine them? What is your overall goal.
You seem to be very new at (bash) shell scripting; Do have a look at the links I posted. I would recommend starting with the first one.
process the output of one shell script in another one
Quote:
Originally Posted by druuna
The echo "iptables rule for X" parts I used are just an example that is safe to use for testing. It does need to be replaced with the real command.
Here's another example that:
- writes to a logfile if danger=0
- adds an iptables rule if danger=1
- halts the system if danger=2
Code:
#!/bin/bash
for THIS in $@
do
case $THIS in
danger=0) echo "some message" >> /var/log/danger.log ;;
danger=1) /sbin/iptables -A INPUT -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j REJECT ;;
danger=2) /sbin/halt ;;
esac
done
Don't blindly copy and paste the above script, only run it when you understand what it does!
I'm still thinking you are trying to do something the hard way. Why do you need 2 scripts? Can't you combine them? What is your overall goal.
You seem to be very new at (bash) shell scripting; Do have a look at the links I posted. I would recommend starting with the first one.
Hello dear,
I am just trying to know more about shell scripting and i am trying to study the links. Regards to my first script, it is just like this:
It will get data from a file then pass it to another program(written in cpp). I used something like this to run the whole thing that i want and get the latest output that i want:
Quote:
./myscript /path/to/*.text | path to my cpp program
by doing this for example for 2 inputs from text file i have 2 outputs like this:
Quote:
danger=0
danger=1
I put the whole
Quote:
./myscript /path/to/*.text | path to my cpp program
in a shell script to be run(and it is my first shell script). Then i try to communicate between this shell script and the second one. Now i am trying to know more about your code that you write. i have a problem with this:
Quote:
for THIS in $@
. Can i ask you to expalin more about this? I know that it contains arguments from input but i do not know exactly how really it works. Does it really get
Quote:
danger=0
danger=1
as input? I think second shell script does not exactly get
I am just trying to know more about shell scripting and i am trying to study the links. Regards to my first script, it is just like this:
It will get data from a file then pass it to another program(written in cpp). I used something like this to run the whole thing that i want and get the latest output that i want:
Code:
./myscript /path/to/*.text | path to my cpp program
by doing this for example for 2 inputs from text file i have 2 outputs like this:
Code:
danger=0
danger=1
I put the whole
Code:
./myscript /path/to/*.text | path to my cpp program
in a shell script to be run(and it is my first shell script). Then i try to communicate between this shell script and the second one.
The above seems like a very elaborate way of doing things. Depending on your cpp/scripting knowledge, it must be possible to make one script (or cpp executable) that does all the things you describe. Lets put this aside for the moment and focus on the next part.
Quote:
Originally Posted by samasara
Now i am trying to know more about your code that you write. i have a problem with this:
Code:
for THIS in $@
Can i ask you to expalin more about this? I know that it contains arguments from input but i do not know exactly how really it works. Does it really get
Code:
danger=0
danger=1
as input? I think second shell script does not exactly get
Code:
danger=0
danger=1
as input?'
I'm not sure I understand what you mean by Does it really get danger=0 danger=1 as input. You mention that your first script does generate this output, if that is so then script_2.sh does get the input.
Code:
#!/bin/bash
# this is script_2.sh
for THIS in $@
do
case $THIS in
danger=0) echo "got danger=0 as input" ;;
danger=1) echo "got danger=1 as input" ;;
esac
done
The above example works as follows:
- The blue part is a for loop.
This loop uses the input given to the script (the $@ part) and parses this, one entry at the time. Each individual entry ($THIS) can be accessed within the do ... done part.
- The green part is a case statement.
It checks to see if the individual entry ($THIS) matches a pattern (the grey parts). If it finds a matching pattern then the brown part is executed. The brown parts can be just about anything you want. In the above example I used echo.
process the output of one shell script in another one
Quote:
Originally Posted by druuna
The above seems like a very elaborate way of doing things. Depending on your cpp/scripting knowledge, it must be possible to make one script (or cpp executable) that does all the things you describe. Lets put this aside for the moment and focus on the next part.
I'm not sure I understand what you mean by Does it really get danger=0 danger=1 as input. You mention that your first script does generate this output, if that is so then script_2.sh does get the input.
Code:
#!/bin/bash
# this is script_2.sh
for THIS in $@
do
case $THIS in
danger=0) echo "got danger=0 as input" ;;
danger=1) echo "got danger=1 as input" ;;
esac
done
The above example works as follows:
- The blue part is a for loop.
This loop uses the input given to the script (the $@ part) and parses this, one entry at the time. Each individual entry ($THIS) can be accessed within the do ... done part.
- The green part is a case statement.
It checks to see if the individual entry ($THIS) matches a pattern (the grey parts). If it finds a matching pattern then the brown part is executed. The brown parts can be just about anything you want. In the above example I used echo.
Hope this clears things up a bit.
hi agian dear user,
I just study more and my problem solved. Really thanks for your kind and help. Just one point is that for linux command like halt to be applied i should write just halt not /sbin/halt,
Really really thanks.
I just study more and my problem solved. Really thanks for your kind and help.
You're welcome.
Quote:
Originally Posted by samasara
Just one point is that for linux command like halt to be applied i should write just halt not /sbin/halt.
I tend to include the full path for commands that are "powerful/dangerous" for safety reasons.
On Debian, RedHat and Slackware the halt command is found in /sbin. You don't mention the Linux distro you are using, so maybe your halt command is placed elsewhere.
I just have a file in .log format. it contains full of logs and each log contain 5 parts. each time my file will be updated and the last log come to the end of log file. i want to get the last two logs each time. i tried tail -n 2 , to get the last two logs, but it shows me just the last line of log. tails -f also do not show the whole log completely. How could it be? how can i get the last two logs each time?
Thanks a lot for your kind and help
Really wit for your reply
each log contain 5 parts. each time my file will be updated and the last log come to the end of log file. i want to get the last two logs each time. i tried tail -n 2 , to get the last two logs, but it shows me just the last line of log. tails -f also do not show the whole log completely. How could it be? how can i get the last two logs each time?
Without knowing the content of the log file in question and which parts you want to get, it is impossible to help you.
Post a relevant(!!) example of the log file and the expected output (both in [code] ... [/code] tags).
Assume that each time my log file will be updated and 1 new log will be created in it. Each time i want the complete two last log of log file. I need the complete part of two last logs.
process the output of one shell script in another one
Hi dear Chris. i have a log file with lots of logs that 2 sample of these logs are shown in my previous message. each time my logfile will be updated and one new log will be added to it. I just want each time two last log that are created in my logfile for further processing(the whole log from A-Z).i think with awk such a think could not be possible. how would it be?
Best regards
If I assume that the example posted are only the 2 blocks you are talking about then I do have questions:
1) Did you copy/paste the same block twice? Both blocks are the same and there's nothing unique in either of the 2 blocks.
2) Is there anything else in that log that is not related to the 2 blocks you are talking about?
As already asked by chrism01, we need to know exactly which parts in post #12 are the "two last logs" you are talking about. We can only guess and this might not give you the answer you want/need.
I've asked before, but will do so again: Please post a relevant part of the log you are talking about. This should include the 2 blocks you are talking about (make 1 block bold and the other italic and the possible other entries that are in that log.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.