Echo Usage if they don't put parameters correct - bash

I have this
USAGE="Usage: -f [file name] -c [column] -v [value] ."
while getopts ":f:c:v: " OPTIONS; do
case $OPTIONS in
f ) file=$OPTARG;;
c ) column=$OPTARG;;
v ) value=$OPTARG;;
h ) echo $USAGE;;
\? ) echo $USAGE
exit 1;;
* ) echo $USAGE
exit 1;;
esac
done
the filename is fun2.sh ... I want to echo the $USAGE if they fail to put a parameter in -f or -c or -v.
I have tryied putting a
" ") echo $USAGE
exit1;;
but that didn't work..
I also tried
if [ $file || $column || $value == "" ]
echo $USAGE
but then again it didn't work..
Any Help would be appreciated
EDIT
What worked for me
if [ "$file" == "" ] ;
then
echo $USAGE
elif [ "$column" == "" ];
then
echo $USAGE
elif [ "$value" == "" ];
then
echo $USAGE
else
show_column
check_temp
file_move
write_file

Try:
[[ -z "$file" || -z "$column" || -z "$value" ]] && echo "$USAGE" && exit

You can't do this in the loop. Check the values of the variables after the loop and print $USAGE if they are empty or the values are wrong (not an integer, for example).

Not entirely sure why your code doesn't work, but this should:
USAGE="Usage: -f [file name] -c [column] -v [value] ."
params="$(getopt -o f:c:v:h --name "$(basename -- "$0")" -- "$#")"
if [ $? -ne 0 ]
then
echo "$USAGE"
exit 1
fi
eval set -- "$params"
while true
do
case $1 in
-f)
file="$2"
shift 2
;;
-c)
column="$2"
shift 2
;;
-v)
value="$2"
shift 2
;;
-h)
echo "$USAGE"
exit
;;
--)
shift
break
;;
*)
echo "$USAGE"
exit 1
;;
esac
done

John, i dont understand what is a bad with your code: In my bash got the following: (the name of script is "go") - OK mean, got what i expected.
jonatan:shell clt$ ./go
#ok
jonatan:shell clt$ ./go ewdwedw
#ok
jonatan:shell clt$ ./go -a
Usage: -f [file name] -c [column] -v [value] .
#ok, -a is incorrect
jonatan:shell clt$ ./go -f
Usage: -f [file name] -c [column] -v [value] .
#ok, -f need an argument
jonatan:shell clt$ ./go -f aaa
#ok, -f has an argiment
jonatan:shell clt$ ./go -c
Usage: -f [file name] -c [column] -v [value] .
jonatan:shell clt$ ./go -c -v
#hm - "-v" comes into `column`, so syntactically is ok, but ....
Therefore you need another check - as you done already. So, your script is OK.
Here is my "go".
#!/bin/bash
USAGE="Usage: -f [file name] -c [column] -v [value] ."
while getopts ":f:c:v:" OPTIONS; do
case "$OPTIONS" in
f ) file=$OPTARG;;
c ) column=$OPTARG;;
v ) value=$OPTARG;;
h ) echo $USAGE;;
\? ) echo $USAGE; exit 1;;
* ) echo $USAGE; exit 1;;
esac
done

Related

How to make it manditory for options to be spaced for bash scripts

I have the following script.
I would like to modify it so that if I were to call temp.sh with both the options, I would have to space them. Ie: A call to the script like temp.sh -fc30 should be invalid, rather it should be temp.sh -f -c 30
ARGS=$(getopt -o c:f -l "charlie:fox" -n "temp.sh" -- "$#");
#bad args
if [ $? -ne 0 ];
then
exit 1
fi
eval set --"$ARGS";
while true; do
case "$1" in
-c|--charlie)
shift;
if [ -n "$1" ]; then
echo "-c =: $1";
shift;
fi
;;
-f|--fox)
shift;
echo "fox used";
;;
--)
shift;
break;
;;
esac
done
Just don't use getopt.
#!/bin/bash
# parse options
while [[ $# -gt 0 ]]; do
case $1 in
-c|--charlie)
echo "$1 = $2"
shift
;;
-f|--fox)
echo "fox used"
;;
--)
shift
break
esac
shift
done
# do script

bash - passing optional arguments to script - parametername + string_value

i use a script that accepts parameter. parameters are optional and may occur in any order.
#!/bin/bash
# script name: test.sh
for var in "$#"
do
if [ ! -z "$var" ] && ([ $var = "--example" ] || [ $var = "-e" ]); then
echo "example"
elif [ ! -z "$var" ] && ([ $var = "--project" ] || [ $var = "-p" ]); then
echo "project with string xxxxxxx"
fi
done
in this simple example, you could call it like follows (some examples):
# this will echo example
./test.sh --example
# this will echo project with string xxxxxxx
./test.sh --project
# this will echo both example and project with string xxxxxxx
./test.sh --example --project
NOW, what i want to achieve is that i can do something like this (warning, this is pseuco code):
#!/bin/bash
# script name: test.sh
for var in "$#"
do
if [ ! -z "$var" ] && ([ $var = "--example" ] || [ $var = "-e" ]); then
echo "example"
elif [ ! -z "$var" ] && ([ $var = "--project" ] || [ $var = "-p" ]); then
echo "project with string $VAR_VALUE"
fi
done
# this will echo example
./test.sh --example
# this will echo project with string myproject1
./test.sh --project="myproject1"
# this will echo both example and project with string myproject2
./test.sh --example --project="myproject2"
can someone help me rewrite it so this will work somehow?
Use getopt. It handles short and long options, allows for both --long value and --long=value, decomposes -abc into -a -b -c, understands -- to end option parsing, and more.
#!/bin/bash
args=$(getopt -o ep: -l example,project: -n "$0" -- "$#") || exit
eval set -- "$args"
while [[ $1 != '--' ]]; do
case "$1" in
-e|--example) echo "example"; shift 1;;
-p|--project) echo "project = $2"; shift 2;;
# shouldn't happen unless we're missing a case
*) echo "unhandled option: $1" >&2; exit 1;;
esac
done
shift # skip '--'
echo "remaining non-option arguments: $#"
There are two possible path toward parsing argument list
Build custom option parser
use getopt, using 'long options'
The first approach is relatively simple (at this time). Using case instead of if to handle variants:
last_arg=
for arg in "$#"
do
if [ "$last_arg" = "-p" ] ; then
VAR_VALUE=$arg ;
last_arg=
echo "project with string $VAR_VALUE"
continue
fi
case "$arg" in
-e | --example)
echo "example" ;;
-p)
last_arg=$arg ;;
--project=*)
VAR_VALUE=${arg#*=}
echo "project with string $VAR_VALUE" ;;
*) ERROR-MESSAGE ;;
esac
done
exit
The BETTER approach is to leverage existing code. In particular getopt, which can handle long options:
#! /bin/bash
if T=$(getopt -o ep: --long 'example,project:' -n ${0#*/} -- "$#") ; then
eval set -- "$T"
else
exit $?
fi
while [ "$#" -gt 0 ] ; do
case "$1" in
-e | --example)
echo "example"
;;
-p | --project)
shift
VAR_VALUE=$1
echo "project with string $VAR_VALUE"
;;
--)
break
;;
*) echo "ERROR:$1" ;;
esac
shift
done

getopts second flag is not required?

Having issues making sure that both -v and -o are both required elements but -h is not in my getopts. how would I fix this?
usage() { echo "Usage: $0 [-v <5.x|6.x>] [-o <string>]" 1>&2; exit 1; }
if ( ! getopts ":v:o:h" opt); then
echo "Usage: `basename $0` options (-v [version]) (-o [organization unit]) -h for help";
exit $E_OPTERROR;
fi
while getopts ":v:o:h" opt; do
case $opt in
v) vermajor=$OPTARG;;
o) org_unit=$OPTARG;;
h) usage;;
\?) echo "Invalid option: -$OPTARG" >&2; exit 1;;
:) echo "Option -$OPTARG requires an argument." >&2; exit 1;;
esac
done
exit
What about this?:
usage() { echo "Usage: $0 [-v <5.x|6.x>] [-o <string>]" 1>&2; exit 1; }
test=$(echo "$#" | grep "\-v" | grep "\-o")
if [[ -z "$test" ]]; then
printf "requirements not met.\n"
usage
fi
if [[ -n "$test" ]]; then
printf "requirements met.\n"
fi
Output:
bob#crunchbang:~$ ./test -v 5.0 -h
requirements not met.
Usage: ./test [-v <5.x|6.x>] [-o <string>]
bob#crunchbang:~$ ./test -v 5.0
requirements not met.
Usage: ./test [-v <5.x|6.x>] [-o <string>]
bob#crunchbang:~$ ./test -o "yoh"
requirements not met.
Usage: ./test [-v <5.x|6.x>] [-o <string>]
bob#crunchbang:~$ ./test
requirements not met.
Usage: ./test [-v <5.x|6.x>] [-o <string>]
Ended up with this
usage() { echo "Usage: $0 [-v <5.x|6.x>] [-o <string>]" 1>&2; exit 1; }
if [[ "$1" =~ "^((-{1,2})([Hh]$|[Hh][Ee][Ll][Pp])|)$" ]]; then
usage
else
while [[ $# -gt 0 ]]; do
opt="$1"; shift
current_arg="$1"
if [[ "$current_arg" =~ ^-{1,2}.* ]]; then
echo "==> WARNING: You may have left an argument blank. Double check your command."
fi
case "$opt" in
"-v"|"--version" ) vermajor="$1"; shift;;
"-o"|"--org" ) org_unit="$1"; shift;;
* ) echo "==> ERROR: Invalid option: \""$opt"\"" >&2
exit 1;;
esac
done
fi
if [[ "$vermajor" == "" || "$org_unit" == "" ]]; then
echo "ERROR: Options -v and -o require arguments." >&2; exit 1
fi
exit

Bash script and getopts outputting saved variables

I'm trying to put together a script to monitor files and im having a hard tiem with getopts. If I run more than once the shell values dont changes... There's a ton of clean up but I've been stuck on this one issue for a couple hours so ...
#!/bin/bash
function printUsage {
echo "Usage: $0 [-e environment] [-r region] [-l logtype]"
echo "Requirements:"
echo " -e environment to look in; prod or dev"
echo " -r 3 digit region designation"
echo " -l log type to find; soap, message, main, service, or detail"
echo " example: $0 -e dev -r 111 -f soap"
exit 0
}
if [ $# -lt 1 ]; then
printUsage
fi
while getopts "e:r:l:" opt; do
case $opt in
e)
environment="$OPTARG"
;;
r)
region="$OPTARG"
;;
l)
logtype="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG"
printUsage;;
:)
echo "-$OPTARG requires an argument"
printUsage;;
esac
done
#build array of directories to search based on argument
function buildProductionArray {
# assume these are here
logpaths=( )
}
#build array of directories to search based on argument
function buildDevArray {
# assume these are here
logpaths=( )
}
function validateArguments {
#validate the environment
if [ "$(echo "$environment" | tr "[:lower:]" "[:upper:]")" == "PROD" ]; then
buildProductionArray
elif [ "$(echo "$environment" | tr "[:lower:]" "[:upper:]")" == "DEV" ]; then
buildDevArray
else
echo "Usage: findlog [-e environment] [-r region] [-l logtype]"
echo "Invalid environment" >&2; exit 1
fi
#validate the region
if ! [ "$region" -eq "$region" ] 2>/dev/null; then
echo "Usage: findlog [-e environment] [-r region] [-l logtype]"
echo "Invalid region" >&2; exit 1
fi
#validate the logtype
function in_array() {
elements=${1}
element=${2}
for i in "${elements[#]}" ; do
if [ "$i" == "$element" ] ; then
return 1
fi
done
return 0
}
logvalues=(soap message service main detail)
log="$(echo "$logtype" | tr '[:upper:]' '[:lower:]')"
echo "$log"
if in_array logvalues "${log}"; then
if [[ "$log" == "service" || "$log" == "detail" ]]; then
log="-$log-time-"
else
log="-$log-"
fi
else
echo "Usage: findlog [-e environment] [-r region] [-l logtype]"
echo "Invalid environment" >&2; exit 1
fi
}
validateArguments
for i in "${logpaths[#]}"
do
#assume this is here
done

command line menu options

I have been trying to get my menu to work, however it seems that it only accepts one argument/option at a time, how can I get it to accept multiple options?
#!/bin/bash
##### Main
function usage
{
echo "
usage: infinidb [[-f file ] [-i] | [-h]] name ...
}
while [ "$1" != "" ]; do
case $1 in
-f | --file ) shift
filename=$1
;;
-i | ) filename2=$2
;;
-h | --help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done
echo $filename
echo $filename2
when running the script
the following output show show
./script.sh -f apple -i tree
apple tree
This works for me
#!/bin/bash
while [ "$1" != "" ]; do
case $1 in
-f | --file ) shift
filename=$1
;;
-i ) filename2=$2
;;
esac
shift
done
echo $filename
echo $filename2
There was only a problem with -i | )

Resources