Quote:
sed -n -e "$n"d $PASS_WRK #delete line $n #
Looks like $n should actually be $N, sed being case-sensitive, and all

........Also, nothing is happening with sed, since you're suppressing the output (-n) and not having any way for sed to update the file..........And, the -e switch is not necesary if you enclose the entire "delete" action in quotes.......
Therefore, replace the
-n -e switches with
-i to tell sed to make the changes directly on the file (
in place) and enclose the whole "delete" action in quotes, not just the variable ("${N}d").....You will need to use the curly braces around the variable name to let bash know that 'd' is not part of the name, or simply add a space in between ("$N d")....
Here's a much simpler approach in the
for loop, but maybe not as obvious:
Code:
#root check
#
if [ "$USER" != "$ROOT" ]; then #this section works#
echo -e "\nOnly root can run this script.\n"
exit $E_WRONG_USER #this section works#
fi #this section works#
cp $PASSWORD_FILE $PASS_WRK #make a copy of passwd (passwd.wrk) file to manipulate and clean up;
rm -f $PASS_LOG && touch $PASS_LOG # remove the old log and create an empty one in it's place
for NAME in $(cut -d: -f1 "$PASSWORD_FILE" ) #read names in from passwd.wrk file
do
## If NAME exists in either list, continue on to the next name:
grep -q "\<$NAME\>" "$BASE_FILE" #grep virtusertable for $NAME
if [ $? -eq 0 ]; then # If NAME exists, increment counter and
let N++ && continue # skip the rest of the loop
fi
grep -q "\<$NAME\>" "$VIRT_FILE" #grep base users for $NAME
if [ $? -eq 0 ]; then # If NAME exists, increment counter and
let N++ && continue # skip the rest of the loop
fi
## If no name was found, delete it from the passwd file:
sed -i "${N}d" $PASS_WRK #delete line $n #
echo $NAME >> $PASS_LOG #log of users deleted#
let N++ #n=$n+1 #
done
For the root user check, I wasn't sure what you were trying to accomplish with the
else keyword in the
if statement, but it's not needed unless the 'echo' command was absolutely necessary for something.......

Instead of trying to empty the log file by reading nothing into it (cat /dev/null), why not simply delete it and create an empty log, instead.......

In the loop itself, all those
if statements are also unnecessary (and a bit confusing to look at) if all you're doing is checking to see whether the names exist in either file.........If a name is found, you simply increment the counter and then issue the
continue command to skip the rest of the loop and start on the next name in the list(s)......
And
awk is not necessary and a bit of overkill in this instance..........
cut would be more appropriate.......
When incrementing the N counter (
N=$(($N+1))), it's much simpler and easier to do it with 'let':
let N++.....Notice there is no dollar sign ($) in front of the variable name when using the
let command...
And if you wanted to speed things up a bit, you can load both user files (base and virtuser) into memory by reading them into variables, using cat, and grepping from those variables.............Reading from memory is much faster than reading from disk for each and every loop:
Code:
VIRT_NAME="$(cat $VIRT_FILE)"
.....blah-blah.....
grep -q "\<$NAME\>" <<<"$VIRT_NAME"
HTH.....
---thegeekster