1

When running a bash script i get the following error:

./hi.sh: line 19: syntax error: unexpected end of file

Here is my code:

#!/bin/bash

#Remove unauthorized users

for line in ./unauthorized_users.txt; do

sudo userdel $line -y

sudo rm -rf "/home/$line" -y


#Remove unauthorized packages

for line in ./bad_packs.txt; do

sudo apt-get remove $line -y

sudo apt-get purge $line -y

Please advise me on what to do.

4
  • 5
    Is that the entire script? You are not closing your for loops with done. Commented Dec 1, 2017 at 20:59
  • 3
    Please take a look: shellcheck.net Commented Dec 1, 2017 at 21:01
  • 3
    It might not be a good idea to play with a script that you don't know exactly what it does and sudo... Commented Dec 1, 2017 at 21:17
  • Also unrelated, please, please check that $line is not empty ([ "$line" ]), before you do sudo rm -rf "/home/$line" -y. Commented Oct 9, 2019 at 5:47

2 Answers 2

5

You are not closing your for loop with done. Check manual for looping constructs' syntax:

for bar in 1 2 3; do
   echo "$bar"
done

Also note that your code won't iterate over lines of the file ./unauthorized_users.txt. To read lines from a file, use while loop instead :

while IFS= read -r line; do
   echo "$line"
done < your_file
Sign up to request clarification or add additional context in comments.

Comments

1

Both 'for' loops need closing 'done's

for line in file; do
    echo "$line" # do some stuff
done

Other bash constructs (if, case, etc.) need closing statements as well

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.