LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to get some bash scripts into a simple bash script with some echo and if statement. (https://www.linuxquestions.org/questions/programming-9/how-to-get-some-bash-scripts-into-a-simple-bash-script-with-some-echo-and-if-statement-932107/)

y0_gesh 03-01-2012 04:01 AM

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?

lithos 03-01-2012 04:47 AM

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

y0_gesh 03-01-2012 05:35 AM

Quote:

Originally Posted by lithos (Post 4615807)
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

lithos 03-01-2012 09:46 AM

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.


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