Problem:
I need to make this bash script to choose based on the user inputs. Example how can i add choices? such that user just select from 1 to 3 and that is set in variable CLUSTER_NAME.
choices are test.com, try.com and me.com
Script
#!/bin/bash
sops_ops() {
sops --version
if [ "$?" -eq "0" ]; then
echo "proceed sops ops"
else
echo "check sops binary"
fi
read -p 'Enter cluster_NAME: = ' CLUSTER_NAME
test_environment="test.com"
test1_environment="test1.com"
test2_environment="test2.com"
case "${$CLUSTER_NAME}" in
prod.$test_environment) ;;
dev.$test1_environment) ;;
stage.$test2_environment) ;;
test.$test_environment) ;;
*) echo "Invalid option: ${CLUSTER_NAME}" 1>&2 && exit 1 ;;
if [ $CLUSTER_NAME = test.$test_env ];then
printf "got cluster $CLUSTER_NAME"
elif [ $CLUSTER_NAME = "test.test.com" ];then
printf "got dev cluster $CLUSTER_NAME"
echo "not found cluster"
else
echo "Environment not available"
fi
}
sops_ops
Question:
How do I do that?
Any help is appreciated!
Related
Restricting user from trying multiple invalid attempt in shell scripting. I wrote the below script but somehow it's not getting me desire output. I have shared the script and script output both. Kindly help. Here I wanted script to terminate if user tried more than 3 times.
While true
do
echo -n "Enter yes or no"
read opt
case $opt in
yes) break ;;
no) break ;;
*) echo "Invalid input"
while [[ $err -le 3 ]]
do
If [[ $err -le 3 ]]
then
echo "err: $err"
((err++))
break
else
echo "Max limit crossed"
exit 1
fi
done
;;
esac
done
This was a nice question and I had a lot of fun solving it. I have to mention that I'm new to shell programming.
n=0
until [ $n -ge 3 ]
do
read line
if [ "$line" = "XYZ" ]; then
echo "Accepted"
break
else
n=$[$n+1]
echo " trying " $n "times "
fi;
done
This article helped me a lot to solve it.
Try:
#!/bin/bash
ANSWER=
max=3
while true; do
echo "Enter yes or no:"
read -r ANSWER
[[ $ANSWER == "yes" || $ANSWER == "no" ]] && break
echo Invalid Input
ANSWER=
((--max))
[[ $max -le 0 ]] && { echo "Max limit crossed"; exit 1; }
done
I am getting a syntax error with the following shell script and I just can't figure out why:
./query_certs.sh: 22: ./query_certs.sh: Syntax error: "(" unexpected (expecting "fi")
Is it because of nested if-else statements?
Thanks in advance.
# Check how many arguments we received
if [ "$#" -gt 2 ]; then
echo "Usage: $0 [MODE] DESTINATION_IP" >&2
echo "MODE may be one of the following" >&2
echo "none - shell script will ask interactively" >&2
echo "HTTP - query the certificate from HTTPS (port 443)" >&2
echo "LDAP - query the certificate from an AD (port 636" >&2
echo "FTP - query the certificate from an FTPs (ports 990/989)" >&2
exit 1
elif [ "$#" == 2 ]; then
MODE=$1
if [ "$MODE" == "HTTP" ]; then
QUERY="foo"
elif [ "$MODE" == "LDAP" ]; then
QUERY="bar"
elif [ "$MODE" == "FTP" ]; then
QUERY="baz"
else
echo "Please choose a mode (HTTP | LDAP | FTP):" # <<< that's line 22
options=("HTTP" "LDAP" "FTP";)
select opt in "${options[#]}" do
case $opt in
"HTTP")
echo "Selected HTTP"
;;
"LDAP")
echo "Selected LDAP"
;;
"FTP")
echo "Selected FTP"
;;
esac
done
fi
fi
Ok, I fixed it myself. One important thing I didn't mention in my original post (added it now so it might be helpful for others): initially I had the following shebang:
#!/usr/bin/env sh
As I wanted to have POSIX conform. Seems like some routines I am using aren't POSIX conform. Hence, I changed to using bash. Furthermore, there were more mistakes, which are fixed as follows:
# Check how many arguments we received
if [ "$#" -gt 2 ]; then
echo "Usage: $0 [MODE] DESTINATION_IP" >&2
echo "MODE may be one of the following" >&2
echo "none - shell script will ask interactively" >&2
echo "HTTP - query the certificate from HTTPS (port 443)" >&2
echo "LDAP - query the certificate from an AD (port 636" >&2
echo "FTP - query the certificate from an FTPs (ports 990/989)" >&2
exit 1
elif [ "$#" = 2 ]; then
MODE=$1
if [ "$MODE" = "HTTP" ]; then
QUERY="foo"
elif [ "$MODE" = "LDAP" ]; then
QUERY="bar"
elif [ "$MODE" = "FTP" ]; then
QUERY="baz"
else
PS3='Please choose a mode (HTTP | LDAP | FTP):'
options=("HTTP" "LDAP" "FTP")
select opt in "${options[#]}" do
case $opt in
"HTTP")
echo "Selected HTTP"
;;
"LDAP")
echo "Selected LDAP"
;;
"FTP")
echo "Selected FTP"
;;
esac
done
fi
fi
Team,
I am not able to figure out what is the issue with my indentation or syntax. can any one hint? I tried it on linux and i get below error:
error: output
line 23: syntax error near unexpected token `['
line 23: ` if [ $CLUSTER_NAME == prod.$test_environment ]; then'
#!/bin/bash
sops_ops() {
sops --version
if [ "$?" -eq "0" ]; then
echo "proceed sops ops"
else
echo "check sops binary"
fi
read -p 'Enter cluster_NAME: ' CLUSTER_NAME
test_environment="test.nvaa.com"
test1_environment="test1.nvaa.com"
case "${$CLUSTER_NAME}" in
prod.$test_environment) ;;
dev.$test1_environment) ;;
*) echo "Invalid option: ${CLUSTER_NAME}" 1>&2 && exit 1 ;;
if [ $CLUSTER_NAME == prod.$test_environment ]; then
printf "got test cluster $CLUSTER_NAME"
elif [ $CLUSTER_NAME == dev.$test1_environment ];then
printf "got test1 cluster $NAME"
echo "not found cluster"
else
echo "Environment not available"
fi
}
sops_ops
You're are missing esac. Add it after the following snippet :
case "${$CLUSTER_NAME}" in
prod.$test_environment) ;;
dev.$test1_environment) ;;
*) echo "Invalid option: ${CLUSTER_NAME}" 1>&2 && exit 1 ;;
I'm trying to add my own custom check to nagios.
I have successfully created a bash script, that is executable from nagios user.
The problem is that this script works fine from linux command line, but it's return the unknown status if runned by nagios ( returned in web gui ).
#!/bin/sh
while getopts ":q:c:w:h:u:p" optname
do
case "$optname" in "q") query=$OPTARG
;;
"c") CIRT=$OPTARG
;;
"w") WARN=$OPTARG
;;
"u") user=$OPTARG
;;
"p") pswd=$OPTARG
;;
"h") echo "Useage: check_SQLplus_query -u user -p password -w warning value -c cirtical value"
exit
;;
"?") echo "Unknown option $OPTARG"
exit
;;
":") echo "No argument value for option $OPTARG"
exit
;;
*) # Should not occur
echo "Unknown error while processing options"
exit
;;
esac
done
RETVAL=`sqlplus -s USER/PASSWORD#HOST:1521/DBNAME<<EOF
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF
$query;
EXIT;
EOF`
if [ "$RETVAL" -le "$CIRT" ]
then
echo "OK - $RETVAL"
exit 0
elif [ "$RETVAL" -gt "$CIRT" ] && [ "$RETVAL" -le "$WARN" ]
then
echo "WARNING - $RETVAL"
exit 1
elif [ "$RETVAL" -gt "$WARN" ]
then
echo "CRITICAL - $RETVAL"
exit 2
else
echo "UNKNOWN - $RETVAL"
exit 3
fi
Actually the user, password are "coded" inside script, even if I've already set the getopt to make this script more flexible.
The idea is simple, use sqlplus to get a simple query that return only a number ( like the number of the row like my case ).
Bash line to start the script:
/usr/lib64/nagios/plugins/check_SQLplus_queryPrimavera.sh -q "select count(*) from ADMUSER_PM.REFRDEL" -w 6000000 -c 8000000
I have a test script the needs to read the variable 'LAB' and e-mail the correct company.
I've looked but can't find anything that has worked yet.
Any thoughts?
#!
#
LAB=3
#
if [ "$LAB" = "$1" ];then
echo "Got Zumbrota" && ./mailZ
fi
#
if [ "$LAB" = "$2" ];then
echo "Got Barron" && ./mailB
fi
#
if [ "$LAB" = "$3" ];then
echo "Got Stearns" && ./mailS
fi
If this a bash script, start your file with
#!/bin/bash
and use -eq for integer comparison and since LAB is an integer in your script
if [ $LAB -eq $1 ]
These cascading if statements can be condensed into a case statement:
case "$LAB" in
1) echo "Got Zumbrota" && ./mailZ
;;
2) echo "Got Barron" && ./mailB
;;
3) echo "Got Stearns" && ./mailS
;;
*) echo "don't know what to do with $LAB"
;;
esac