Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 意外标记“elif';”附近出现语法错误已经读过其他帖子了_Bash_If Statement_Syntax Error_Whitespace - Fatal编程技术网

Bash 意外标记“elif';”附近出现语法错误已经读过其他帖子了

Bash 意外标记“elif';”附近出现语法错误已经读过其他帖子了,bash,if-statement,syntax-error,whitespace,Bash,If Statement,Syntax Error,Whitespace,首先,我已经在这里列出的其他评论中查看了前面的评论,但不幸的是,提供的任何帮助都没有解决我的问题 我在CentOS 7中作为我的环境工作,我正在将一些错误处理编码到我的添加用户脚本中 #! /bin/bash echo -n "Enter username: " read -r username /bin/egrep -i "^${username}:" /etc/passwd if [ $? -eq 0 ] echo "User $username already exists. Please

首先,我已经在这里列出的其他评论中查看了前面的评论,但不幸的是,提供的任何帮助都没有解决我的问题

我在CentOS 7中作为我的环境工作,我正在将一些错误处理编码到我的添加用户脚本中

#! /bin/bash
echo -n "Enter username: "
read -r username
/bin/egrep -i "^${username}:" /etc/passwd
if [ $? -eq 0 ]
echo "User $username already exists. Please check the username and try again."
elif [ $? eq 1 ]
echo "User $username does not exist. Please proceed with account creation."
then
adduser "$username"
echo -n "Enter password: "
read -r -s password
echo $username:$password | chpasswd
touch /etc/sudoers.d/sugroup
chmod 0440 /etc/sudoers.d/sugroup
echo "$username ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers.d/sugroup
else
echo "Error encountered."
fi
当我去测试它时,我得到以下错误消息:

./testscript-error.sh line 7: syntax error near unexpected token 'elif'
./testscript-error.sh line 7: elif [ $? eq 1 ]
我试过:

elif [ $? eq 1 ]**;**
echo "User $username does not exist. Please proceed with account creation."
then
adduser "$username"
echo -n "Enter password: "
read -r -s password
echo $username:$password | chpasswd
touch /etc/sudoers.d/sugroup
chmod 0440 /etc/sudoers.d/sugroup
echo "$username ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers.d/sugroup**;**
我也试过:

elif [ $? eq 1 ]
then
echo "User $username does not exist. Please proceed with account creation."
then
adduser "$username"
echo -n "Enter password: "
read -r -s password
echo $username:$password | chpasswd
touch /etc/sudoers.d/sugroup
chmod 0440 /etc/sudoers.d/sugroup
echo "$username ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers.d/sugroup

同样的结果。不确定我遗漏了什么,可以用另一双眼睛来观察。

给你。。。我希望您能更好地理解语法和用法:

#!/bin/bash

while true; do
    echo -n "Enter username: "
    read -r username
    /bin/egrep -i "^${username}:" /etc/passwd
    if [ $? -eq 0 ]; then
        echo "User $username already exists. Please check the username and try again."
    else
        echo "User $username does not exist. Proceed with account creation."
        break
    fi
done

adduser "$username"
if [ $? -gt 0 ]; then
    echo "Error encountered."
    exit 1
fi

echo -n "Enter password: "
read -r -s password
echo "$username:$password" | chpasswd
if [ $? -gt 0 ]; then
    echo "Error encountered."
    exit 1
fi

touch /etc/sudoers.d/sugroup
chmod 0440 /etc/sudoers.d/sugroup
echo "$username ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers.d/sugroup
if [ $? -gt 0 ]; then
    echo "Error encountered."
    exit 1
fi

这是完成的工作代码

#!/bin/bash
#========================================================================================================
# This script allows for account creation on a server                                                   |
# It also performs error handling to ensure that the user doesn't currently exist on the system.        |
# Also provides feedback from the input to verify the entries are correct.                              |
#========================================================================================================
while true; do
    echo -n "Enter username: "
    read -r username
    /bin/egrep -i "^${username}:" /etc/passwd
    if [ $? -eq 0 ]; then
        echo "User $username already exists. Please check the username and try again."
    else
        echo "User $username does not exist. Proceed with account creation."
        break
    fi
done

adduser "$username"
if [ $? -gt 0 ]; then
    echo "Error encountered."
    exit 1
fi

echo -n "Enter password: "
read -r -s password
echo "$username:$password" | chpasswd
echo "Password was succesfully set for $username."
if [ $? -gt 0 ]; then
    echo "Error encountered. There was a problem with your entry. Please re-run the script and try again."
    exit 1
fi

usermod -a -G wheel "$username"
echo "User was succesfully added to the group wheel."
if [ $? -gt 0 ]; then
    echo "Error encountered."
    exit 1
fi
echo "Successfully added $username to the system."

请看一看:如果
<0
是一个命令,但
[0
不是。
[1
也不是。当您编写
[$?
时,您正试图运行名为
[0
[1
的命令(或
$?
的任何值)。您需要一个空格:
,如果[$?…
@williampersell,我检查了我在这里输入的代码并添加了空格。它在我运行它的实际框上有空格。我已经通过shellchecker运行了它,甚至从帮助页面也无法理解为什么它会给我一个解析错误。@ladycoder2098没有关系。这仍然是问题所在。旁白:
如果[$?-gt 0]
比仅仅将
if
s合并到您想要测试其成功或失败的语句中要糟糕得多。
if!adduser“$username”;然后…
,或者更好的是,一行代码:
adduser“$username”|{echo”遇到错误“>&2;exit 1;
…或者一行代码带有函数:
die(){echo“$*”>&2;退出1;}
,然后稍后:
useradd“$username”| | die“无法添加用户”
编写此类脚本的其他方法大约有100万种。只要完成了这项工作,就没有好坏之分。基于这个问题,很明显,一个初学者根本看不到自己的错误,只需要一个简单的例子来纠正他简单的逻辑。没有复杂的语法可以做到这一点。当然有一些措施其中之一是健壮性:如果代码在修改时不太可能被破坏,那么这比它更容易被破坏要好。(例如,添加新的日志记录可能会破坏
$?
,使对其值的依赖变得脆弱)。另一个措施是重复:遵循DRY的代码(不要重复自己的代码)更容易在一个地方修改(即,在我上面给出的函数中,您可以通过仅在一个地方进行更改来更改是否将错误记录到stdout或stderr)。