【问题标题】:Linux : need help in finding the syntax error (line 3)Linux:在查找语法错误时需要帮助(第 3 行)
【发布时间】:2019-12-17 10:34:15
【问题描述】:

因此,我们的想法是将所有具有用户 ID -le 500 的活动用户插入到一个 mysql 表中。

语法错误在第 3 行;我已经搜索了整个脚本,但仍然没有找到它。

这是脚本:

#!/bin/bash

for name in $(cat /etc/passwd) do

    username=`echo $name | cut -d : -f1`
    userid=`echo $name | cut -d : -f3`
    userpass=`cat /etc/shadow | grep $username | cut -d : -f2`

    if [[ "`expr $userpass`" =~ "!" ]]; then
        echo "$username is disabled, skipped
    else
        if [[ $userid -le 500 ]]; then
            echo "User id of $username is over 500, skipped"
        else
            fullname=`echo $name | cut -d : -f5`
                    "INSERT INTO`mailbox`(username,`password`,`name`,`maildir`,`quota`,`local_part`,`domain`,`created`,`modified`,active)VALUES($username,'$pass','$fullname$',0,'xxx','date --rfc3339=date','date --rfc3339=date',1)
        fi
    fi
done

【问题讨论】:

  • 我不熟悉这种编码,但通过谷歌快速搜索,看起来“f1”、“f2”、“f3”、“f5”都应该有间距。 “f 1”、“f 2”、“f 3”、“f 5”
  • 不行,我用过无数次了
  • edit你的问题,然后只写出包含语法错误的行,以免我不小心选择了错误的第3行。
  • 请用shellcheck.net检查你的脚本有很多问题,不推荐使用的代码,缺少引号在这里详细说明每个问题。一旦您处理了 ShellCheck 提出的每个问题,并且如果您仍在努力解决一些问题,请发布具体问题。我知道只有 1 个声望点你不能编辑你的帖子。
  • 谢谢大家,我解决了这个问题!原来它确实是for语句中的分号呵呵

标签: bash syntax


【解决方案1】:

这里的语法错误是do之前缺少;

for name in $(cat /etc/passwd) do

应该是

for name in $(cat /etc/passwd); do

在您添加; 之后,还要在echo "$username is disabled, skipped 中添加一个结束"。除了这些语法错误之外,您还可以在脚本中改进许多内容。我推荐https://www.shellcheck.net/

【讨论】:

  • 天哪,成功了。谢谢:),我会解决剩余的错误并修复其他问题:)
【解决方案2】:

您在第 3 行中缺少分号:

for name in $(cat /etc/passwd); do

【讨论】:

  • 顺便说一句,这个脚本还有很多其他问题:缺少双引号符号、命令之类的字符串...
猜你喜欢
  • 1970-01-01
  • 2021-10-13
  • 2016-05-25
  • 2021-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多