troubleshooting Question

Delete a Line in Passwd file

Avatar of blaztoff
blaztoff asked on
Shell ScriptingUnix OSLinux
18 Comments1 Solution446 ViewsLast Modified:
I am trying to write a function that will search the passwd file for a string. if it finds it it will prompt you to delete it. If user says yes, it will delete if no then moves on to next match. I almost have it working but for some reason it does not delete the file and actually adds a bunch of string that make up the location of the temp file to the passwd file Here is my code.  Its got to be simple of what i am doing wrong
remove(){
 
    TMP1=temp1
    TMP2=temp2
 
    cp $file $TMP1 2> /dev/null
 
 
    grep $1 $TMP1 > $TMP2 2> /dev/null
 
    #use exec and read what you found; 
    exec 5< $TMP2
 
    #Read line
    while read LINE <&5
    do
        echo $LINE
 
        promptYESNO "Delete this entry" "n"
 
        if [ $YESNO = "y" ] ; then
           grep -v $LINE $TMP1 > $TMP1.new 2>/dev/null
           mv $TMP1.new $TMP1 2> /dev/null
        fi
    done
 
 
    exec 5<&-
    mv $file $file.bak
 
    mv $TMP1 $file
 
}

Open in new window

ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 18 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 18 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros