first steps with command line args

This commit is contained in:
Hartmut Bauer committed 2025-03-19 01:38:35 +01:00
1 parent 6d69da50bc
commit 24f6fc05ea
7 files changed
+234 -26

No files matched your search

Executable
+8
View File
@@ -0,0 +1,8 @@
INC=include
HOST_NAME=$(hostname)
testfunc() {
echo "testfunc() works!"
}
Executable
+88
View File
@@ -0,0 +1,88 @@
#!/usr/bin/env bash
show_help() {
echo "usage: $0 options"
echo " -a, --auto start nightly backup"
echo " -l, --list list archives"
echo ' -c, --content <archive> '
echo " show contents of archive"
echo ' -r, --restore <archive> <file(s)> '
echo " restore files from archive to pwd"
echo " -h, -?. --help show this help"
}
echo " ### main.sh"
# Initialize all the option variables.
# This ensures we are not contaminated by variables from the environment.
file=
archive=
while :; do
case $1 in
-h|-\?|--help)
show_help # Display a usage synopsis.
exit 0
;;
-a|--auto)
shift
. $HERE/$HOST_NAME.sh "$@" >$LOGF 2>&1
break # exit kehrt nicht zu run.sh zurück
;;
-l|--list)
. $HERE/$HOST_NAME.sh -l
break;
;;
-c|--content)
if [ "$2" ]; then
echo "not yet implemented"
#. $HERE/$HOST_NAME.sh -c $2
else
echo 'ERROR: "--content" requires a non-empty option argument.'
fi
break
;;
-r|--restore)
if [ "$2" ]; then
echo "not yet implemented"
archive=$2
shift
#. $HERE/$HOST_NAME.sh -r $archive $@
else
echo 'ERROR: "--restore" requires a non-empty option argument.'
fi
break
;;
# --- example -> https://mywiki.wooledge.org/BashFAQ/035
-f|--file) # Takes an option argument; ensure it has been specified.
if [ "$2" ]; then
file=$2
shift
else
die 'ERROR: "--file" requires a non-empty option argument.'
fi
;;
--file=?*)
file=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
--file=) # Handle the case of an empty --file=
die 'ERROR: "--file" requires a non-empty option argument.'
;;
--) # End of all options.
shift
break
;;
-?*)
echo "WARN: Unknown option (ignored): $1"
echo
show_help
exit
;;
*) # Default case: No more options, so break out of the loop.
break
esac
shift
done
Executable
+10
View File
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
echo " *** nathan.sh"
for i in "$@"; do echo "param: $i"; done
echo " include=$INC"
echo " host=$HOST_NAME"
testfunc
echo " *** nathan.sh end"
Executable
+11
View File
@@ -0,0 +1,11 @@
#!/usr/bin/env bash
HERE=$(dirname "$0")
. $HERE/main.inc
LOGF=$HERE/t1.log
echo "--- run.sh"
. $HERE/main.sh "$@"
echo "--- run.sh end"
+10
View File
@@ -0,0 +1,10 @@
testfunc() works!
### main.sh
usage: ./run.sh options
-a, --auto start nightly backup
-l, --list list archives
-c, --content <archive>
show contents of archive
-r, --restore <archive> <file(s)>
restore files from archive to pwd
-h, -?. --help show this help