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 / 问题 / 1566596
Accepted
Christian Hick
Christian Hick
Asked: 2020-07-07 13:28:38 +0800 CST2020-07-07 13:28:38 +0800 CST 2020-07-07 13:28:38 +0800 CST

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

  • 772

我有以下 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 2 个回答
  • 112 Views

2 个回答

  • Voted
  1. Best Answer
    Kamil Maciorowski
    2020-07-08T04:11:07+08:002020-07-08T04:11:07+08:00

    问题在于set --$input,它“应该”是set -- $input。或者它根本不应该在这里。


    目的set -- $something和运作方式

    建议您使用set -- $OUTPUT. 完整的相关片段如下:

    IFS=,; set -f; set -- $OUTPUT
    for i do …
    

    注意:for i do和for i; do是for i in "$@"; do等价的。

    在那个问题中,您想使用,分隔符来拆分变量的内容。然后你想循环生成的块。您尝试使用awk. 有人指出,shell 可以做到这一点,而代码片段就是这样。让我们分析一下它是如何工作的。

    主要思想是利用未加引号的变量在扩展时经历分词的事实。通常,未引用的变量也会触发文件名生成(例如,通配符的扩展,例如*它们是否存储在变量中)。通常您既不需要生成文件名也不需要分词,因此一般规则是始终使用双引号,除非您知道自己在做什么。在少数情况下不引用是合理的set -- $something,但你需要知道你在做什么。这就是你正在做的事情:

    • 您指定IFS=,以便将来的分词视为,分隔符;
    • 通过set -f禁用文件名生成机制。

    在此之后,未引用的$something单词安全地扩展为零个或多个单词。你仍然需要遍历它们。循环遍历位置参数很容易(即,您可以使用$1, $2, ... 获得的参数;或使用$@or (不等效地)$*一次获得所有这些参数;引用很重要)。

    set是一个 shell 内置函数,可以设置或取消设置 shell 选项(例如set -f,我们已经知道)和位置参数。看起来不像选项的参数会修改位置参数数组。例如set foo bar使$1expand tofoo和$2expand to bar。

    可以将扩展的单词$something分配给位置参数,set $something但这至少在两个方面存在缺陷:

    1. 如果一个词看起来像一个选项set,它将被视为一个选项。该选项可能会或可能不会被识别为有效(受支持的)选项。
    2. 如果$something扩展到恰好零个单词,那么set $something最终将成为set不修改位置参数数组的唯一。

    第一个问题是一般性的,而不是特定于set. 许多工具(包括set)支持双破折号(--)来解决它。第二个问题是特定于set. 该工具的设计也--解决了第二个问题。我的意思是,如果$something扩展为恰好零个单词,那么set -- $something最终会set --像预期的那样修改位置参数数组:所有位置参数都将被取消设置。

    因此,这set -- $something是一种从扩展分配零个或多个单词$something到位置参数的可靠方法,然后您可以轻松循环。

    笔记:

    • 可以跳过set -- $something并开始这样的循环:

      IFS=,; set -f
      for i in $something; do
      

      IFS但是随后您使用修改后的and进入循环set -f。使用set -- $somethingand thenfor i do可以让您恢复之前for的 . 在您当前的代码中,您似乎并不关心修改后的IFS和set -f. 一般来说,他们可能会适得其反(你应该已经知道了)。

    • 在 Bash 中,您可以使用命名数组(例如myarray在这个答案中)并循环它。


    set --$something或有什么问题set --"$something"?

    如果set --$something(或set --"$something")--与扩展的第一个单词$something(或扩展的唯一单词"$something")连接。有一些边缘情况会--留下来--,但很可能会变成--whatever. 该字符串以 开头-,因此它看起来像一个或多个选项。

    关于选项的约定很少。大多数工具支持单破折号单字符选项(短选项,例如-a或-1)。许多工具支持双破折号多字符选项(长选项,例如--almost-all)。很少有工具将单破折号多字符选项视为单个选项(例如,在7zthere is-bd或中),但如果既不需要选项参数,也不需要选项参数,-bt许多工具会处理-abclike 。一般来说,一个工具可以以任何它想要的方式解析和解释它的参数,所以这可能很复杂。-a -b -c-a-b

    set --whatever不whatever视为长选项,它根本不支持长选项。它看起来set --whatever几乎像set -- -w -h -a -t -e -v -e -r,除了这--不像前面阐述的双破折号那样工作。这里--尝试作为一个短选项,一个单破折号单字符选项,其中单字符部分恰好是破折号。但是没有这么短的选择:

    set: --: invalid option
    

    --需要是一个单独的参数。--通过在您set错误地将其第一个参数解释为选项后省略空格。很有趣,关于正确使用 的要点--是指示选项的结束。


    有什么问题set -- "$something"?

    在一个(现已删除的)评论中set -- "$something"出现了这个想法,可能是因为“总是引用”规则。这扩展$something为一个单词。最初的主要目标是拆分为可能的多个单词,因此引用肯定不是正确的做法。

    让我们假设有一段时间你真的想要$something一个单词,你做到了set -- "$something",而且效果很好。在这种情况下,连续的for i in "$@"; do没有意义,因为您正在迭代单个元素。不需要循环。


    你真的需要set和for吗case?

    在您已经提到的问题中,您使用相同的代码测试了每个字段。遍历字段是个好主意。

    在当前问题中:

    • 在某些时候,您传递了$inputthrough的值,awk我强烈怀疑按照设计,之后恰好有四个逗号分隔的字段。
    • 然后你IFS=, read -r用来拆分$input为$b, $c,$d和$e; 这些是领域。
    • 然后你${b}ttt,${c}ttt,${d}xxx,${e}www又完全省钱了。
    • 然后你又把它分开了,这次用set -- $input, 所以不是 , $b,$c而是$d你$e有$1, $2, $3and $4。
    • 然后,您将遍历字段,并使用case为不同的字段运行不同的代码。我的意思是NULLxxx只能匹配第三个字段(${d}xxx)。您xxx用于识别第三个字段,但您已将其分开,$d如前所述。为什么不首先测试$d一下NULL呢?

    如果您事先不知道您有多少个字段,则拆分为位置参数很有用。如果你知道你正好有四个,那么

    IFS=, read -r  b c d e
    

    在主 shell 中将为您提供四个定义明确的单独变量以供使用。您仍然可以循环使用for i in "$b" "$c" "$d" "$e"; do,但由于仅在您的原始代码中${b}ttt并${c}ttt共享它们可以匹配 ( ) 的案例,因此循环遍历and ,然后分别解析和*ttt是有意义的。和的通用代码非常简单,您也可以在不引入循环的情况下重复它:"$b""$c""$d""$e""$b""$c"

    out="$out,'$b:00'"
    out="$out,'$c:00'"
    

    接着:

    # what to do if $d is empty
    # what to do if $d is NULL
    # what to do if $e is NULL
    # what to do if $e is empty
    # what to do if $e is valid
    # etc.
    

    如果您希望能够快速升级代码以支持需要验证的其他字段(如已支持的字段),则使用ttt或标记不同字段的方法是有意义的。xxx是这样吗?即便如此,拆分、连接和再次拆分似乎过于复杂。

    • 1
  2. DavidPostill
    2020-07-07T14:05:14+08:002020-07-07T14:05:14+08:00

    任何人都可以让我走上正确的轨道吗?

    与往常一样,对于不太工作的 bash 脚本,我通过ShellCheck - shell 脚本分析工具运行它们。

    您的脚本会生成以下错误/警告:

    $ shellcheck myscript
     
    Line 23:
    if [ -n "$st" -a "$ft" ] ; then
                  ^-- SC2166: Prefer [ p ] && [ q ] as [ p -a q ] is not well defined.
     
    Line 45:
           if [ -n "$b1" -a -n "$b2"  ] ; then
                         ^-- SC2166: Prefer [ p ] && [ q ] as [ p -a q ] is not well defined.
     
    Line 49:
           if [ -n "$tz1" -a -n "$tz2" ] ; then
                          ^-- SC2166: Prefer [ p ] && [ q ] as [ p -a q ] is not well defined.
     
    Line 62:
    input="$((IFS=, read -r  b c d e ; echo "${b}ttt,${c}ttt,${d}xxx,${e}www" )<<<"$input")"
           ^-- SC1102: Shells disambiguate $(( differently or not at all. For $(command substition), add space after $( . For $((arithmetics)), fix parsing errors.
     
    Line 64:
    IFS=,; set -f; set --$input; out=
                         ^-- SC2086: Double quote to prevent globbing and word splitting.
    
    Did you mean: (apply this, apply all SC2086)
    IFS=,; set -f; set --"$input"; out=
     
    Line 125:
    echo "" >> /tmp/crew.txt
    ^-- SC2129: Consider using { cmd1; cmd2; } >> file instead of individual redirects.
     
    Line 130:
    let a++
    ^-- SC2219: Instead of 'let expr', prefer (( expr )) .
    
    $ 
    

    我建议您仔细查看第 62 行和第 64 行的消息输出。

    请注意,如果您自己通过 ShellCheck 运行脚本,您将看到输出包含指向有关所生成的各种消息的更多信息的可单击链接。

    • 0

相关问题

  • 在非 root 用户上用 bash 替换 zsh

  • 在 macOS High Sierra 的终端中设置环境变量时遇到问题

  • 对于 cp 或 mv,是否有等同于 cd - 的东西?

  • Notify-发送窗口下出现的通知

  • 如何从 WSL 打开 office 文件

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
    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
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    fixer1234 “HTTPS Everywhere”仍然相关吗? 2019-10-27 18:06:25 +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