Last active
August 1, 2022 09:05
-
-
Save mxyq/506ce77b016c5888721ce5c94e2ba41d to your computer and use it in GitHub Desktop.
[Getting options] #Bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
lpr_bin="/usr/bin/lpr" | |
lpadmin_bin="/usr/sbin/lpadmin" | |
named="fuji" | |
size="A4" | |
sides="one-sided" | |
colormode="color" | |
number="1" | |
LONG_ARGUMENT_LIST=( | |
ip | |
delete | |
name | |
number | |
size | |
color | |
sides | |
file | |
) | |
TURNS_ARGUMENT_LIST=( | |
a | |
m | |
d | |
p | |
) | |
ARGS=$(getopt \ | |
--options "$(printf "%s," "${TURNS_ARGUMENT_LIST[@]}")" \ | |
--longoptions "$(printf "%s:," "${LONG_ARGUMENT_LIST[@]}")" \ | |
-n "$(basename "$0")" \ | |
-- "$@" | |
) | |
if [ ! "$?" ]; then echo "Terminating..." >&2 ; exit 1 ; fi | |
eval set -- "${ARGS}" | |
while true; | |
do | |
case "$1" in | |
-a | -m) nsenter -t 1 -m -u -n -i sh -c "$lpadmin_bin -p ""$named"" -E -v socket://""$ip_address"" -m lsb/usr/cupsfilters/Fuji_Xerox-DocuPrint_CM305_df-PDF.ppd" ; shift ;; | |
-d) nsenter -t 1 -m -u -n -i sh -c "$lpadmin_bin -x ""$named""" ; shift ;; | |
-p) nsenter -t 1 -m -u -n -i sh -c "$lpr_bin -P ""$named"" -#""$number"" -o media=""$size"" -o sides=""$sides"" -o colormode=""$colormode"" ""$file"""; shift ;; | |
--name) named="$2" ; shift 2 ;; | |
--ip) ip_address="$2" ; shift 2 ;; | |
--number) number="$2" ; shift 2 ;; | |
--size) size="$2" ; shift 2 ;; | |
--color) colormode="$2" ; shift 2 ;; | |
--sides) sides="$2" ; shift 2 ;; | |
--file) | |
trap 'rm -f "$file"' EXIT | |
file=$(mktemp /var/files/script/.file.XXXXXXXX) || exit 1 | |
if ! curl -s -o "$file" "$2" >> /dev/null 2>&1 ; then | |
unset file | |
fi | |
shift 2 | |
;; | |
--) shift ; break ;; | |
esac | |
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in | |
-V | --version ) | |
echo $version | |
exit | |
;; | |
-s | --string ) | |
shift; string=$1 | |
;; | |
-f | --flag ) | |
flag=1 | |
;; | |
esac; shift; done | |
if [[ "$1" == '--' ]]; then shift; fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment