LinuxQuestions.org
Visit Jeremy's Blog.
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 03-01-2012, 04:01 AM   #1
y0_gesh
LQ Newbie
 
Registered: Nov 2011
Posts: 11

Rep: Reputation: Disabled
How to get some bash scripts into a simple bash script with some echo and if statement.


just wrote some scripts and am wondering how to merge these together making only one script.

i wrote these scripts to be enable to route my active network to another when the active one is down.

These are as follows
Script 1:
Quote:
#!/bin/bash
VARIABLE=27
cat policy | sed $VARIABLE'q;d'
This script tells me weather i am using network 1(ADSL) or network 2 (IPVPN)
so as a result i get this message back:
Quote:
dmz ipvpn REJECT info
Script 1 give me line 27 in my shorewall. IPVPN is REJECT so i know im using ADSL

Script 2:
Quote:
#!/bin/sh
# edit_policy.sh
# by ME
#

TESTFILE=policy

# modify line 27. Change REJECT to ACCEPT
sed -i '27,27 s/REJECT/ACCEPT/' -i $TESTFILE

echo REJECT | sed s/REJECT/ACCEPT/
Script 2 modify the line 27 policy(shorewall) and it change REJECT to ACCEPT
so when i echo REJECT i get ACCEPT.

Script 3:
Quote:
#!/bin/sh
# modify_tcrules.sh
# by Yogesh

TESTFILE=tcrules

# modify line 15. Change 1 to 2
sed -i '15,15 s/1/2/' -i $TESTFILE

echo 1 | sed s/1/2/
Script 3 will edit edit line 15 in tcrules(shorewall) and change number 1 to 2
when echo 1 i get 2

How can i get these in only one script with go output info with echo and if statement?
 
Old 03-01-2012, 04:47 AM   #2
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Hi y0_gesh

I would try it this way (may not be completely correct syntax, but you will get it right):
Code:
# 1. getting the output of 1st script to a variable:

#!/bin/bash
VARIABLE=27
POLICY=$(cat policy | sed $VARIABLE'q;d')

# the next line echo is just for testing the output, when script in use, comment it
echo $POLICY
# shold produce output:> dmz ipvpn REJECT info 

# 2. getting the output of 1st part and act according
## =!= /bin/sh
# edit_policy.sh
# by ME
#

# the next "if" line should be corrected properly if this is not working, probably quotes "$POLICY"
if [ "$POLICY" = "dmz ipvpn REJECT info"]; then 
# so if the $POLICY output is TRUE, then do the 2nd script
{
# instead of: TESTFILE=policy from 2nd script
POLICYFILE=policy

# modify line 27. Change REJECT to ACCEPT
sed -i '27,27 s/REJECT/ACCEPT/' -i $POLICYFILE

# echo REJECT | sed s/REJECT/ACCEPT/   <-- can't figure out what should this line do, except it echoes ACCEPT, so I commented
# it is the SAME as: echo "ACCEPT"


# 3. now the 3rd script
## =!=/bin/sh
# modify_tcrules.sh
# by Yogesh

# instead of : TESTFILE=tcrules
RULESFILE=tcrules

# modify line 15. Change 1 to 2
sed -i '15,15 s/1/2/' -i $RULESFILE

# echo 1 | sed s/1/2/    <-- again, I can't figure out what  this line would do echoing 2 - I commented
}
and that should do it (you could copy/paste it for test into script just omit 1st line # 1. getting the..., because everything else should be working)

good luck

Last edited by lithos; 03-01-2012 at 04:53 AM.
 
Old 03-01-2012, 05:35 AM   #3
y0_gesh
LQ Newbie
 
Registered: Nov 2011
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by lithos View Post
Hi y0_gesh

I would try it this way (may not be completely correct syntax, but you will get it right):
Code:
# 1. getting the output of 1st script to a variable:

#!/bin/bash
VARIABLE=27
POLICY=$(cat policy | sed $VARIABLE'q;d')

# the next line echo is just for testing the output, when script in use, comment it
echo $POLICY
# shold produce output:> dmz ipvpn REJECT info 

# 2. getting the output of 1st part and act according
## =!= /bin/sh
# edit_policy.sh
# by ME
#

# the next "if" line should be corrected properly if this is not working, probably quotes "$POLICY"
if [ "$POLICY" = "dmz ipvpn REJECT info"]; then 
# so if the $POLICY output is TRUE, then do the 2nd script
{
# instead of: TESTFILE=policy from 2nd script
POLICYFILE=policy

# modify line 27. Change REJECT to ACCEPT
sed -i '27,27 s/REJECT/ACCEPT/' -i $POLICYFILE

# echo REJECT | sed s/REJECT/ACCEPT/   <-- can't figure out what should this line do, except it echoes ACCEPT, so I commented
# it is the SAME as: echo "ACCEPT"


# 3. now the 3rd script
## =!=/bin/sh
# modify_tcrules.sh
# by Yogesh

# instead of : TESTFILE=tcrules
RULESFILE=tcrules

# modify line 15. Change 1 to 2
sed -i '15,15 s/1/2/' -i $RULESFILE

# echo 1 | sed s/1/2/    <-- again, I can't figure out what  this line would do echoing 2 - I commented
}
and that should do it (you could copy/paste it for test into script just omit 1st line # 1. getting the..., because everything else should be working)

good luck
Hi lithos,
Thanks for the reply and support. I've used the script you just gave me but i get an error
Quote:
dmz ipvpn REJECT info
./final_script.sh: line 42: syntax error: unexpected end of file
As you can see only the 1st script works for the others it wont apply. i just can this error message. :S

Last edited by y0_gesh; 03-01-2012 at 05:36 AM.
 
Old 03-01-2012, 09:46 AM   #4
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Hi

of course you get error, I missed if ... fi (end of loop) and a space between [ "$POLICY" = "dmz ipvpn REJECT info" ]; then
so the complete working code is:
Code:
#!/bin/bash
VARIABLE=27
POLICY=$(cat policy | sed $VARIABLE'q;d')

# the next line echo is just for testing the output, when script in use, comment it
echo $POLICY
# shold produce output:> dmz ipvpn REJECT info 

# 2. getting the output of 1st part and act according
## =!= /bin/sh
# edit_policy.sh
# by ME
#

# the next "if" line should be corrected properly if this is not working, probably quotes "$POLICY"
if [ "$POLICY" = "dmz ipvpn REJECT info" ]; then 

# so if the $POLICY output is TRUE, then do the 2nd script
{
# instead of: TESTFILE=policy from 2nd script
POLICYFILE=policy

# modify line 27. Change REJECT to ACCEPT
sed -i '27,27 s/REJECT/ACCEPT/' -i $POLICYFILE

# echo REJECT | sed s/REJECT/ACCEPT/   <-- can't figure out what should this line do, except it echoes ACCEPT, so I commented
# it is the SAME as: echo "ACCEPT"


# 3. now the 3rd script
## =!=/bin/sh
# modify_tcrules.sh
# by Yogesh

# instead of : TESTFILE=tcrules
RULESFILE=tcrules

# modify line 15. Change 1 to 2
sed -i '15,15 s/1/2/' -i $RULESFILE

# echo 1 | sed s/1/2/    <-- again, I can't figure out what  this line would do echoing 2 - I commented
}
fi
which runs on my server without error.
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Run multiple bash and php scripts from a bash script charu Programming 5 07-26-2011 02:40 AM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 06:00 AM
Bash scripts do not echo commands sean@responsivedata. Linux - Newbie 9 01-17-2006 10:25 AM
Bash scripts do not echo commands sean@responsivedata. Programming 7 01-17-2006 09:08 AM
Bash scripts do not echo commands sean@responsivedata. Linux - Software 3 01-13-2006 09:03 PM


All times are GMT -5. The time now is 03:51 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