我有以下 bash 脚本,它提供了一个 Zentiy TUI 来将数据插入数据库。
#!/bin/bash
tip_run1="$(zenity --entry --text "ENTER number of tip runs:" --entry-text "1")"
a=1
until
[[ $tip_run1 -lt $a ]]
do
input="$(zenity --forms --title="table tip_run" --text="Add a new tip run" --separator="," \
--add-entry="ENTER start_time, e.g. 8:20: " \
--add-entry="ENTER finish_time, e.g. 12:30: " \
--add-entry="ENTER weight in kg, -t numeric: "\
--add-entry="ENTER a note, -t text: ")"
psql -tA -U chh1 -d crewdb -c "SELECT SETVAL('tip_run_tip_runid_seq', (SELECT MAX(tip_runid) FROM tip_run), true);" >/dev/null 2>&1
startt="$(echo "$input" | awk -F, -v OFS=, '{print $1}')"
finisht="$(echo "$input" | awk -F, -v OFS=, '{print $2}')"
st="$( date --date="$startt" +%s 2>/dev/null )"
ft="$( date --date="$finisht" +%s 2>/dev/null )"
if [ -n "$st" -a "$ft" ] ; then
startt="$(date +%H:%M -d "$startt" )"
finisht="$(date +%H:%M -d "$finisht" )"
tzdiff="$(( ft - st ))"
else
tzdiff=0
fi
while [[ ( ( ! "$startt" =~ ^[0-1][0-9]:[0-5][0-9]$ ) && ( ! "$startt" =~ ^[0-2][0-3]:[0-5][0-9]$ ) ) ||
( ( ! "$finisht" =~ ^[0-1][0-9]:[0-5][0-9]$ ) && ( ! "$finisht" =~ ^[0-2][0-3]:[0-5][0-9]$ ) ) ||
( "$tzdiff" -le 0 ) ]];
do
var2="$(zenity --forms --title="start_time and/or finish_time are incorrect" --text "Add a start_time and a finish_time" --separator="," \
--add-entry="WARNING! Something went wrong. Please enter a valid start_time, e.g. 8:20: " \
--add-entry="WARNING! Something went wrong. Please enter a valid finish_time, e.g. 12:30: ")"
tzdiff=0
if [ -n "$var2" ] ; then
b1=$(echo "$var2" | cut -d, -f1 )
b2=$(echo "$var2" | cut -d, -f2 )
if [ -n "$b1" -a -n "$b2" ] ; then
tz1=$( date --date="$b1" +%s 2>/dev/null )
tz2=$( date --date="$b2" +%s 2>/dev/null )
if [ -n "$tz1" -a -n "$tz2" ] ; then
startt=$(date +%H:%M -d "$b1" )
finisht=$(date +%H:%M -d "$b2" )
tzdiff=$(( tz2 - tz1 ))
fi
fi
fi
done
var2="$startt,$finisht"
input="$( echo "$input" | awk -v vart="$var2" 'BEGIN { FS="," } { print vart "," $3 "," $4 ; }' )"
input="$((IFS=, read -r b c d e ; echo "${b}ttt,${c}ttt,${d}xxx,${e}www" )<<<"$input")"
IFS=,; set -f; set --$input; out=
for i in "$@"; do
case "$i" in
xxx) var2="$(zenity --forms --title="weight field in table tip_run" --text "Add a weight in kg" --separator="," \
--add-entry="WARNING! You forgot to enter a weight. Please enter a valid weight, e.g. 12.5: ")"
until [[ ${var2} =~ ^[0-9]+([.][0-9]+)?$ ]] || [[ ${var2} = NULL ]]; do
var2="$(zenity --forms --title="weight field in table tip_run" --text "Add a weight in kg" --separator="," \
--add-entry="WARNING! You either forgot to enter a weight or didn't enter a number. Please enter a valid weight, e.g. 12.5: ")"
done
out="$out,${var2}"
;;
NULLxxx) out="$out,${i/%xxx/}";;
*xxx) if [[ "${i/%xxx}" =~ ^[0-9]+([.][0-9]+)?$ ]]; then
out="$out,${i/%xxx/}"
else
until [[ ${var2} =~ ^[0-9]+([.][0-9]+)?$ ]] || [[ ${var2} = NULL ]]; do
var2="$(zenity --forms --title="weight field in table tip_run" --text "Add a weight in kg" --separator="," \
--add-entry="WARNING! You either forgot to enter a weight or didn't enter a number. Please enter a valid weight, e.g. 12.5: ")"
done
out="$out,${var2}"
fi
;;
*ttt) out="$out,'${i/%ttt/}:00'";;
#TRUEbool) out="$out,${i/%bool/}";;
#FALSEbool) out="$out,${i/%bool/}";;
#*bool) echo "empty input not allowed"; exit 0;;
NULLwww) out="$out,${i/%www/}";;
www) var2="$(zenity --forms --title="note field in table tip_run" --text "Add a note" --separator="," \
--add-entry="WARNING! You either forgot to enter a note. Please enter a note or NULL: ")"
until [[ ! ${var2} = "" ]]; do
var2="$(zenity --forms --title="note field in table tip_run" --text "Add a note" --separator="," \
--add-entry="WARNING! You either forgot to enter a note or to enter NULL. Please enter a note or NULL: ")"
done
if [[ ${var2} = "NULL" ]]; then
out="$out,${var2}"
else
out="$out,\$\$${var2}\$\$"
fi;;
*www) out="$out,\$\$${i/%www/}\$\$";;
esac;
done
#echo "${out:1}"
echo "" >> /tmp/crew.txt
echo "" >> /tmp/crew.txt
echo "-- INSERT INTO tip_run:" >> /tmp/crew.txt
echo "INSERT INTO tip_run (date_linkid, start_time, finish_time, weight, note) VALUES (${out:1});" >> /tmp/crew.txt
let a++
done
我知道脚本的各个组件都可以工作,但是当我运行它时,我收到以下错误消息:
./tip_run.txt: line 64: set: --: invalid option
set: usage: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]
第 64 行位于以下位置:
62 input="$((IFS=, read -r b c d e ; echo "${b}ttt,${c}ttt,${d}xxx,${e}www" )< <<"$input")"
63
64 IFS=,; set -f; set --$input; out=
65 for i in "$@"; do
66
67 case "$i" in
我尝试了 set 的不同选项,但似乎都没有。任何人都可以让我走上正确的轨道吗?