LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-04-2019, 06:39 AM   #1
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Rep: Reputation: 1
I need SERIOUS noob help fixing this script (dont know anything about shell scripting)


Im trying to do a shell script with mostly three things in mind

1: Takes 3 parameters, first is a string and the second and third can be a int, float or string
2: I go thru functions and those functions should exit the shell script returning a exit code
3: The two "core" functions are always the same:

First a integer function: If it is less than, return 0, if it is greater than a but less than b, return 1, if it is greater than b return a 2

Then a string function. This is a two parter. If the function receives one parameter, if the parameter is equal to a value, return 0, if it isnt return 3. The second part would be if the function receives two parameters. If it is equal to the first parameter, return a 1, if it is equal to the second parameter, return a 2. Else, return 0.

So far, I have this shitty code:

https://codeshare.io/amvN6k

it doesnt work and I hope someone can lend a hand and fix it.

Thanks
 
Old 05-04-2019, 08:10 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
returning in bash functions
https://www.linuxjournal.com/content...bash-functions

Comparison Operators
http://tldp.org/LDP/abs/html/comparison-ops.html

Conditionals
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-6.html

BASH does not do floats (last time I checked)


I put my functions on top.

Code:
#!/bin/bash
OK=0
WARN=1
CRIT=2
UNKNOWN=3

#double brackets use them it is bash not sh

# Functions go below this line

check_load() #($2,$3)
{
	#poorly formed command for putting a value inside a variable
    #value=upsc cyberpower ups.load > /dev/stdout 2> /dev/null
    
    #Changed to
    value="$(upsc cyberpower ups.load)" # > /dev/stdout 2> /dev/null
    
    if [[ $value -lt $2 ]]
    then
        echo "OK. Load is $value"
       # return $OK
    #else if [ $value >= $2 && $value < $3 ]
   elif [[ $value -ge $2 && $value -lt $3 ]]
   then
            echo "WARNING. Load is $value"
            #return $WARN
    elif [[ $value -ge $3 ]]
    then
            echo "CRITICAL. Load is $value"
           # return $CRIT
    else
        echo "UNKNOWN. Load is $value"
       # return $UNKNOWN
    fi
}

check_model() #(2)
{
   #  value=upsc cyberpower ups.model > /dev/stdout 2> /dev/null
    
     value="$(upsc cyberpower ups.model)" # > /dev/stdout 2> /dev/null
     
    if [[ "$value" -eq "$2" ]]
    then
        echo "OK. Model is $value"
        #return $OK
    elif [["$value" -ne "$2"]]
    then
            echo "CRITICAL. Model is $value"
         #return $CRITICAL
    else
        echo "UNKNOWN. Model is $value"
        #return $UNKNOWN
    fi
}

#just another way to check for argc, amount of prams on cli

[[ $# -lt '3' ]] && { echo "enter 3 somethings" ; exit ; }

# ??? here you are checking for load and model, but asking for cli prams

case "$1" in
    load )
        check_load $2 $3 ;;
    model )
        check_model $2 ;;
		"" )
        echo -e "You need to supply a parameter." ;;
esac
not saying that is going to do what you want, but it is syntax-ually correct. Though you might want to slap some double quotes around all of your variables too. For that, if you do not know, quote me, safety net.

and that case statement is a little funny.

case statements
Code:
case EXPRESSION in

  PATTERN_1)
    STATEMENTS
    ;;

  PATTERN_2)
    STATEMENTS
    ;;

  PATTERN_N)
    STATEMENTS
    ;;

  *)
    STATEMENTS
    ;;
esac
so the cli should look something like this?
Code:
./script model 2 5
The growth of a BASH case statement incorporating multiple cli prams

https://www.linuxquestions.org/quest...ptions-557981/

also more error checking in your case statement too is advisable.

Last edited by BW-userx; 05-04-2019 at 09:04 AM.
 
Old 05-04-2019, 08:51 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Code:
check_model() #(2)
{
   #  value=upsc cyberpower ups.model > /dev/stdout 2> /dev/null
    
     value="$(upsc cyberpower ups.model)" # > /dev/stdout 2> /dev/null
     
    if [[ "$value" -eq "$2" ]]
    then
        echo "OK. Model is $value"
        #return $OK
    elif [["$value" -ne "$2"]]
    then
            echo "CRITICAL. Model is $value"
         #return $CRITICAL
    else
        echo "UNKNOWN. Model is $value"
        #return $UNKNOWN
    fi
}
Since you are passing one argument to the function it is $1 not $2...
OP, I don't understand purpose of the check_model function since the strings are either the same or not.
 
Old 05-05-2019, 05:57 AM   #4
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by BW-userx View Post
returning in bash functions
https://www.linuxjournal.com/content...bash-functions

Comparison Operators
http://tldp.org/LDP/abs/html/comparison-ops.html

Conditionals
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-6.html

BASH does not do floats (last time I checked)


I put my functions on top.

Code:
#!/bin/bash
OK=0
WARN=1
CRIT=2
UNKNOWN=3

#double brackets use them it is bash not sh

# Functions go below this line

check_load() #($2,$3)
{
	#poorly formed command for putting a value inside a variable
    #value=upsc cyberpower ups.load > /dev/stdout 2> /dev/null
    
    #Changed to
    value="$(upsc cyberpower ups.load)" # > /dev/stdout 2> /dev/null
    
    if [[ $value -lt $2 ]]
    then
        echo "OK. Load is $value"
       # return $OK
    #else if [ $value >= $2 && $value < $3 ]
   elif [[ $value -ge $2 && $value -lt $3 ]]
   then
            echo "WARNING. Load is $value"
            #return $WARN
    elif [[ $value -ge $3 ]]
    then
            echo "CRITICAL. Load is $value"
           # return $CRIT
    else
        echo "UNKNOWN. Load is $value"
       # return $UNKNOWN
    fi
}

check_model() #(2)
{
   #  value=upsc cyberpower ups.model > /dev/stdout 2> /dev/null
    
     value="$(upsc cyberpower ups.model)" # > /dev/stdout 2> /dev/null
     
    if [[ "$value" -eq "$2" ]]
    then
        echo "OK. Model is $value"
        #return $OK
    elif [["$value" -ne "$2"]]
    then
            echo "CRITICAL. Model is $value"
         #return $CRITICAL
    else
        echo "UNKNOWN. Model is $value"
        #return $UNKNOWN
    fi
}

#just another way to check for argc, amount of prams on cli

[[ $# -lt '3' ]] && { echo "enter 3 somethings" ; exit ; }

# ??? here you are checking for load and model, but asking for cli prams

case "$1" in
    load )
        check_load $2 $3 ;;
    model )
        check_model $2 ;;
		"" )
        echo -e "You need to supply a parameter." ;;
esac
not saying that is going to do what you want, but it is syntax-ually correct. Though you might want to slap some double quotes around all of your variables too. For that, if you do not know, quote me, safety net.

and that case statement is a little funny.

case statements
Code:
case EXPRESSION in

  PATTERN_1)
    STATEMENTS
    ;;

  PATTERN_2)
    STATEMENTS
    ;;

  PATTERN_N)
    STATEMENTS
    ;;

  *)
    STATEMENTS
    ;;
esac
so the cli should look something like this?
Code:
./script model 2 5
The growth of a BASH case statement incorporating multiple cli prams

https://www.linuxquestions.org/quest...ptions-557981/

also more error checking in your case statement too is advisable.

Thanks a lot for the script. It looks really good and more or less what I need.....

While I build on it, question: You removed the return lines. How do I return 0, 1, 2, or 3 out of the script? Unless Ive missed something I dont see it.
 
Old 05-05-2019, 06:00 AM   #5
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by michaelk View Post
Code:
check_model() #(2)
{
   #  value=upsc cyberpower ups.model > /dev/stdout 2> /dev/null
    
     value="$(upsc cyberpower ups.model)" # > /dev/stdout 2> /dev/null
     
    if [[ "$value" -eq "$2" ]]
    then
        echo "OK. Model is $value"
        #return $OK
    elif [["$value" -ne "$2"]]
    then
            echo "CRITICAL. Model is $value"
         #return $CRITICAL
    else
        echo "UNKNOWN. Model is $value"
        #return $UNKNOWN
    fi
}
Since you are passing one argument to the function it is $1 not $2...
OP, I don't understand purpose of the check_model function since the strings are either the same or not.
check_model (or any other string based function, forget the name, its just a example) is a two parter than can be used in two ways


./check_model "hi" "bye" - If the value returned by check_model is "hi", it returns $WARNING. If the value returned by check_model is "bye", it returns $CRIT. Anything else other than those two, returns $OK

./check_model "hello" - If the value returned by check_model is "hello", it returns a $OK. Anything else, it returns a $CRIT.


This means that depending if check_model recieves two or three parameters, the logic is different.
 
Old 05-05-2019, 07:56 AM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
In your check_model function as stated is a string based function.
Code:
if [[ "$value" -eq "$2" ]]
The -eq is a binary operator and not used for strings. Use instead:

Code:
if [[ "$value" == "$2" ]]
Quote:
/check_model "hi" "bye" - If the value returned by check_model is "hi", it returns $WARNING. If the value returned by check_model is "bye", it returns $CRIT. Anything else other than those two, returns $OK
Code:
     value="$(upsc cyberpower ups.model)" 
     
    if [[ "$value" == "$1" ]]
    then
        echo "WARNING. Model is $value"
        return $WARNING
    elif [[ "$value" == "$2" ]]
    then
         echo "CRITICAL. Model is $value"
         return $CRITICAL
    else
        echo "OK. Model is $value"
        return $OK
    fi
You need to check the number of arguments since that determines how the function works.
 
Old 05-05-2019, 08:38 AM   #7
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
Quote:
Originally Posted by riahc3 View Post
Thanks a lot for the script. It looks really good and more or less what I need.....

While I build on it, question: You removed the return lines. How do I return 0, 1, 2, or 3 out of the script? Unless Ive missed something I dont see it.
Ss you missed the link I gave you for function and return, it is the first link on top. I’m on my phone 📲 writing this so I’ll leave it as that.
 
Old 05-05-2019, 09:57 AM   #8
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by michaelk View Post
In your check_model function as stated is a string based function.
Code:
if [[ "$value" -eq "$2" ]]
The -eq is a binary operator and not used for strings. Use instead:

Code:
if [[ "$value" == "$2" ]]


Code:
     value="$(upsc cyberpower ups.model)" 
     
    if [[ "$value" == "$1" ]]
    then
        echo "WARNING. Model is $value"
        return $WARNING
    elif [[ "$value" == "$2" ]]
    then
         echo "CRITICAL. Model is $value"
         return $CRITICAL
    else
        echo "OK. Model is $value"
        return $OK
    fi
You need to check the number of arguments since that determines how the function works.

Thanks.

I updated my script again....I cant get the logic working on if the string function gets two or three parameters.

https://codeshare.io/amvN6k

This is what I have for now.
 
Old 05-05-2019, 12:08 PM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
For checking your syntax try www.shellcheck.net
 
Old 05-05-2019, 01:00 PM   #10
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Original Poster
Rep: Reputation: 1
I think I finally have it working
 
Old 05-05-2019, 06:32 PM   #11
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Original Poster
Rep: Reputation: 1
Working good except the string part....

I suspect that the string isnt clean; How do I remove all whitespaces from a variable and reassign it to the same variable? The string I am expecting will never have whitespaces, spaces, blanks, etc.
 
Old 05-05-2019, 07:25 PM   #12
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
There are several ways to debug your script and actually see what is happening. See the following link to see how to use the -x option.

http://tldp.org/LDP/Bash-Beginners-G...ect_02_03.html
 
Old 05-05-2019, 07:54 PM   #13
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
Quote:
Originally Posted by riahc3 View Post
Working good except the string part....

I suspect that the string isnt clean; How do I remove all whitespaces from a variable and reassign it to the same variable? The string I am expecting will never have whitespaces, spaces, blanks, etc.
google 'how to remove blank spaces between strings sed linux'
echo your vars to the cli to see what it is looking at.
do something like this to see it all.
Code:
#!/bin/bash


echo "

[[ "$var1" == "$2" ]] && { echo " it is it is" ; } || { echo "it is not" ;}

"
echo the code itself. Or do what michaelk suggested.
 
  


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
Fixing MBR (Boot Record) Kali (I know, I know..) StevenSmithCIS Linux - Newbie 15 01-31-2018 04:06 PM
[SOLVED] script help (Case & IF statement syntax), easy to answer if you know shell scripting. casperpache Linux - Newbie 12 05-09-2011 07:18 AM
Noob PPPoE ,WLAN problem (dont know what to call it) khaleel5000 Linux - Networking 3 05-02-2011 12:58 AM
i dont know anything about linux - I'm buying a new OS for my laptop slinger2k Linux - Newbie 8 05-03-2006 07:45 PM
Dont know how to install anything stryker5233 Linux - Newbie 4 09-16-2004 01:59 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:03 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