Skip to content

Instantly share code, notes, and snippets.

@td-shi
Last active March 3, 2021 19:50
Show Gist options
  • Save td-shi/73b7688db1b9cb4ceeccfd610133b882 to your computer and use it in GitHub Desktop.
Save td-shi/73b7688db1b9cb4ceeccfd610133b882 to your computer and use it in GitHub Desktop.
WIP.
#!/bin/bash --posix
# -*- coding:utf-8 -*-
# === Coding shell scripting Memo ==============================================
# ${<name>#<pattern>} :: matching delete with shortest by forword.
# ${<name>##<pattern>} :: matching delete with longest by forword.
# ${<name>%<pattern>} :: matching delete with shortest by backword.
# ${<name>%%<pattern>} :: mathing delete with longest by backword.
# ${<name>/<before>/<after>} :: replace only first matching.
# ${<name>//<before>/<after>} :: replace all matching.
# ${<name>:-<value>} :: if no yet set value, return value.
# ${<name>:=<value>} :: if no yet set value, return value and set.
# ". <shell script>" is to keep current shell and take over environment.
# === Initialize shell environment =============================================
#set -u # Just stop undefined values.
set -e # Just stop error.
#set -x # Debug running command.
umask 0022
export LC_ALL=C
export LANG=C
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/bin:${PATH+:}${PATH-}"
type command >/dev/null 2>&1 && type getconf >/dev/null 2>&1 &&
export PATH="$(command -p getconf PATH):${PATH}"
export UNIX_STD=2003 # to make HP-UX conform to POSIX
# === Define the functions for printing usage and error message ================
usage_and_exit(){
cat <<-" USAGE" 1>&2
GlossTable.sh is .
Usage : GlossTable.sh -
This script is run Apache2 server access.
This application is always recived json params on any method
(GET,PUT,POST,DELETE).
Options : NULL
Version : 2019-05-28_13:58:25 0.01
LICENSE : CC0
This is a public-domain software (CC0). It means that
all of the people can use this for any purposes with no
restrictions at all. By the way, We are fed up with the
side effects which are brought about by the major licenses.
Author : 2019 TD
USAGE
exit 1
}
error_exit() {
${2+:} false && echo "${0##*/}: $2" 1>&2
exit "$1"
}
# === Initialize parameters ====================================================
# Detect home directory of this app. and define more
#Homedir="$(d=${0%/*}/; [ "_$d" = "_$0/" ] && d='./'; cd "$d.."; pwd)"
#PATH="$Homedir/<Add Dir>:$PATH" # for additional command
zzz='' # test value.
#. "$Homedir/<shell script config-file.>" # configration value.
# === Confirm that the required commands exist =================================
# --- cURL or Wget (exsample)
#if type curl >/dev/null 2>&1; then
# CMD_CURL='curl'
#elif type wget >/dev/null 2>&1; then
# CMD_WGET='wget'
#else
# error_exit 1 'No HTTP-GET/POST command found.'
#fi
# === Print usage and exit if one of the help options is set ===================
#case "$# ${1:-}" in
# '1 -h'|'1 --help'|'1 --version') usage_and_exit;;
#esac
# === Read options =============================================================
# while :; do
# case "${1:-}" in
# --zzz=*)
# zzz=$(printf '%s' "${1#--zzz=}" | tr -d '\n')
# echo "${zzz}"
# shift
# ;;
# -zzz)
# zzz=$(printf '%s' "${2:-}" | tr -d '\n')
# echo "${zzz}"
# shift 2
# ;;
# --|-)
# break
# ;;
# --*|-*)
# error_exit 1 'Invalid option'
# ;;
# *)
# break
# ;;
# esac
#done
# === Require parameters check =================================================
#printf '%s\n' "${zzz}" | grep -Eq '^$|^-?[0-9.]+,-?[0-9.]+$' || {
# error_exit 1 'Invalid -l,--location option'
#}
# === Last parameter ===========================================================
#case $# in
# 0) <input>=$(cat -)
# ;;
# 1) case "${1:-}" in
# '--') usage_and_exit;;
# '-') <input>=$(cat -) ;;
# *) <input>=$1 ;;
# esac
# ;;
# *) case "$1" in '--') shift;; esac
# <input>="$*"
# ;;
#esac # Escape 0x0A to 0x1E
# === Define funcitons =========================================================
head_cont (){
printf "Content-type: %s" "${1}"
}
head_state (){
case ${1} in
200) ret="200 OK" ;;
201) ret="201 Created" ;;
204) ret="204 No Content" ;;
400) ret="400 Bad Request" ;;
404) ret="404 Not Found" ;;
501) ret="501 Not Implemented" ;;
*) ret="500 Internal Server Error"
esac
printf "Status: ${ret}"
}
# === Main routine =============================================================
if [-n "${CONTENT_LENGTH}" 0 -ne "${CONTENT_LENGTH}" ] ; then
Request=$(dd bs=${CONTENT_LENGTH})
fi
RESPONSE=""
case ${REQUEST_METHOD} in
GET)
RESPONSE=$(head_state 200)"\n"$(head_cont "application/json")"\n\n"
BODY="{\"Result\": \"未実装\"}"
RESPONSE=${RESPONSE}${BODY}
;;
PUT)
RESPONSE=$(head_state 204)"\n"$(head_cont "text/plain")"\n\n"
;;
POST)
RESPONSE=$(head_state 201)"\n"$(head_cont "text/plain")"\n\n"
;;
DELETE)
RESPONSE=$(head_state 501)"\n"$(head_cont "text/plain")"\n\n"
;;
*) error_exit "1" "UNKNOWN METHOD." ;;
esac
printf "${RESPONSE}"
# === End shell script =========================================================
exit 0
#!/bin/bash --posix
# -*- coding:utf-8 -*-
# === Coding shell scripting Memo ==============================================
# ${<name>#<pattern>} :: matching delete with shortest by forword.
# ${<name>##<pattern>} :: matching delete with longest by forword.
# ${<name>%<pattern>} :: matching delete with shortest by backword.
# ${<name>%%<pattern>} :: mathing delete with longest by backword.
# ${<name>/<before>/<after>} :: replace only first matching.
# ${<name>//<before>/<after>} :: replace all matching.
# ${<name>:-<value>} :: if no yet set value, return value.
# ${<name>:=<value>} :: if no yet set value, return value and set.
# ". <shell script>" is to keep current shell and take over environment.
# === Initialize shell environment =============================================
#set -u # Just stop undefined values.
#set -e # Just stop error.
#set -x # Debug running command.
umask 0022
export LC_ALL=C
export LANG=C
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/bin:${PATH+:}${PATH-}"
type command >/dev/null 2>&1 && type getconf >/dev/null 2>&1 &&
PATH="$(command -p getconf PATH):${PATH}"
export PATH
export UNIX_STD=2003 # to make HP-UX conform to POSIX
# === Define the functions for printing usage and error message ================
usage_and_exit(){
cat <<-" USAGE" 1>&2
<>.sh is .
Usage : <>.sh -
This script is run Apache2 server access.
This application is always recived json params on any method
(GET,PUT,POST,DELETE).
Options : NULL
Version : 2021-03-03T23:05 0.02
LICENSE : CC0
Author : 2021 TD
USAGE
exit 1
}
error_exit() {
${2+:} false && echo "${0##*/}: $2" 1>&2
exit "$1"
}
# === Initialize parameters ====================================================
# Detect home directory of this app. and define more
#Homedir="$(d=${0%/*}/; [ "_$d" = "_$0/" ] && d='./'; cd "$d.."; pwd)"
#PATH="$Homedir/<Add Dir>:$PATH" # for additional command
# === Confirm that the required commands exist =================================
# === Print usage and exit if one of the help options is set ===================
# === Read options =============================================================
# === Require parameters check =================================================
#printf '%s\n' "${zzz}" | grep -Eq '^$|^-?[0-9.]+,-?[0-9.]+$' || {
# error_exit 1 'Invalid -l,--location option'
#}
# === Last parameter ===========================================================
# === Define funcitons =========================================================
head_cont (){
printf "Content-type: %s\n" "${1}"
}
head_state (){
case "${1}" in
200) ret="200 OK" ;;
201) ret="201 Created" ;;
204) ret="204 No Content" ;;
400) ret="400 Bad Request" ;;
404) ret="404 Not Found" ;;
501) ret="501 Not Implemented" ;;
*) ret="500 Internal Server Error" ;;
esac
printf "Status: %s\n" "${ret}"
}
# The following is a remnant of a time when both dd and od were banned.
# cat_bytes () { dd bs=$1 count=1 2>/dev/null ; }
# cat_bytes () { od -A n -N $1 -t o1 -v | tr -Cd '01234567\n' | sed 's/.../\\\\&/g' | xargs -n 1 printf ; }
cat_bytes (){
hexdump -n "${1}" -e '16/1 " %03o" "\n"' -v |\
tr -Cd '01234567\n' |\
sed 's/.../\\\\&/g' |\
xargs -n 1 printf
}
# === Main routine =============================================================
if [ 0 -ne "${CONTENT_LENGTH:-0}" ] ; then
cat_bytes "${CONTENT_LENGTH}" > /tmp/ttt
fi
case "${REQUEST_METHOD}" in # Header.
GET)
printf "%s%s" "$(head_state 200)" "$(head_cont 'application/json')"
;;
PUT)
printf "%s%s" "$(head_state 204)" "$(head_cont 'text/plain')"
;;
# POST)
# printf "%s%s" "$(head_state 201)" "$(head_cont 'text/plain')"
# ;;
# DELETE)
# printf "%s%s" "$(head_state 501)" "$(head_cont 'text/plain')"
# ;;
*)
printf "%s%s" "$(head_state 500)" "$(head_cont 'text/plain')"
;;
esac
printf "%s" "\n" # Sepalater.
case "${REQUEST_METHOD}" in # Body.
GET)
printf "%s" "{\"Result\": \"未実装\"}\n"
;;
PUT)
cat /tmp/ttt
printf "\n"
;;
# POST)
# Head=$(head_state 201)"\n"$(head_cont "text/plain")
# ;;
# DELETE)
# Head=$(head_state 501)"\n"$(head_cont "text/plain")
# ;;
*)
:
;;
esac
# === End shell script =========================================================
exit 0
printf "Content-type: text/palin\n" # Headder
printf "\n" # Separate Head and Body
printf "HTTP_HOST %s\n" "${HTTP_HOST}" # Server address
printf "HTTP_USER_AGENT %s\n" \ # Cliant App
"${HTTP_USER_AGENT}"
printf "REQUEST_METHOD %s\n" # GET,PUT,POST,DELETE
printf "QUERY_STRING %s" \ # Option params by GET,DELETE
"${QUERY_STIRNG}"
printf "DataLen %s\n" "${CONTENT_LENGTH}" # Option Body length by PUT,POST
printf "Data \n%s\n" \ # Option Body by PUT,POST
"$(dd bs=${CONTETN_LENGTH})"
# In `dd`, without optin `if=<any>` and option `of=<any>`,
# then input is stdin and output is stdout.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment