我创建了一个脚本,可以帮助管理员在文本文件中添加和删除用户,而不是手动执行。我遇到了这个错误,我无法解决它。
[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
答案 0 :(得分:3)
您应该在第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
答案 1 :(得分:0)
注意if
语句的间距,使用http://www.shellcheck.net/之类的工具来了解您可能犯错的位置。