LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Somehow my functions are not working and bing jumped to directly end stanza (https://www.linuxquestions.org/questions/programming-9/somehow-my-functions-are-not-working-and-bing-jumped-to-directly-end-stanza-4175622142/)

blason 01-22-2018 02:01 AM

Somehow my functions are not working and bing jumped to directly end stanza
 
Hi Guys,

I have a below script and somehow this is not working and even I have given the error but not sure why the certain functions are not getting executed.

Can someone please help me here?

Code:

#################
clear
sleep 1
YUMREPO="/etc/yum.repos.d"
HOST=`hostname`
MYIP=`hostname -I`
DIRUNTAR="/opt/collect"
sleep 2

echo -y "Shall I proceed with the Installation? [y/n]:"
echo "Starting the Installation, Please wait...."
        runImpRpm () {
yum install -y wget curl unzip java
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
wget http://www.webmin.com/jcameron-key.asc
rpm --import jcameron-key.asc
        }
#yum -y update
sleep 2
echo "Setting Environment.."
echo "Setting $HOST name"
echo "$MYIP  $HOST" >> /etc/hosts
### Need to change PS1 as well using selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
### Need to check condition if succesful
sed -i 's/SELINUXTYPE=targeted/#ELINUXTYPE=targeted/g' /etc/sysconfig/selinux
systemctl disable firewalld.service
systemctl stop firewalld.service
sleep 2
#### Setting Banner
        runSetupBanner () {
                > /etc/issue
                > /etc/issue.net
                echo "DNS v1.8" >> /etc/issue
                echo "DNS v1.8" >> /etc/issue.net
                        }
echo "Setting Banner"
runSetupBanner
sleep 1
        runSetHosts () {
                echo "xx.xx.xx.xx  host.test.in" >> /etc/hosts
                        }
runSetHosts
yum install -y epel-release bind bind-utils httpd
echo "Enabling HTTP Service"
systemctl enable httpd.service
        setRepos () {
echo "Setting repos.."
###### Elast Repo
cat << EOF > $YUMREPO/elasticserch.repo
[elasticsearch-5.x]
name=Elasticsearch repository for 5.x packages
baseurl=https://artifacts.elastic.co/packages/5.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
EOF
## iSetting Logstash Repo##################
cat << EOF > $YUMREPO/logstash.repo
[logstash-5.x]
name=Elastic repository for 5.x packages
baseurl=https://artifacts.elastic.co/packages/5.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
EOF
####### Setting Kibana Repo
cat << EOF > $YUMREPO/kibana.repo
[kibana-5.x]
name=Kibana repository for 5.x packages
baseurl=https://artifacts.elastic.co/packages/5.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
EOF
######### setting webmin repo
wget http://www.webmin.com/jcameron-key.asc
rpm --import jcameron-key.asc
cat << EOF > $YUMREPO/webmin.repo
[Webmin]
name=Webmin Distribution Neutral
#baseurl=https://download.webmin.com/download/yum
mirrorlist=https://download.webmin.com/download/yum/mirrorlist
enabled=1
EOF
                }
echo "Setting Repositories"
setRepos
sleep 1

        runMoveFiles () {
cd $DIRUNTAR
cp sinkhole.target /etc/named/sinkhole.target
cp change.sh /opt/change.sh
cp -Rpv cf /
mv /var/www/html /var/www/htmlbak
cp -Rpv html /var/www/
mv /etc/named.conf /etc/named.conf-bak
cp named/named.conf /etc/
cp logstash/* /etc/logstash/conf.d/
cp /usr/lib/systemd/system/named.service /usr/lib/systemd/system/named-service-bak
sed -i 's/ExecStartPre/#ExecStartPre/g' /usr/lib/systemd/system/named.service
mv /etc/named.conf /etc/named.conf-bak
        }

###
echo "Setting Directory sturcture....."

        setDirStruct () {
mkdir -p /var/log/named/
        }
setDirStruct
        runYumUpdate () {
        yum update -y all
        }
yum install -y telnet
yum install -y filebeat
yum install -y docker
yum install -y kibana
yum install -y elasticsearch
yum install -y logstash
yum install -y bunzip
yum --enablerepo=epel-testing install -y salt-minion
echo "Enabling and Running Docker Service."
echo "This may take some time, please be patient..."
systemctl enable docker
systemctl enable docker.storage
        runMoveFiles
sleep 2

        runSetupSalt () {
        echo "default_include: minion.d/*.conf" >> /etc/salt/minion
        echo "master: soc.isnlab.in" >> /etc/salt/minion
        echo "master_port: 14146" >> /etc/salt/minion
        echo "pidfile: /var/run/salt-minion.pid" >> /etc/salt/minion
        echo "cache_jobs: True" >> /etc/salt/minion
        echo "ping_interval: 120" >> /etc/salt/minion
        systemctl enable salt-minion.service
        systemctl restart salt-minion.service
                        }
echo "Setting SO&A Environment"
runSetupSalt

        runSetupKibana () {
        echo "server.port: 5601" >> /etc/kibana/kibana.yml
        echo "server.host: $HOST" >>  /etc/kibana/kibana.yml
        echo "server.name: $HOST" >> /etc/kibana/kibana.yml
                }
echo "Setting Kiabana Service"
runSetupKibana

        runSetupelastic () {
        echo "cluster.name: $HOST-clus" >> /etc/elasticsearch/elasticsearch.yml
        echo "node.name: $HOST" >> /etc/elasticsearch/elasticsearch.yml
        echo "network.host: $HOST" >> /etc/elasticsearch/elasticsearch.yml
        echo "http.port: 9200" >> /etc/elasticsearch/elasticsearch.yml
                                }
echo "Setting up Elasticstack"
runSetupelastic

        runDockerSetup () {
        echo "Pulling Docker Images..Please wait"
        sleep 2
        docker pull securityonionsolutions/so-freqserver
        docker pull securityonionsolutions/so-domainstats
        docker run -d --restart=always --name dsisn -p 20000:20000 docker.io/securityonionsolutions/so-domainstats
        sleep 2
        docker run -d --restart=always --name freqisn -p 10004:10004 docker.io/securityonionsolutions/so-freqserver
        sleep 1
        docker ps -a
        sleep 2
        wget -q http://s3.amazonaws.com/alexa-static/top-1m.csv.zip; unzip top-1m.csv.zip;
        docker cp top-1m.csv container_name_goes_here:/opt/domain_stats/top-1m.csv
        docker restart dsisn
        echo "Check whether dockers are in UP state"
                }
runDockerSetup

        runDomainStats () {
                cat << EOF > /etc/cron.d/domainstats
#crontab entry to grab new Top 1m CSV for DomainStats Docker image
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/localbin:/sbin:/bin/usr/sbin:/usr/bin
1 07 * * *  root ( wget -q http://s3.amazonaws.com/alexa-static/top-1m.csv.zip -O /tmp/top-1m.csv.zip && unzip -o /tmp/top-1m.csv.zip -d /tmp && docker cp /tmp/top-1m.csv dsisn:/opt/domain_stats/top-1m.csv && docker restart dsisn && rm -f /tmp/top-1m.csv* ) > /dev/null 2>&1
EOF
        systemctl restart crond.service
        }
echo "Starting Domain stat services.."
runDomainStats
sleep 1

        runWebminConf () {
                /etc/webmin/stop
                sed -i 's/listen=10000/listen=8443/g' /etc/webmin/miniserv.conf
                sed -i 's/port=10000/port=8443/g' /etc/webmin/miniserv.conf
                /etc/webmin/start
                }
echo "Setting Web Portal.."
runWebminConf
sleep 1

        runELKConfig () {
systemctl enable elasticsearch
systemctl enable kibana.service
systemctl enable logstash.service
systemctl start elasticsearch
systemctl start kibana.service
systemctl start logstash.service
/usr/share/logstash/bin/logstash-plugin install logstash-filter-tld
/usr/share/logstash/bin/logstash-plugin install logstash-filter-rest
/usr/share/elasticsearch/bin/elasticsearch-plugin install ingest-geoip
/usr/share/elasticsearch/bin/elasticsearch-plugin install ingest-user-agent
/usr/share/logstash/bin/logstash-plugin install logstash-filter-translate
/usr/share/logstash/bin/logstash-plugin install logstash-filter-tld
/usr/share/logstash/bin/logstash-plugin install logstash-filter-rest
                }
runELKConfig
#runYumUpdate
########################

And here is the error I am facing

Code:

**************************************************

Enabling HTTP Service
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'
Starting Domain stat services..
./install.sh: line 220: runDomainStats: command not found
Setting Web Portal..
./install.sh: line 224: /etc/webmin/stop: No such file or directory
sed: can't read /etc/webmin/miniserv.conf: No such file or directory
sed: can't read /etc/webmin/miniserv.conf: No such file or directory
./install.sh: line 227: /etc/webmin/start: No such file or directory
Failed to issue method call: No such file or directory
Failed to issue method call: No such file or directory
Failed to issue method call: No such file or directory
Failed to issue method call: Unit elasticsearch.service failed to load: No such file or directory.
Failed to issue method call: Unit kibana.service failed to load: No such file or directory.
Failed to issue method call: Unit logstash.service failed to load: No such file or directory.
./install.sh: line 240: /usr/share/logstash/bin/logstash-plugin: No such file or directory
./install.sh: line 241: /usr/share/logstash/bin/logstash-plugin: No such file or directory
./install.sh: line 242: /usr/share/elasticsearch/bin/elasticsearch-plugin: No such file or directory
./install.sh: line 243: /usr/share/elasticsearch/bin/elasticsearch-plugin: No such file or directory
./install.sh: line 244: /usr/share/logstash/bin/logstash-plugin: No such file or directory
./install.sh: line 245: /usr/share/logstash/bin/logstash-plugin: No such file or directory
./install.sh: line 246: /usr/share/logstash/bin/logstash-plugin: No such file or directory


BW-userx 01-22-2018 07:59 AM

at the start where you ask y/n, and your over all structure of your script with usage of functions.

functions on top then call them when needed.
Code:

#!/bin/bash

 

printMessage()
{
        echo "this is function"
}

read -p "would you like to start? y/n" what

if [[ "$what" -eq 'y' ]] ;
then

        echo "you entered $what"
else
        echo "you entered $what"
fi
#executed once
printMessage

echo "in main"

#executed again, twice
printMessage

please edit your post and add code tags for easier reading.
edit:
adding functions usage while post below posted about it... good call.

MensaWater 01-22-2018 08:03 AM

You should define all functions near the beginning of the script.

Defining a function is much like creating a script within your script. Simply defining it does NOT "run" it. You have to call the function later in the script much as you would call another script.

Code:

!/bin/bash
#
# Define function named "testfunc"
testfunc(){
ls -l
}

Main script
# First output a line saying we're testing the function
echo Testing my function
# Now run the fuction
testfunc


NevemTeve 01-22-2018 08:21 AM

@OP: As a start, edit your post and add the [code] and [/code] tags. Then remove everything that is not essential to show the problem. https://stackoverflow.com/help/mcve

Edit: Also read (preferably: learn by heart) this topic: https://www.linuxquestions.org/quest...gs-4175464257/

blason 01-22-2018 08:54 AM

Agree!! and my apologies for that but I kept on writing script while testing each function while at the end something broke that I am not able to figure it out.

Any way let me see if i can define the functions and call them below in a sequence.

BW-userx 01-22-2018 08:59 AM

DO NOT FORGET CODE TAGS! please and thank you.

astrogeek 01-22-2018 02:04 PM

As others have rightly said, in future please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls. I have done that for you at this time.

Also, please try to reduce your problem to a simplest case which reproduces the error, and not ask others to read through a wall of unformatted text for details. This will demonstrate what you have done to troubleshoot the problem so far and help others to more quickly understand it and offer help. Very often in doing so you solve the problem in the process.

Please review the Site FAQ for guidance in posting well formed questions and general forum usage.

Ramurd 01-26-2018 11:25 AM

oh; and if you want to debug and see what is actually going wrong: "set -x" is such a nice thing to have around; be sure to remove / comment it out when you start using it for real... otherwise your terminal may explode.


All times are GMT -5. The time now is 08:25 AM.