1

I created a script that helps administrators add and remove users from text file instead of doing it manually. I ran into this error and i'm having trouble solving it.

[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'

How could I fix this error?

#!/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
Jone Jamson
  • 457
  • 1
  • 5
  • 6

2 Answers2

3

You should add a space between if and [ in lines 20 and 25. Your code should be like this.

#!/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
v7d8dpo4
  • 1,399
  • 8
  • 9
0

Pay attention to spacing around your if statements, make use of a tool such as http://www.shellcheck.net/ to understand where you might have made mistakes.

Julian
  • 2,837
  • 17
  • 15