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
中迭代文件内容的正确方法。