Linux shell script to check if another user has unread mail
I'm looking to create a shell script that will accept userids as an argument to check if that user has unread mail in /var/spool/mail
.
How would I even go about checking a user's mail status? Is /var/spool/mail
only unread mail? If so, then I'm assuming I would just check for users with files of size greater than 0.
linux email
bumped to the homepage by Community♦ 1 min ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
I'm looking to create a shell script that will accept userids as an argument to check if that user has unread mail in /var/spool/mail
.
How would I even go about checking a user's mail status? Is /var/spool/mail
only unread mail? If so, then I'm assuming I would just check for users with files of size greater than 0.
linux email
bumped to the homepage by Community♦ 1 min ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
will this script be running asroot
, and does it need to be portable?
– thrig
Dec 11 '17 at 23:53
No it will not, and no it doesn't need to be portable.
– linuxnewbie
Dec 12 '17 at 0:11
you can get only the file size (unless something set a looser umask than my MTA does) but that will not tell you whether the mail has been read or not
– thrig
Dec 12 '17 at 0:58
add a comment |
I'm looking to create a shell script that will accept userids as an argument to check if that user has unread mail in /var/spool/mail
.
How would I even go about checking a user's mail status? Is /var/spool/mail
only unread mail? If so, then I'm assuming I would just check for users with files of size greater than 0.
linux email
I'm looking to create a shell script that will accept userids as an argument to check if that user has unread mail in /var/spool/mail
.
How would I even go about checking a user's mail status? Is /var/spool/mail
only unread mail? If so, then I'm assuming I would just check for users with files of size greater than 0.
linux email
linux email
edited Dec 19 '17 at 1:53
Jeff Schaller
43.2k1159138
43.2k1159138
asked Dec 11 '17 at 23:26
linuxnewbielinuxnewbie
111
111
bumped to the homepage by Community♦ 1 min ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 1 min ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
will this script be running asroot
, and does it need to be portable?
– thrig
Dec 11 '17 at 23:53
No it will not, and no it doesn't need to be portable.
– linuxnewbie
Dec 12 '17 at 0:11
you can get only the file size (unless something set a looser umask than my MTA does) but that will not tell you whether the mail has been read or not
– thrig
Dec 12 '17 at 0:58
add a comment |
will this script be running asroot
, and does it need to be portable?
– thrig
Dec 11 '17 at 23:53
No it will not, and no it doesn't need to be portable.
– linuxnewbie
Dec 12 '17 at 0:11
you can get only the file size (unless something set a looser umask than my MTA does) but that will not tell you whether the mail has been read or not
– thrig
Dec 12 '17 at 0:58
will this script be running as
root
, and does it need to be portable?– thrig
Dec 11 '17 at 23:53
will this script be running as
root
, and does it need to be portable?– thrig
Dec 11 '17 at 23:53
No it will not, and no it doesn't need to be portable.
– linuxnewbie
Dec 12 '17 at 0:11
No it will not, and no it doesn't need to be portable.
– linuxnewbie
Dec 12 '17 at 0:11
you can get only the file size (unless something set a looser umask than my MTA does) but that will not tell you whether the mail has been read or not
– thrig
Dec 12 '17 at 0:58
you can get only the file size (unless something set a looser umask than my MTA does) but that will not tell you whether the mail has been read or not
– thrig
Dec 12 '17 at 0:58
add a comment |
2 Answers
2
active
oldest
votes
You could use mailx
(formerly mail
) command with options -e
and u
:
-e Just check if mail is present in the system mailbox. If yes, return an exit status of zero, else, a non-zero value.
-u user
Reads the mailbox of the given user name.
To allow non-root user to check the other user's email you have to create a sudo
rule in the /etc/sudoers
file that will allow that user to run mailx
as root, e.g.:
fred localhost=/bin/mailx -e -u *
check man sudoers
for the complete format.
Then you just test the exit status of
sudo mailx -e -u tom
executed by fred
This looks like a great method, but I forgot to mention that I don't have root access.
– linuxnewbie
Dec 12 '17 at 1:54
@linuxnewbie what keeps you from asking administrator to set the things for you if your intents are legitimate?
– Serge
Dec 12 '17 at 2:35
because this is a script/awk assignment and i'm only allowed to work with the permissions I currently have.
– linuxnewbie
Dec 12 '17 at 2:59
add a comment |
One method that is used to find unread mail is to check the timestamps on the mail spool file. If the file was written to after it was accessed (read), i.e. its mtime
is greater than atime
, then there is unread mail:
for f in /var/spool/mail/* ; do
[ $(stat -c '%Y -gt %X' "$f") ] && echo "$f has unread mail"
done
That of course considers everything in the mailbox as "read" after it's opened, regardless of if anyone looked at the individual messages.
In many cases, read messages are also moved away from the spool directory (to ~/mbox
), so you could do with just checking the file size.
I forgot to mention I don't have root access, will this still work? Also how does this script take in the userid as an argument? I need to check specific users.
– linuxnewbie
Dec 12 '17 at 1:56
To my understand the /var/spool/mail/ directory is a temporary directory that only holds unread mail. Once it is opened it is either deleted or moved to a different directory thats dedicated to the user. So I should be able to see if the user has unread mail in there just based off of the directory size. I'm just not sure if all of this adds up lol
– linuxnewbie
Dec 12 '17 at 1:59
@linuxnewbie, you only needx
access to the directory tostat
a file, and you have that anyway, since otherwise you couldn't reach your own mail spool file. Of course, as written, it doesn't take an argument, but that's not a very hard exercise (start here: [Tests and Conditionals])mywiki.wooledge.org/BashGuide/TestsAndConditionals) and thestat(1)
man page). And sure, checking the size might be enough too. Depends on how the user works with their mailbox.
– ilkkachu
Dec 12 '17 at 7:42
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f410305%2flinux-shell-script-to-check-if-another-user-has-unread-mail%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could use mailx
(formerly mail
) command with options -e
and u
:
-e Just check if mail is present in the system mailbox. If yes, return an exit status of zero, else, a non-zero value.
-u user
Reads the mailbox of the given user name.
To allow non-root user to check the other user's email you have to create a sudo
rule in the /etc/sudoers
file that will allow that user to run mailx
as root, e.g.:
fred localhost=/bin/mailx -e -u *
check man sudoers
for the complete format.
Then you just test the exit status of
sudo mailx -e -u tom
executed by fred
This looks like a great method, but I forgot to mention that I don't have root access.
– linuxnewbie
Dec 12 '17 at 1:54
@linuxnewbie what keeps you from asking administrator to set the things for you if your intents are legitimate?
– Serge
Dec 12 '17 at 2:35
because this is a script/awk assignment and i'm only allowed to work with the permissions I currently have.
– linuxnewbie
Dec 12 '17 at 2:59
add a comment |
You could use mailx
(formerly mail
) command with options -e
and u
:
-e Just check if mail is present in the system mailbox. If yes, return an exit status of zero, else, a non-zero value.
-u user
Reads the mailbox of the given user name.
To allow non-root user to check the other user's email you have to create a sudo
rule in the /etc/sudoers
file that will allow that user to run mailx
as root, e.g.:
fred localhost=/bin/mailx -e -u *
check man sudoers
for the complete format.
Then you just test the exit status of
sudo mailx -e -u tom
executed by fred
This looks like a great method, but I forgot to mention that I don't have root access.
– linuxnewbie
Dec 12 '17 at 1:54
@linuxnewbie what keeps you from asking administrator to set the things for you if your intents are legitimate?
– Serge
Dec 12 '17 at 2:35
because this is a script/awk assignment and i'm only allowed to work with the permissions I currently have.
– linuxnewbie
Dec 12 '17 at 2:59
add a comment |
You could use mailx
(formerly mail
) command with options -e
and u
:
-e Just check if mail is present in the system mailbox. If yes, return an exit status of zero, else, a non-zero value.
-u user
Reads the mailbox of the given user name.
To allow non-root user to check the other user's email you have to create a sudo
rule in the /etc/sudoers
file that will allow that user to run mailx
as root, e.g.:
fred localhost=/bin/mailx -e -u *
check man sudoers
for the complete format.
Then you just test the exit status of
sudo mailx -e -u tom
executed by fred
You could use mailx
(formerly mail
) command with options -e
and u
:
-e Just check if mail is present in the system mailbox. If yes, return an exit status of zero, else, a non-zero value.
-u user
Reads the mailbox of the given user name.
To allow non-root user to check the other user's email you have to create a sudo
rule in the /etc/sudoers
file that will allow that user to run mailx
as root, e.g.:
fred localhost=/bin/mailx -e -u *
check man sudoers
for the complete format.
Then you just test the exit status of
sudo mailx -e -u tom
executed by fred
edited Dec 12 '17 at 1:49
answered Dec 12 '17 at 1:15
SergeSerge
5,70521325
5,70521325
This looks like a great method, but I forgot to mention that I don't have root access.
– linuxnewbie
Dec 12 '17 at 1:54
@linuxnewbie what keeps you from asking administrator to set the things for you if your intents are legitimate?
– Serge
Dec 12 '17 at 2:35
because this is a script/awk assignment and i'm only allowed to work with the permissions I currently have.
– linuxnewbie
Dec 12 '17 at 2:59
add a comment |
This looks like a great method, but I forgot to mention that I don't have root access.
– linuxnewbie
Dec 12 '17 at 1:54
@linuxnewbie what keeps you from asking administrator to set the things for you if your intents are legitimate?
– Serge
Dec 12 '17 at 2:35
because this is a script/awk assignment and i'm only allowed to work with the permissions I currently have.
– linuxnewbie
Dec 12 '17 at 2:59
This looks like a great method, but I forgot to mention that I don't have root access.
– linuxnewbie
Dec 12 '17 at 1:54
This looks like a great method, but I forgot to mention that I don't have root access.
– linuxnewbie
Dec 12 '17 at 1:54
@linuxnewbie what keeps you from asking administrator to set the things for you if your intents are legitimate?
– Serge
Dec 12 '17 at 2:35
@linuxnewbie what keeps you from asking administrator to set the things for you if your intents are legitimate?
– Serge
Dec 12 '17 at 2:35
because this is a script/awk assignment and i'm only allowed to work with the permissions I currently have.
– linuxnewbie
Dec 12 '17 at 2:59
because this is a script/awk assignment and i'm only allowed to work with the permissions I currently have.
– linuxnewbie
Dec 12 '17 at 2:59
add a comment |
One method that is used to find unread mail is to check the timestamps on the mail spool file. If the file was written to after it was accessed (read), i.e. its mtime
is greater than atime
, then there is unread mail:
for f in /var/spool/mail/* ; do
[ $(stat -c '%Y -gt %X' "$f") ] && echo "$f has unread mail"
done
That of course considers everything in the mailbox as "read" after it's opened, regardless of if anyone looked at the individual messages.
In many cases, read messages are also moved away from the spool directory (to ~/mbox
), so you could do with just checking the file size.
I forgot to mention I don't have root access, will this still work? Also how does this script take in the userid as an argument? I need to check specific users.
– linuxnewbie
Dec 12 '17 at 1:56
To my understand the /var/spool/mail/ directory is a temporary directory that only holds unread mail. Once it is opened it is either deleted or moved to a different directory thats dedicated to the user. So I should be able to see if the user has unread mail in there just based off of the directory size. I'm just not sure if all of this adds up lol
– linuxnewbie
Dec 12 '17 at 1:59
@linuxnewbie, you only needx
access to the directory tostat
a file, and you have that anyway, since otherwise you couldn't reach your own mail spool file. Of course, as written, it doesn't take an argument, but that's not a very hard exercise (start here: [Tests and Conditionals])mywiki.wooledge.org/BashGuide/TestsAndConditionals) and thestat(1)
man page). And sure, checking the size might be enough too. Depends on how the user works with their mailbox.
– ilkkachu
Dec 12 '17 at 7:42
add a comment |
One method that is used to find unread mail is to check the timestamps on the mail spool file. If the file was written to after it was accessed (read), i.e. its mtime
is greater than atime
, then there is unread mail:
for f in /var/spool/mail/* ; do
[ $(stat -c '%Y -gt %X' "$f") ] && echo "$f has unread mail"
done
That of course considers everything in the mailbox as "read" after it's opened, regardless of if anyone looked at the individual messages.
In many cases, read messages are also moved away from the spool directory (to ~/mbox
), so you could do with just checking the file size.
I forgot to mention I don't have root access, will this still work? Also how does this script take in the userid as an argument? I need to check specific users.
– linuxnewbie
Dec 12 '17 at 1:56
To my understand the /var/spool/mail/ directory is a temporary directory that only holds unread mail. Once it is opened it is either deleted or moved to a different directory thats dedicated to the user. So I should be able to see if the user has unread mail in there just based off of the directory size. I'm just not sure if all of this adds up lol
– linuxnewbie
Dec 12 '17 at 1:59
@linuxnewbie, you only needx
access to the directory tostat
a file, and you have that anyway, since otherwise you couldn't reach your own mail spool file. Of course, as written, it doesn't take an argument, but that's not a very hard exercise (start here: [Tests and Conditionals])mywiki.wooledge.org/BashGuide/TestsAndConditionals) and thestat(1)
man page). And sure, checking the size might be enough too. Depends on how the user works with their mailbox.
– ilkkachu
Dec 12 '17 at 7:42
add a comment |
One method that is used to find unread mail is to check the timestamps on the mail spool file. If the file was written to after it was accessed (read), i.e. its mtime
is greater than atime
, then there is unread mail:
for f in /var/spool/mail/* ; do
[ $(stat -c '%Y -gt %X' "$f") ] && echo "$f has unread mail"
done
That of course considers everything in the mailbox as "read" after it's opened, regardless of if anyone looked at the individual messages.
In many cases, read messages are also moved away from the spool directory (to ~/mbox
), so you could do with just checking the file size.
One method that is used to find unread mail is to check the timestamps on the mail spool file. If the file was written to after it was accessed (read), i.e. its mtime
is greater than atime
, then there is unread mail:
for f in /var/spool/mail/* ; do
[ $(stat -c '%Y -gt %X' "$f") ] && echo "$f has unread mail"
done
That of course considers everything in the mailbox as "read" after it's opened, regardless of if anyone looked at the individual messages.
In many cases, read messages are also moved away from the spool directory (to ~/mbox
), so you could do with just checking the file size.
edited Dec 12 '17 at 7:37
answered Dec 12 '17 at 1:15
ilkkachuilkkachu
60.7k1098172
60.7k1098172
I forgot to mention I don't have root access, will this still work? Also how does this script take in the userid as an argument? I need to check specific users.
– linuxnewbie
Dec 12 '17 at 1:56
To my understand the /var/spool/mail/ directory is a temporary directory that only holds unread mail. Once it is opened it is either deleted or moved to a different directory thats dedicated to the user. So I should be able to see if the user has unread mail in there just based off of the directory size. I'm just not sure if all of this adds up lol
– linuxnewbie
Dec 12 '17 at 1:59
@linuxnewbie, you only needx
access to the directory tostat
a file, and you have that anyway, since otherwise you couldn't reach your own mail spool file. Of course, as written, it doesn't take an argument, but that's not a very hard exercise (start here: [Tests and Conditionals])mywiki.wooledge.org/BashGuide/TestsAndConditionals) and thestat(1)
man page). And sure, checking the size might be enough too. Depends on how the user works with their mailbox.
– ilkkachu
Dec 12 '17 at 7:42
add a comment |
I forgot to mention I don't have root access, will this still work? Also how does this script take in the userid as an argument? I need to check specific users.
– linuxnewbie
Dec 12 '17 at 1:56
To my understand the /var/spool/mail/ directory is a temporary directory that only holds unread mail. Once it is opened it is either deleted or moved to a different directory thats dedicated to the user. So I should be able to see if the user has unread mail in there just based off of the directory size. I'm just not sure if all of this adds up lol
– linuxnewbie
Dec 12 '17 at 1:59
@linuxnewbie, you only needx
access to the directory tostat
a file, and you have that anyway, since otherwise you couldn't reach your own mail spool file. Of course, as written, it doesn't take an argument, but that's not a very hard exercise (start here: [Tests and Conditionals])mywiki.wooledge.org/BashGuide/TestsAndConditionals) and thestat(1)
man page). And sure, checking the size might be enough too. Depends on how the user works with their mailbox.
– ilkkachu
Dec 12 '17 at 7:42
I forgot to mention I don't have root access, will this still work? Also how does this script take in the userid as an argument? I need to check specific users.
– linuxnewbie
Dec 12 '17 at 1:56
I forgot to mention I don't have root access, will this still work? Also how does this script take in the userid as an argument? I need to check specific users.
– linuxnewbie
Dec 12 '17 at 1:56
To my understand the /var/spool/mail/ directory is a temporary directory that only holds unread mail. Once it is opened it is either deleted or moved to a different directory thats dedicated to the user. So I should be able to see if the user has unread mail in there just based off of the directory size. I'm just not sure if all of this adds up lol
– linuxnewbie
Dec 12 '17 at 1:59
To my understand the /var/spool/mail/ directory is a temporary directory that only holds unread mail. Once it is opened it is either deleted or moved to a different directory thats dedicated to the user. So I should be able to see if the user has unread mail in there just based off of the directory size. I'm just not sure if all of this adds up lol
– linuxnewbie
Dec 12 '17 at 1:59
@linuxnewbie, you only need
x
access to the directory to stat
a file, and you have that anyway, since otherwise you couldn't reach your own mail spool file. Of course, as written, it doesn't take an argument, but that's not a very hard exercise (start here: [Tests and Conditionals])mywiki.wooledge.org/BashGuide/TestsAndConditionals) and the stat(1)
man page). And sure, checking the size might be enough too. Depends on how the user works with their mailbox.– ilkkachu
Dec 12 '17 at 7:42
@linuxnewbie, you only need
x
access to the directory to stat
a file, and you have that anyway, since otherwise you couldn't reach your own mail spool file. Of course, as written, it doesn't take an argument, but that's not a very hard exercise (start here: [Tests and Conditionals])mywiki.wooledge.org/BashGuide/TestsAndConditionals) and the stat(1)
man page). And sure, checking the size might be enough too. Depends on how the user works with their mailbox.– ilkkachu
Dec 12 '17 at 7:42
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f410305%2flinux-shell-script-to-check-if-another-user-has-unread-mail%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
will this script be running as
root
, and does it need to be portable?– thrig
Dec 11 '17 at 23:53
No it will not, and no it doesn't need to be portable.
– linuxnewbie
Dec 12 '17 at 0:11
you can get only the file size (unless something set a looser umask than my MTA does) but that will not tell you whether the mail has been read or not
– thrig
Dec 12 '17 at 0:58