Linux shell script to check if another user has unread mail












2















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.










share|improve this question
















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 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
















2















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.










share|improve this question
















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 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














2












2








2








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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



















  • 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

















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










2 Answers
2






active

oldest

votes


















0














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






share|improve this answer


























  • 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



















0














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.






share|improve this answer


























  • 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 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











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
});


}
});














draft saved

draft discarded


















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









0














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






share|improve this answer


























  • 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
















0














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






share|improve this answer


























  • 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














0












0








0







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






share|improve this answer















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







share|improve this answer














share|improve this answer



share|improve this answer








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



















  • 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













0














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.






share|improve this answer


























  • 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 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
















0














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.






share|improve this answer


























  • 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 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














0












0








0







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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








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 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



















  • 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 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

















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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

サソリ

広島県道265号伴広島線

Setup Asymptote in Texstudio