LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-22-2018, 02:01 AM   #1
blason
Member
 
Registered: Feb 2016
Posts: 122

Rep: Reputation: Disabled
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

Last edited by astrogeek; 01-22-2018 at 02:07 PM. Reason: Added CODE tags to improve readability
 
Old 01-22-2018, 07:59 AM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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.

Last edited by BW-userx; 01-22-2018 at 08:11 AM.
 
1 members found this post helpful.
Old 01-22-2018, 08:03 AM   #3
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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
 
Old 01-22-2018, 08:21 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,874
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
@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/

Last edited by NevemTeve; 01-22-2018 at 08:24 AM.
 
1 members found this post helpful.
Old 01-22-2018, 08:54 AM   #5
blason
Member
 
Registered: Feb 2016
Posts: 122

Original Poster
Rep: Reputation: Disabled
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.
 
Old 01-22-2018, 08:59 AM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
DO NOT FORGET CODE TAGS! please and thank you.
 
1 members found this post helpful.
Old 01-22-2018, 02:04 PM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
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.

Last edited by astrogeek; 01-22-2018 at 02:09 PM.
 
1 members found this post helpful.
Old 01-26-2018, 11:25 AM   #8
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to delete a stanza without using vi editor? shibir Linux - Newbie 2 09-03-2012 01:03 PM
Do I need a new stanza in network-interfaces to get wireless connection up? sonichedgehog Linux - Networking 9 08-26-2009 04:30 AM
Hi. Jumped in at the deep end of linux pool.glub glub widda LinuxQuestions.org Member Intro 2 06-11-2009 03:38 AM
need lilo stanza for suse eclectic4 SUSE / openSUSE 3 08-18-2006 03:12 PM
C inline functions working linetnew Programming 0 04-17-2005 08:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration