I need SERIOUS noob help fixing this script (dont know anything about shell scripting)
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
#!/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
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.
#!/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
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.
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.
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.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.