AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题

问题[zenity](computer)

Martin Hope
Christian Hick
Asked: 2020-07-07 13:28:38 +0800 CST

用于数据库插入语句的 Bash 脚本 TUI 不起作用。设置 bash 内置错误消息

  • 6

我有以下 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 的不同选项,但似乎都没有。任何人都可以让我走上正确的轨道吗?

bash zenity
  • 2 个回答
  • 112 Views
Martin Hope
Christian Hick
Asked: 2020-06-29 16:27:05 +0800 CST

通过 zenity 验证两个时间值的 Bash 脚本不起作用

  • 5

我正在尝试验证程序以 $input 的形式传递的两个时间值。一旦验证,时间值将用于 SQL 插入语句。如果值超出范围,我将无法使用 date 命令,因为我收到一条错误消息。

必须将时间值传递给数据库,例如 xx:xx,因此 08:20 不能作为 8:20 传递,并且必须在 00:00 到 23:59 的有效范围内。我拆分了 $input 并通过 awk 导出了两个时间值 $startt 和 $finisht。$finisht 必须大于 $startt。

如果不满足之前的标准,我想打开带有两个时间字段的 Zenity 输入框,直到输入正确的标准。

到目前为止,我有以下 Bash 脚本,但它不起作用。有人可以帮忙吗?

#!/bin/bash

input=30:20,12:45

startt=$(echo $input | awk -F, -v  OFS=, '{print $1}')
finisht=$(echo $input | awk -F, -v  OFS=, '{print $2}')

st=`date --date="$startt" +%s`
ft=`date --date="$finisht" +%s`

let "tDiff=$ft-$st" 

if [[ ! $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] ]] || [[ "$tDiff" -le 0 ]];
then
                                until [[ $b1 =~ [0-1][0-9]:[0-5][0-9] ]] || [[ ! $b1 =~ [0-2][0-3]:[0-5][0-9] ]] && [[ ! $b2 =~ [0-1][0-9]:[0-5][0-9] ]] \
                                        || [[ $b2 =~ [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: " \
                                      --add-entry="WARNING! Something went wrong. Please enter a valid finish_time: ")"

                                b1=$(echo $var2 | awk -F, -v  OFS=, '{print $1}')
                                b2=$(echo $var2 | awk -F, -v  OFS=, '{print $2}')

                                tz1=`date --date="$b1" +%s`
                                tz2=`date --date="$b2" +%s`
                                let "tzDiff=$tz2-$tz1"

                                done

fi

echo $var2

bash zenity
  • 1 个回答
  • 248 Views
Martin Hope
Christian Hick
Asked: 2020-06-24 14:36:51 +0800 CST

Bash until 循环也保持时间范围有效不起作用

  • 5

我在执行以下直到循环时遇到问题bash。只要输入的时间值超出有效范围,我就希望出现 Zenity 弹出框。弹出窗口打开得很好。但是,无论是否输入了有效数据,它都会不断弹出(我必须退出终端才能跳出循环)。

#!/bin/bash

startt=30:20
finisht=12:45

until [[ $startt =~ [0-2][0-9]:[0-5][0-9] ]] && [[ $finisht =~ [0-2][0-9]:[0-5][0-9] ]]; 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: " \
           --add-entry="WARNING! Something went wrong. Please enter a valid finish_time: ")"
done

如果有人可以帮助解决这个问题,我们将不胜感激。

bash zenity
  • 1 个回答
  • 113 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Vickel Firefox 不再允许粘贴到 WhatsApp 网页中? 2023-08-18 05:04:35 +0800 CST
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve