Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.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
Linux 意外标记“then';_Linux_Bash - Fatal编程技术网

Linux 意外标记“then';

Linux 意外标记“then';,linux,bash,Linux,Bash,我创建了一个脚本,帮助管理员从文本文件中添加和删除用户,而不是手动执行。我遇到了这个错误,我很难解决它 [ec2-user@ip-172-31-15-55 ~]$ ./account-manager user add ./account-manager: line 21: syntax error near unexpected token `then' ./account-manager: line 21: ` then' 如何修复此错误 #!/bin/bash file=$1 act

我创建了一个脚本,帮助管理员从文本文件中添加和删除用户,而不是手动执行。我遇到了这个错误,我很难解决它

[ec2-user@ip-172-31-15-55 ~]$ ./account-manager user add
./account-manager: line 21: syntax error near unexpected token `then'
./account-manager: line 21: `   then'
如何修复此错误

#!/bin/bash

file=$1
action=$2

if [ -z "$file" ]
then
        echo " Please enter a file with your users"
        exit 0
fi

if [ -z "$action" ]
then
        echo " Please define an account to remove or deleete"
        exit 0
fi

for user in `cat $file`
do
        if[ "$action" == "add" ]
        then
                echo adding user: $user
                useradd $user -m -p password
        fi
        if[ "$action" == "remove" ]
        then
                echo removing user: $user
                userdel -r $user
        fi

done

注意你的
if
语句周围的空格,使用一个工具来理解你可能犯错误的地方。

你应该在第20行和第25行的
if
[
之间加一个空格。你的代码应该是这样的

#!/bin/bash

file=$1
action=$2

if [ -z "$file" ]
then
        echo " Please enter a file with your users"
        exit 0
fi

if [ -z "$action" ]
then
        echo " Please define an account to remove or deleete"
        exit 0
fi

for user in `cat $file`
do
        if [ "$action" == "add" ]
        then
                echo adding user: $user
                useradd $user -m -p password
        fi
        if [ "$action" == "remove" ]
        then
                echo removing user: $user
                userdel -r $user
        fi

done

如果[
不一样,如果[
(注意空格)谢谢你的帮助,把答案贴出来,我会接受你的one@MatSee了解在
bash
中迭代文件内容的正确方法。