1

I am creating a bash script to delete a user account on a mac.

When I pull up terminal and type the commands

sudo dscl . delete /users/username
sudo rm -rf /users/username

It is successful in deleting the desired account and removing the user files.

However, when I create a bash script to do so...

#!/bin/bash
sudo dscl . delete /users/username
sudo rm -rf /users/username

I get the error

delete: Invalid Path
<dscl_cmd> DS Error: -14009 (eDSUnknownNodeName)

Very strange behavior I haven't been able to figure out, although it is likely a simple mistake.

Edit: In my original script, I was doing looping among other things, but for this post I simplified the problem down to a couple of commands wrapped in a bash script.

Thanks for any help.

spencer741
  • 965
  • 1
  • 10
  • 22
  • 2
    Check your script file for stray characters (I would suggest that Windows line endings might be the problem, but you're on a Mac). – Dennis Williamson Aug 09 '19 at 20:51
  • @DennisWilliamson I have created an entire new file and re-typed it verbatim, making sure there aren't any stray characters. Sadly, I am getting the same results. – spencer741 Aug 09 '19 at 21:18
  • 1
    On my Mac, the directory is `/Users/username` (capital U). – Dennis Williamson Aug 09 '19 at 21:21
  • 2
    The bash script syntax is correct, I tried your command inside bash script and it worked here. Are you sure you are not trying to delete a user that was already deleted or does not exist? Try to check for the user existence using `sudo dscl . list /users` – Prado Aug 09 '19 at 23:21

1 Answers1

1

I thought I would come back and answer this for anybody who is trying to delete a user profile on macs. The original issue I was having was due to some weird path anomaly local to my machine, but here was that final script. As you can see, I have a hard-coded exclusion list built in for profiles I don't want to delete.

This could be improved upon, but is a basic way to do it.

#!/bin/bash
for d in /users/*/;
do
    if [ "$d" != "/users/exclusionprofile1/" ] && [ "$d" != "/users/exclusionprofile2/" ]; then
           echo "$d"
           sudo rm -rf "$d"
    fi
done
spencer741
  • 965
  • 1
  • 10
  • 22