将bash函数调用为命令行参数

时间:2017-09-21 15:22:21

标签: linux bash firewalld

希望你能帮忙......

我有一个脚本,用于根据所选功能创建firewalld规则。 我想知道的是可以从命令行调用函数

E.G ./script.sh web

#!/usr/bin/env bash
set -e

### Set IP ###

## Set Ports ###
CHAT=3000
HTTP=80
HTTPS=443
DNS=53
SSH=22  
SMTP=25
MONGO=27017
NFS=111
GMAIL=587


### check for Root user ###

if [ "$(whoami)" == "root" ] ; then
    echo "you are root"
else
    echo "you are not root, This script must be run as root"
exit 1
fi

### Error Checking ###

error_check() {

if [ $? -eq 0 ]; then
    echo "$(tput setaf 2) [ OK ]  $(tput sgr0)"
    sleep 2
else
    echo "$(tput setaf 1) [ FAILED ]  $(tput sgr0)"
    exit 1
fi
}

### No command line arguments ###

if [[ $# -eq 0 ]] ; then
    echo -e "\nUsage: $0 web nfs mail \n" 
    exit 0
fi


 ### Check which firewall is running ###

firewalld=`systemctl list-unit-files | grep firewalld | awk {'print $2'}`
iptables=`systemctl list-unit-files | grep iptables | awk {'print $2'}`


if [[ ${firewalld} == "enabled" ]]; then
    echo "FirewallD is enabled.."
else
    echo "Checking if iptables is enabled.."   
if [[ ${iptables} == "enabled" ]]; then
      echo "iptables is enabled.....Disabling"
      systemctl stop iptables
      systemctl disable iptables
      echo "Starting FirewallD"
      systemctl enable firewalld
      systemctl start firewalld
   echo "FirewallD is now enabled"
  fi
fi

web() {

   firewall-cmd --zone=public --add-port=${HTTP} --permanent
   firewall-cmd --zone=public --add-port=${HTTPS} --permanent
   sudo firewall-cmd --reload       

}

这不是完成的文章,而是一个长镜头,但任何帮助克服这个障碍都会很棒。

谢谢:D

0 个答案:

没有答案