Reputation: 3
I seem to be having trouble with a piece of code I'm writing (at least I think anyways). It's a script that parses a text file called test3_data.txt with a bunch of user ID's in them, and puts those user Id's into a variable called "USER_ID". I then test to see if they have a home directory with whatever text is in the USER_ID variable. If so, it deletes the user from the system, along with their files, home directory, etc. The script works fine, but when it's done running, the man pages for the userdel command show up. Is there anyway to stop that from happening? Is their something wrong with my code? Thanks! and here is my code.
!/bin/sh
while read IN_RECORD
do
#Variable that reads user id
USER_ID=`echo $IN_RECORD |cut -d'|' -f1`
#Remove added users from data file.
test -d /home/{$USER_ID}
if [ "$?" = "1" ];
then
userdel -r $USER_ID
fi
done < test3_data.txt
Upvotes: 0
Views: 118
Reputation: 438028
A number of things are wrong with your script:
The shebang line should read #!/bin/sh
(missing #
), or #!/usr/bin/env sh
The variable reference {$USER_ID}
is not what you intended - ${USER_ID}
is the correct form (typically, just $USER_ID
is enough).
You're incorrectly testing for success: it is exit code 0, not 1 that indicates success. Also, in a bash script you should use [[
instead of the deprecated [
for tests. (*)
One possible explanation for the command-line help (not the man page) showing is that $USER_ID
is empty - the resulting invalid syntax then causes userdel
utility to show its command-line help.
Finally, there are ways of streamlining your script:
A statement-ending ;
is only needed if you place more statements on the same line.
Your test could be combined into a single statement: if [ -d "/home/$USER_ID" ]; then
(*): As @thom points out, [
and test
still have their place, if your script must remain POSIX-compliant. By contrast, in a bash-specific script, [[
is preferable, because it is more convenient to use and has more features than [
- see http://mywiki.wooledge.org/BashFAQ/031.
Note that the OP's shebang line specified sh
- which may or may not result in processing by bash, depending on the platform.
Upvotes: 5