Delete Users From List
up vote
0
down vote
favorite
I'm working on a script to delete users from a .txt file but give the option to delete as a final failsafe.
The text file is called badUsers.txt
I'm basically modifying this script which I know works:
cat badUsers.txt | while read user
echo "Deleting: ${user}"
echo userdel -r "${user}"
done
This is the new version:
while true; do
read -p "Should ${user} be deleted from the system? : " yn
case $yn in
[Yy]* ) echo "Deleting: ${user}"
echo userdel -r "${user}"
break;;
[Nn]* ) echo "next user"
break;;
* ) echo "Please answer yes or no.";;
esac
done
Everytime I run it, I keep getting an infinite loop but I'm not sure why. What am I doing wrong here?
bash scripts
add a comment |
up vote
0
down vote
favorite
I'm working on a script to delete users from a .txt file but give the option to delete as a final failsafe.
The text file is called badUsers.txt
I'm basically modifying this script which I know works:
cat badUsers.txt | while read user
echo "Deleting: ${user}"
echo userdel -r "${user}"
done
This is the new version:
while true; do
read -p "Should ${user} be deleted from the system? : " yn
case $yn in
[Yy]* ) echo "Deleting: ${user}"
echo userdel -r "${user}"
break;;
[Nn]* ) echo "next user"
break;;
* ) echo "Please answer yes or no.";;
esac
done
Everytime I run it, I keep getting an infinite loop but I'm not sure why. What am I doing wrong here?
bash scripts
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm working on a script to delete users from a .txt file but give the option to delete as a final failsafe.
The text file is called badUsers.txt
I'm basically modifying this script which I know works:
cat badUsers.txt | while read user
echo "Deleting: ${user}"
echo userdel -r "${user}"
done
This is the new version:
while true; do
read -p "Should ${user} be deleted from the system? : " yn
case $yn in
[Yy]* ) echo "Deleting: ${user}"
echo userdel -r "${user}"
break;;
[Nn]* ) echo "next user"
break;;
* ) echo "Please answer yes or no.";;
esac
done
Everytime I run it, I keep getting an infinite loop but I'm not sure why. What am I doing wrong here?
bash scripts
I'm working on a script to delete users from a .txt file but give the option to delete as a final failsafe.
The text file is called badUsers.txt
I'm basically modifying this script which I know works:
cat badUsers.txt | while read user
echo "Deleting: ${user}"
echo userdel -r "${user}"
done
This is the new version:
while true; do
read -p "Should ${user} be deleted from the system? : " yn
case $yn in
[Yy]* ) echo "Deleting: ${user}"
echo userdel -r "${user}"
break;;
[Nn]* ) echo "next user"
break;;
* ) echo "Please answer yes or no.";;
esac
done
Everytime I run it, I keep getting an infinite loop but I'm not sure why. What am I doing wrong here?
bash scripts
bash scripts
asked Nov 12 at 5:58
Daniel
1
1
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
There are 3 things you need to concern yourself with:
- The looping construct for iterating over the list of names
- The method for determining whether a user should be deleted
- The user deletion
In pseudo-code, the algorithm would be something like the following.
for each name N in the input file
ask if N should be interpreted as a user and deleted; wait for a reply R
if R is yes delete the user else skip
With respect to (3), we can reuse your approach. I have corrected to remove the echo as that prevents the user deletion.
With respect to (2), there are at least 2 ways.
read -p "Should ${user} be deleted? (yN)" -r yn
case $yn in
...
esac
and
echo Should ${user} be deleted?
select yn in "Y" "N"
do
case $yn in
...
done
done
With respect to (1), there are at least 2 methods.
for user in $(<badUsers.txt)
do
...
done
while read -r user
do
...
done < badUsers.txt
The last method is similar to the cat construct you use above, but is preferred as it allows for file descriptor redirection. If we prefer the methods used in the example you provide, we get something like the following.
while read -u 3 -r user
do
read -p "Should ${user} be deleted from the system? (yN)" -r yn
case $yn in
[Yy]) echo "Deleting: ${user}"
userdel -r -- "${user}" 2> /dev/null
;;
*) echo next user
;;
esac
done 3< badUsers.txt
exit 0
As you may already know, read redirects the file to standard in, which is numbered 0. The nested read, used to prompt the user, inherits this redirected file descriptor. This means the read with the prompt will take its input from 0, which is now not the terminal, but a file.
To avoid this problem it is better redirect the file to descriptor 3, and configure the outer read to use descriptor 3 as its input.
Thanks for your comment! I tried running it the third way you showed and I get an error: ./automatic_user_management.sh: 32: read: Illegal option -u
– Daniel
Nov 12 at 19:48
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1092127%2fdelete-users-from-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
There are 3 things you need to concern yourself with:
- The looping construct for iterating over the list of names
- The method for determining whether a user should be deleted
- The user deletion
In pseudo-code, the algorithm would be something like the following.
for each name N in the input file
ask if N should be interpreted as a user and deleted; wait for a reply R
if R is yes delete the user else skip
With respect to (3), we can reuse your approach. I have corrected to remove the echo as that prevents the user deletion.
With respect to (2), there are at least 2 ways.
read -p "Should ${user} be deleted? (yN)" -r yn
case $yn in
...
esac
and
echo Should ${user} be deleted?
select yn in "Y" "N"
do
case $yn in
...
done
done
With respect to (1), there are at least 2 methods.
for user in $(<badUsers.txt)
do
...
done
while read -r user
do
...
done < badUsers.txt
The last method is similar to the cat construct you use above, but is preferred as it allows for file descriptor redirection. If we prefer the methods used in the example you provide, we get something like the following.
while read -u 3 -r user
do
read -p "Should ${user} be deleted from the system? (yN)" -r yn
case $yn in
[Yy]) echo "Deleting: ${user}"
userdel -r -- "${user}" 2> /dev/null
;;
*) echo next user
;;
esac
done 3< badUsers.txt
exit 0
As you may already know, read redirects the file to standard in, which is numbered 0. The nested read, used to prompt the user, inherits this redirected file descriptor. This means the read with the prompt will take its input from 0, which is now not the terminal, but a file.
To avoid this problem it is better redirect the file to descriptor 3, and configure the outer read to use descriptor 3 as its input.
Thanks for your comment! I tried running it the third way you showed and I get an error: ./automatic_user_management.sh: 32: read: Illegal option -u
– Daniel
Nov 12 at 19:48
add a comment |
up vote
1
down vote
There are 3 things you need to concern yourself with:
- The looping construct for iterating over the list of names
- The method for determining whether a user should be deleted
- The user deletion
In pseudo-code, the algorithm would be something like the following.
for each name N in the input file
ask if N should be interpreted as a user and deleted; wait for a reply R
if R is yes delete the user else skip
With respect to (3), we can reuse your approach. I have corrected to remove the echo as that prevents the user deletion.
With respect to (2), there are at least 2 ways.
read -p "Should ${user} be deleted? (yN)" -r yn
case $yn in
...
esac
and
echo Should ${user} be deleted?
select yn in "Y" "N"
do
case $yn in
...
done
done
With respect to (1), there are at least 2 methods.
for user in $(<badUsers.txt)
do
...
done
while read -r user
do
...
done < badUsers.txt
The last method is similar to the cat construct you use above, but is preferred as it allows for file descriptor redirection. If we prefer the methods used in the example you provide, we get something like the following.
while read -u 3 -r user
do
read -p "Should ${user} be deleted from the system? (yN)" -r yn
case $yn in
[Yy]) echo "Deleting: ${user}"
userdel -r -- "${user}" 2> /dev/null
;;
*) echo next user
;;
esac
done 3< badUsers.txt
exit 0
As you may already know, read redirects the file to standard in, which is numbered 0. The nested read, used to prompt the user, inherits this redirected file descriptor. This means the read with the prompt will take its input from 0, which is now not the terminal, but a file.
To avoid this problem it is better redirect the file to descriptor 3, and configure the outer read to use descriptor 3 as its input.
Thanks for your comment! I tried running it the third way you showed and I get an error: ./automatic_user_management.sh: 32: read: Illegal option -u
– Daniel
Nov 12 at 19:48
add a comment |
up vote
1
down vote
up vote
1
down vote
There are 3 things you need to concern yourself with:
- The looping construct for iterating over the list of names
- The method for determining whether a user should be deleted
- The user deletion
In pseudo-code, the algorithm would be something like the following.
for each name N in the input file
ask if N should be interpreted as a user and deleted; wait for a reply R
if R is yes delete the user else skip
With respect to (3), we can reuse your approach. I have corrected to remove the echo as that prevents the user deletion.
With respect to (2), there are at least 2 ways.
read -p "Should ${user} be deleted? (yN)" -r yn
case $yn in
...
esac
and
echo Should ${user} be deleted?
select yn in "Y" "N"
do
case $yn in
...
done
done
With respect to (1), there are at least 2 methods.
for user in $(<badUsers.txt)
do
...
done
while read -r user
do
...
done < badUsers.txt
The last method is similar to the cat construct you use above, but is preferred as it allows for file descriptor redirection. If we prefer the methods used in the example you provide, we get something like the following.
while read -u 3 -r user
do
read -p "Should ${user} be deleted from the system? (yN)" -r yn
case $yn in
[Yy]) echo "Deleting: ${user}"
userdel -r -- "${user}" 2> /dev/null
;;
*) echo next user
;;
esac
done 3< badUsers.txt
exit 0
As you may already know, read redirects the file to standard in, which is numbered 0. The nested read, used to prompt the user, inherits this redirected file descriptor. This means the read with the prompt will take its input from 0, which is now not the terminal, but a file.
To avoid this problem it is better redirect the file to descriptor 3, and configure the outer read to use descriptor 3 as its input.
There are 3 things you need to concern yourself with:
- The looping construct for iterating over the list of names
- The method for determining whether a user should be deleted
- The user deletion
In pseudo-code, the algorithm would be something like the following.
for each name N in the input file
ask if N should be interpreted as a user and deleted; wait for a reply R
if R is yes delete the user else skip
With respect to (3), we can reuse your approach. I have corrected to remove the echo as that prevents the user deletion.
With respect to (2), there are at least 2 ways.
read -p "Should ${user} be deleted? (yN)" -r yn
case $yn in
...
esac
and
echo Should ${user} be deleted?
select yn in "Y" "N"
do
case $yn in
...
done
done
With respect to (1), there are at least 2 methods.
for user in $(<badUsers.txt)
do
...
done
while read -r user
do
...
done < badUsers.txt
The last method is similar to the cat construct you use above, but is preferred as it allows for file descriptor redirection. If we prefer the methods used in the example you provide, we get something like the following.
while read -u 3 -r user
do
read -p "Should ${user} be deleted from the system? (yN)" -r yn
case $yn in
[Yy]) echo "Deleting: ${user}"
userdel -r -- "${user}" 2> /dev/null
;;
*) echo next user
;;
esac
done 3< badUsers.txt
exit 0
As you may already know, read redirects the file to standard in, which is numbered 0. The nested read, used to prompt the user, inherits this redirected file descriptor. This means the read with the prompt will take its input from 0, which is now not the terminal, but a file.
To avoid this problem it is better redirect the file to descriptor 3, and configure the outer read to use descriptor 3 as its input.
edited Nov 12 at 8:42
answered Nov 12 at 7:52
forty-lanterns
215
215
Thanks for your comment! I tried running it the third way you showed and I get an error: ./automatic_user_management.sh: 32: read: Illegal option -u
– Daniel
Nov 12 at 19:48
add a comment |
Thanks for your comment! I tried running it the third way you showed and I get an error: ./automatic_user_management.sh: 32: read: Illegal option -u
– Daniel
Nov 12 at 19:48
Thanks for your comment! I tried running it the third way you showed and I get an error: ./automatic_user_management.sh: 32: read: Illegal option -u
– Daniel
Nov 12 at 19:48
Thanks for your comment! I tried running it the third way you showed and I get an error: ./automatic_user_management.sh: 32: read: Illegal option -u
– Daniel
Nov 12 at 19:48
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1092127%2fdelete-users-from-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown