Using EOF in ksh function - linux

I am writing a script to pass commands to a console and redirect the output to a log for analysis. This is the script I have now.
#!/bin/ksh
gg_sci(){
$GG_HOME/ggsci <<EOF > /home/org/obey.log
obey /home/org/mon.oby
EOF
}
check_st(){
status=`cat obey.log | grep -i $1 | awk '$2!=""{print $2}'`
echo $status
if [ $status -eq "RUNNING" ]
then
echo "GG process $1 is running"
exit 0
}
gg_sci
check_st test
This script works if I put the 2 functions into 2 different scripts. When I put them into one script, I get an error
ksh: 0403-057 Syntax error: `}' is not expected.
After debugging, I've determined the EOF is reading in the } that closes the function. I'm not sure what I'm doing wrong, the EOF function works correctly if that's the only thing in the script.

You're missing a fi to close your if.
Also, -eq is used to check for numeric equality, to compare strings use =:
check_st(){
status=`cat obey.log | grep -i $1 | awk '$2!=""{print $2}'`
echo "$status"
if [ "$status" = "RUNNING" ]
then
echo "GG process $1 is running"
exit 0
fi
}

Related

Port Scanner on with Bash

#!/bin/bash
host=$1
startport=$2
stopport=$3
function pingcheck
{
ping = `ping -c 1 $host | grep bytes | wc -l`
if [ $ping > 1 ]; then
echo "$host is up";
else
echo "$host is down quitting";
exit
fi
}
function portcheck
{
for ((counter=$startport; counter<=$stopport; counter++))
do
(echo > /dev/tcp/$host/$counter) > /dev/null 2>&1 && echo "$counter open"
done
}
pingcheck
portcheck
I tried testing the script by passing 127.0.0.1 1 5 to it from the terminal but all i keep getting is ping: unknown host =
127.0.0.1 is down quitting. Tried with other IP Addresses as well, I got the same output. I was following instruction from a book as I am new to shell scripting. It will be helpful if someone can tell me what I am doing wrong.
I made some comments inline:
#!/bin/bash
host=$1
startport=$2
stopport=$3
function pingcheck
{
ping=`ping -c 1 $host | grep bytes | wc -l` #Don't use spaces before and after the "="
if [ $ping -gt 1 ]; then #Don't use >, use -gt
# if [[ $ping > 1 ]]; then #Or use [[ and ]], but this won't work in all shells
echo "$host is up";
else
echo "$host is down quitting";
exit
fi
}
function portcheck
{
for ((counter=$startport; counter<=$stopport; counter++))
do
(echo > /dev/tcp/$host/$counter) > /dev/null 2>&1 && echo "$counter open"
done
}
pingcheck
portcheck
Variables in bash are always in the format:
VARNAME=VALUE
You should not put spaces in between there. VALUE could be an expression using `` or using $(). $() is usually the preferred way, because you can do $(something $(something)) and you can't do `something `something``.
The syntax of if is:
if EXPRESSION
then
something
fi
An expression is in sh always a call to an application. [ is an application usually used in ifs. You can get a really good manual of [ by doing man [. Bash has native support for [[, which isn't an application, but can do more than [.

Shell script to find the process state while excluding 'ps grep command'

can someone guide me writing shell script to find if the process is active or not? I have to exclude my own grep process filtering from ps command. I want to pass the process as a parameter,
script: (this is currently catching my own process)
#!/bin/sh
SERVICE=$1
echo $1
if ps ax | grep $SERVICE > /dev/null
then
echo "ok"
else
echo "not ok"
fi
example input tried: (though the process is dead I'm getting status as "ok")
./processchecker.sh '/usr/sbin/mysqld'
./processchecker.sh '[/usr/sbin/]mysqld' (i tried using square brackets using online suggestions but failed)
Please help.
You can use pgrep as well - which is a little more efficient:
#!/bin/sh
service=$1
status=0
if [ ! -z "$service" ]; then
pgrep "$service" >/dev/null; status=$?
if [ "$status" -eq 0 ]; then
echo "ok"
else
echo "not ok"
fi
fi
exit "$status"
It's better to have an appropriate exit value as well.
What you have is close, but you want to save the status of the grep command (via $?) and then if/else off of that value.
#!/bin/sh
SERVICE=$1
echo $1
ps ax | grep $SERVICE | grep -v ${0} > /dev/null
status=${?}
if [ "${status}" = "0" ]; then
echo "ok"
else
echo "not ok"
fi

Bash - How to call a function in this scenario

Hello I was wondering what would be the best way break this block of code into functions and
case $# in
1) ports='1-1023'
host=$1 ;;
2) ports=$1
host=$2 ;;
*) echo 'Usage: portscan [port|range] host'
exit 1 ;;
esac
# check port range
if [ "$(echo $ports | grep '^[1-9][0-9]*-[1-9][0-9]*$')" != "" ]; then
firstport=$(echo $ports | cut -d- -f1)
lastport=$(echo $ports | cut -d- -f2)
elif [ "$(echo $ports | grep '^[1-9][0-9]*$')" != "" ]; then
firstport=$ports
lastport=$ports
else
echo "$ports is an invalid port(s) value"
exit 2
fi
# check firstport > lastport
if [ $firstport -gt $lastport ]; then
echo $firstport is larger than $lastport
exit 3
fi
**and call them in main like this****
# Check parameters and exit 1 if problem
check_parms $#
# Check port range and exit 2 if problem
check_ports $ports
# Check port order and exit 3 if problem
check_order $firstport $lastport
When you call a bash function, it too has its own $*, $#, $1, .... to reflect that invocation. So you could build a bunch small functions like
function check_parms() {
local num=$# <----------- I changed this line
case $num)
... etc
}
and then,
function check_ports() {
local ports=$1
if ...
}
etc.
I hope this gives you some ideas for refactoring. In case you were thinking of sourceing the bash script, a good idea might be to unset the functions that you defined at the end of the script.
A nice tutorial at here
In the main block, call it like any other bash function. For example:
echo "hello"
check_parms $*
check_ports $ports
echo "..."

Cant get my if to work in my shellscript

To be as simple as I can be.
I want this script to catch a line whitch say "hello" in the logfile called "out.log"..
The problem I encounter is that it always get "echo expression evaluated as false" as an output even when I know the logfile contains the line that I want
#!/bin/bash
SERVICE_HOME="home/username/Desktop/SMX3test"
CURRENT_USER=`whoami`
start=0
NULLPOINT=out.log
OUT=hello
LINES=20
if [ 'tail -n $LINES $NULLPOINT | grep hello' = "$OUT" ];
then
echo expression evaluated as true
else
echo expression evaluated as false
fi
exit $start
Anywone with a tip or the soloution?
/Phew
#
I got it! ##
#
Pre-Final-Code:
#!/bin/bash
############################
# Phew's Start/stop script #
############################
SERVICE_HOME="opt/data/log/ls/smx3/"
CURRENT_USER=`whoami`
start=0
NULLPOINT=servicemix.log
OUT=NullPointerException
LINES=20
REV=$(tail -n $LINES $NULLPOINT | grep NullPointerException)
if [ $REV = "$OUT" ];
then
/etc/init.d/ls-smx3 stop && sleep 300 && /etc/init.d/ls-smx3 start
else
echo "Allting är okej!"
fi
exit $start
#####
#END#
#####
You can do something like this:
if tail -n $LINES $NULLPOINT | grep hello >/dev/null; then
echo expression evaluated as true
else
echo expression evaluated as false
fi
You don't have to actually match the word (unless you explicitly want that, so you should trim '\n' and make sure that 'hello' it's the only thing on that line).
To grab the output of a command you should surround it with $() like this
A=$(uname)
echo $A
which is equivalent to
A=`uname`
echo $A

Bash Script Variable Scope Issue

username="hello"
password="3333"
function login {
# 1 - Username
# 2 - Password
match=0
cat LoginsMaintMenu.txt | while read line; do
x=`echo $line | awk '{print $1}'`
y=`echo $line | awk '{print $2}'`
if [ "${x}" == "${1}" ] && [ "${y}" == "${2}" ]; then
echo "match"
match=1
echo $match
break
fi
done
echo $match
return $match
}
echo $username $password
login ${username} ${password}
if [ $? -eq 0 ]; then
echo "FAIL"
else
echo "success"
fi
output:
hello 3333
match
1
0
FAIL
THE PROBLEM:
I don't understand why it is echoing "fail". the "match" variable gets set to 1 inside the while loop, but for some reason once I am out of the while loop it still thinks it is the initial zero from its declaration.
I have tried doing a lot of different things, so if someone could give me something concrete to try that'd be great!
Thanks
The reason that this is not working is actually the UUOC. In bash, the right side of a pipeline is ran inside of a sub-shell. Any variables set inside of a sub shell will not be set in the parent shell. To fix this, use redirection instead of a pipeline:
username="hello"
password="3333"
function login {
# 1 - Username
# 2 - Password
match=0
while read x y _; do
if [ "${x}" == "${1}" ] && [ "${y}" == "${2}" ]; then
echo "match"
match=1
echo $match
break
fi
done < LoginsMaintMenu.txt
echo $match
return $match
}
echo $username $password
if login "${username}" "${password}"; then
echo "FAIL"
else
echo "success"
fi
The while read ... part of your code (that gets its input from the cat pipe) runs in a subshell. Changes to variables inside that are not visible outside that subshell.
To work around that, change your loop to:
while read ... ; do
...
done < LoginsMaintMenu.txt

Resources