How to get some bash scripts into a simple bash script with some echo and if statement.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
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)
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.