Register
It is currently Thu Sep 18, 2014 5:45 pm

How to create an executable bash script for these commands?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Sat May 24, 2014 11:20 pm   

Joined: Sat May 24, 2014 11:14 pm
Posts: 2
I wish to create an executable bash script that will run the following commands as root, that is, using sudo su
Code:
iptables-save | awk '/^[*]/ { print $1 }
                     /^:[A-Z]+ [^-]/ { print $1 " ACCEPT" ; }
                     /COMMIT/ { print $0; }' | iptables-restore

My first attempt at bash scripting is as follows:
Code:
#!/bin/bash
sudo su
iptables-save | awk '/^[*]/ { print $1 }
                     /^:[A-Z]+ [^-]/ { print $1 " ACCEPT" ; }
                     /COMMIT/ { print $0; }' | iptables-restore
sleep 3;
exit 0

Could someone correct the above and make it workable?


Top
 Profile  
 PostPosted: Sun May 25, 2014 5:19 am   

Joined: Sat May 24, 2014 11:14 pm
Posts: 2
How do you transform the following into a single line?
Code:
iptables-save | awk '/^[*]/ { print $1 }
                     /^:[A-Z]+ [^-]/ { print $1 " ACCEPT" ; }
                     /COMMIT/ { print $0; }' | iptables-restore

Does it look like this?
Code:
iptables-save | awk '^[*] { print $1 } ^:[A-Z]+ [^-] { print $1 " ACCEPT" ; } COMMIT { print $0; }' | iptables-restore

I removed the "/" wherever it appears. Am I correct to state that the "/" is used to break a long line into smaller lines?

What I plan to do is:
Code:
#!/bin/bash
iptables-save | awk '^[*] { print $1 } ^:[A-Z]+ [^-] { print $1 " ACCEPT" ; } COMMIT { print $0; }' | iptables-restore
sleep 3;
exit 0

Save the above and call it cleariptables.sh. I will set it as executable.

It will be placed in a folder called bin in the following path: /home/bonafide/bin/
(username=bonafide)

I will create a shortcut to cleariptables.sh on the desktop. The contents of the shortcut will be:
Code:
[Desktop Entry]
Version=1.0
Type=Application
Terminal=true
Icon[en_US]=nm-device-wired
Name[en_US]=ClearIPtables
Exec=gksudo /home/bonafide/bin/cleariptables.sh
Comment[en_US]=Flush iptables filters
Name=ClearIPtables
Comment=Flush iptables script
Icon=nm-device-wired

Whenever I click on the desktop shortcut, I will be prompted to enter the password and the script will run.


Top
 Profile  
 PostPosted: Sun May 25, 2014 6:12 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 546
hi,

Quote:
Am I correct to state that the "/" is used to break a long line into smaller lines?
no, to break lines into smaller use \, like this, for example:
Code:
some_command \
   | piped2another \
   | andAnother
or
Code:
command -with \
        -a \
        -lot \
        -of \
        -options


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP