Teil 27.
This commit is contained in:
55
teil27/getopt_demo.sh
Executable file
55
teil27/getopt_demo.sh
Executable file
@@ -0,0 +1,55 @@
|
|||||||
|
#! /usr/bin/bash
|
||||||
|
|
||||||
|
help=0
|
||||||
|
verbose=0
|
||||||
|
level=""
|
||||||
|
|
||||||
|
|
||||||
|
# parsing the valid arguments
|
||||||
|
VALID_ARGS=$(getopt -o hvl: --long help,verbose,level: -- "$@")
|
||||||
|
if [[ $? -ne 0 ]]; then
|
||||||
|
exit 1;
|
||||||
|
fi
|
||||||
|
|
||||||
|
eval set -- "$VALID_ARGS"
|
||||||
|
print_params() {
|
||||||
|
count=0
|
||||||
|
for var in "$@"
|
||||||
|
do
|
||||||
|
echo "Param ${count}: ${var}"
|
||||||
|
count=$((${count} + 1))
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
print_params "$@"
|
||||||
|
echo "VALID_ARGS=${VALID_ARGS}"
|
||||||
|
eval set -- "$VALID_ARGS"
|
||||||
|
print_params "$@"
|
||||||
|
|
||||||
|
while [ : ]; do
|
||||||
|
case "${1}" in
|
||||||
|
-h | --help)
|
||||||
|
help=1
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
-v | --verbose)
|
||||||
|
verbose=1
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
-l | --level)
|
||||||
|
level="${2}"
|
||||||
|
shift 2
|
||||||
|
;;
|
||||||
|
--) shift;
|
||||||
|
break
|
||||||
|
;;
|
||||||
|
*) echo "unbekannter Parameter ${1}"
|
||||||
|
exit 1
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
echo "help=${help}"
|
||||||
|
echo "verbose=${verbose}"
|
||||||
|
echo "level=${level}"
|
||||||
|
|
||||||
38
teil27/param_parsing.sh
Executable file
38
teil27/param_parsing.sh
Executable file
@@ -0,0 +1,38 @@
|
|||||||
|
#! /usr/bin/bash
|
||||||
|
|
||||||
|
help=0
|
||||||
|
verbose=0
|
||||||
|
level=""
|
||||||
|
args=()
|
||||||
|
|
||||||
|
while [[ $# -gt 0 ]]; do
|
||||||
|
case "${1}" in
|
||||||
|
-h|--help)
|
||||||
|
help=1
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
-v|--verbose)
|
||||||
|
verbose=1
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
-l|--level)
|
||||||
|
level="${2}"
|
||||||
|
shift 2
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
args+=("${1}")
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
echo "help=${help}"
|
||||||
|
echo "verbose=${verbose}"
|
||||||
|
echo "level=${level}"
|
||||||
|
|
||||||
|
count=0
|
||||||
|
for var in "${args[@]}"; do
|
||||||
|
echo "Arg ${count}: ${var}"
|
||||||
|
((count++))
|
||||||
|
done
|
||||||
|
|
||||||
21
teil27/test_param.sh
Executable file
21
teil27/test_param.sh
Executable file
@@ -0,0 +1,21 @@
|
|||||||
|
#! /usr/bin/bash
|
||||||
|
|
||||||
|
print_params() {
|
||||||
|
count=0
|
||||||
|
for var in "$@"
|
||||||
|
do
|
||||||
|
echo "Param ${count}: ${var}"
|
||||||
|
count=$((${count} + 1))
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
VALID_ARGS=$(getopt -o hvl: --long help,verbose,level: -- "$@")
|
||||||
|
if [[ $? -ne 0 ]]; then
|
||||||
|
exit 1;
|
||||||
|
fi
|
||||||
|
echo "Originalargumente:"
|
||||||
|
print_params "$@"
|
||||||
|
eval set -- "$VALID_ARGS"
|
||||||
|
printf "\nnach eval set:\n"
|
||||||
|
print_params "$@"
|
||||||
|
|
||||||
Reference in New Issue
Block a user