Wrong test interpretation for simple function.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Wrong test interpretation for simple function.
# 1  
Old 02-13-2018
Wrong test interpretation for simple function.

Hello everyone,
I have written simple script below to check if ip is added to interface
Code:
#!/usr/local/bin/bash

IFCONFIG="/sbin/ifconfig"
SERVICE="/usr/sbin/service"
IP="79.137.X.X"

GREP=$(${IFCONFIG} | grep ${IP})

ip_quantity_check () {

        echo ${GREP} | wc -l

}


if [[ ip_quantity_check -gt 0 ]]; then
        echo "IP is linked"
else
        echo "Nie jest"
        exit 1
fi

When i use only ip_quantity_check function im getting "1" as should be and this is ok. But when i put function into the test "[[ ]]", im getting some wrong interpretation of it becouse i always get:
Code:
else
echo "Nie jest"

Where is a problem ?
Thank you,
# 2  
Old 02-13-2018
You need to deploy "command substitution" as well for shell functions:
Code:
if [[ $( ip_quantity_check) -gt 0 ]] ...

OR use the function's exit code (which can be modified in the return statement) like
Code:
if ip_quantity_check; then ...

This User Gave Thanks to RudiC For This Post:
# 3  
Old 02-13-2018
Ahh, you are right. It's working now.

Thank you !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

I have no idea what is wrong with my simple script.

I am doing a simple "recycle bin" script. It is to check and see if the directory .TRASH exists within the home directory. If not then it makes the directory then appends a date and time to file and then finally moves the file in. However, when I run this script, it is not making the directory as... (5 Replies)
Discussion started by: iamdeman
5 Replies

2. Shell Programming and Scripting

Simple issue, what is wrong?

I had this working a few days ago but I since changed it. Heres the code x=1 while 1 2 3 4 5 6 1=$(ps -ef | grep process | awk '{ print $2}') if then echo "The database is accepting connections..." echo "Now I will check the next process" 2=$(ps -ef | grep process1 |... (10 Replies)
Discussion started by: jeffs42885
10 Replies

3. Shell Programming and Scripting

Whats wrong in the Function ?

Need your assistance, to find the bug in the function. Function usage erroring out even after passing parameters. usage() { if || ; then echo "************************************************************" echo " CHECK USAGE FOR CORRECT PARAMETERS ... (26 Replies)
Discussion started by: raghunsi
26 Replies

4. UNIX for Dummies Questions & Answers

What's wrong with this simple program in APUE?

I start wetting my toes in Linux programming. I tried the first program myls.c in Advanced Programming in the Unix Environment. #include <sys/types.h> #include <dirent.h> #include "apue.h" int main(int argc, char *argv) { DIR *dp; struct... (1 Reply)
Discussion started by: cqlouis
1 Replies

5. Programming

what is wrong with the log function in c?

Hi I looked up the reference but couldn't figure out whats wrong with my log funtions in ANSI C, need this log functions for a memory compiler, please help!! #include <stdio.h> #include <math.h> int main(void) { double d ; double logB(double x, double base); printf(" ... (2 Replies)
Discussion started by: return_user
2 Replies

6. UNIX for Dummies Questions & Answers

What's wrong with this line: if ${TEST:?} ; then echo empty; fi

I want to print "empty" if $TEST is empty. TEST= if ${TEST:?} ; then echo empty; fi But it doesn't work. What's wrong? (2 Replies)
Discussion started by: meili100
2 Replies

7. Shell Programming and Scripting

Wrong date function

Hi, I am getting some very strange output when using date function in PERL on Solaris. Infact the month portion is wrong and it is 1 less then the current, means today it is responding as month =3 , andthis should be 4 ------> April Any help my code is ($day, $month, $year) =... (3 Replies)
Discussion started by: Asteroid
3 Replies

8. Shell Programming and Scripting

Whats wrong with my function?? <newbie>

First of all im using Bash, on a Debian-based machine. I tried to write a function that if the ls program found listed more than 25 lines I would automaticly use "ls | less". Its on another computer but if I recall it looked something like this... Note: some code may look strange because im on... (4 Replies)
Discussion started by: riwa
4 Replies

9. Shell Programming and Scripting

What's wrong with this simple statement?

I know that this is a ridiculously simple statement, but I am getting an error when I execute it, and I can't figure out what it is. Can anyone point me in the right direction? #!/bin/ksh integer dateMonth=0 integer intZero=0 if then dateMonth = 1 fi echo $dateMonth (7 Replies)
Discussion started by: mharley
7 Replies

10. UNIX for Dummies Questions & Answers

What is wrong with this simple script?

The script below is supposed to list file and folder names and list the type besides each - It has no practical value - I am just trying to learn KSH (so far sounds like a bad idea!) Expected output should look like this: folder 1 *** dir *** file1 * file * The code runs but produces... (9 Replies)
Discussion started by: GMMike
9 Replies
Login or Register to Ask a Question