Create a file to automatically check user list for users specified











up vote
1
down vote

favorite












I am realitively new to bash scripting, but have created this from my intense googling



I am attempting to create a bash script to run on Ubuntu that will check the user list and find any users that aren’t in a specified list.
I have written this so far:



Cat /etc/passwd | grep -o -P ‘.{0,40}:1[0-9][0-9][0-9].{0,0}.’ | cut -d: -f1



views passwd file, then finds only the lines with a user ID of 1000 or above, and everything before the userID of each line is piped into cut which removes all except the username of each user.



I then want to have the script check a file for usernames I specify (probably copied from a list) and compare each to the output of the above. Removing all usernames specified.



So for instance:



I have users John, Ben and Tom on my computer
If I put John and Ben in the file the script is accessing, it should output Tom since he is not specified



How would I go about doing this?










share|improve this question


















  • 1




    Have you heard of awk. I'm not very good at it but it's a well powerful programing language. ` awk -F: '{print $1}' /etc/passwd` lists all user id
    – rhubarbdog
    Dec 4 at 3:55












  • I'll experiment more with it, but when I run that, it also includes system users such as root, I'm only looking for user accounts that you can login as, such as a personal user account
    – Tech_Person
    Dec 4 at 4:00










  • Yes but this is where awk becomes powerful because you can precede the print program with a pattern based on $2 the user number. I think you can put if statements in your program
    – rhubarbdog
    Dec 4 at 4:06















up vote
1
down vote

favorite












I am realitively new to bash scripting, but have created this from my intense googling



I am attempting to create a bash script to run on Ubuntu that will check the user list and find any users that aren’t in a specified list.
I have written this so far:



Cat /etc/passwd | grep -o -P ‘.{0,40}:1[0-9][0-9][0-9].{0,0}.’ | cut -d: -f1



views passwd file, then finds only the lines with a user ID of 1000 or above, and everything before the userID of each line is piped into cut which removes all except the username of each user.



I then want to have the script check a file for usernames I specify (probably copied from a list) and compare each to the output of the above. Removing all usernames specified.



So for instance:



I have users John, Ben and Tom on my computer
If I put John and Ben in the file the script is accessing, it should output Tom since he is not specified



How would I go about doing this?










share|improve this question


















  • 1




    Have you heard of awk. I'm not very good at it but it's a well powerful programing language. ` awk -F: '{print $1}' /etc/passwd` lists all user id
    – rhubarbdog
    Dec 4 at 3:55












  • I'll experiment more with it, but when I run that, it also includes system users such as root, I'm only looking for user accounts that you can login as, such as a personal user account
    – Tech_Person
    Dec 4 at 4:00










  • Yes but this is where awk becomes powerful because you can precede the print program with a pattern based on $2 the user number. I think you can put if statements in your program
    – rhubarbdog
    Dec 4 at 4:06













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am realitively new to bash scripting, but have created this from my intense googling



I am attempting to create a bash script to run on Ubuntu that will check the user list and find any users that aren’t in a specified list.
I have written this so far:



Cat /etc/passwd | grep -o -P ‘.{0,40}:1[0-9][0-9][0-9].{0,0}.’ | cut -d: -f1



views passwd file, then finds only the lines with a user ID of 1000 or above, and everything before the userID of each line is piped into cut which removes all except the username of each user.



I then want to have the script check a file for usernames I specify (probably copied from a list) and compare each to the output of the above. Removing all usernames specified.



So for instance:



I have users John, Ben and Tom on my computer
If I put John and Ben in the file the script is accessing, it should output Tom since he is not specified



How would I go about doing this?










share|improve this question













I am realitively new to bash scripting, but have created this from my intense googling



I am attempting to create a bash script to run on Ubuntu that will check the user list and find any users that aren’t in a specified list.
I have written this so far:



Cat /etc/passwd | grep -o -P ‘.{0,40}:1[0-9][0-9][0-9].{0,0}.’ | cut -d: -f1



views passwd file, then finds only the lines with a user ID of 1000 or above, and everything before the userID of each line is piped into cut which removes all except the username of each user.



I then want to have the script check a file for usernames I specify (probably copied from a list) and compare each to the output of the above. Removing all usernames specified.



So for instance:



I have users John, Ben and Tom on my computer
If I put John and Ben in the file the script is accessing, it should output Tom since he is not specified



How would I go about doing this?







bash scripts






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 4 at 3:46









Tech_Person

83




83








  • 1




    Have you heard of awk. I'm not very good at it but it's a well powerful programing language. ` awk -F: '{print $1}' /etc/passwd` lists all user id
    – rhubarbdog
    Dec 4 at 3:55












  • I'll experiment more with it, but when I run that, it also includes system users such as root, I'm only looking for user accounts that you can login as, such as a personal user account
    – Tech_Person
    Dec 4 at 4:00










  • Yes but this is where awk becomes powerful because you can precede the print program with a pattern based on $2 the user number. I think you can put if statements in your program
    – rhubarbdog
    Dec 4 at 4:06














  • 1




    Have you heard of awk. I'm not very good at it but it's a well powerful programing language. ` awk -F: '{print $1}' /etc/passwd` lists all user id
    – rhubarbdog
    Dec 4 at 3:55












  • I'll experiment more with it, but when I run that, it also includes system users such as root, I'm only looking for user accounts that you can login as, such as a personal user account
    – Tech_Person
    Dec 4 at 4:00










  • Yes but this is where awk becomes powerful because you can precede the print program with a pattern based on $2 the user number. I think you can put if statements in your program
    – rhubarbdog
    Dec 4 at 4:06








1




1




Have you heard of awk. I'm not very good at it but it's a well powerful programing language. ` awk -F: '{print $1}' /etc/passwd` lists all user id
– rhubarbdog
Dec 4 at 3:55






Have you heard of awk. I'm not very good at it but it's a well powerful programing language. ` awk -F: '{print $1}' /etc/passwd` lists all user id
– rhubarbdog
Dec 4 at 3:55














I'll experiment more with it, but when I run that, it also includes system users such as root, I'm only looking for user accounts that you can login as, such as a personal user account
– Tech_Person
Dec 4 at 4:00




I'll experiment more with it, but when I run that, it also includes system users such as root, I'm only looking for user accounts that you can login as, such as a personal user account
– Tech_Person
Dec 4 at 4:00












Yes but this is where awk becomes powerful because you can precede the print program with a pattern based on $2 the user number. I think you can put if statements in your program
– rhubarbdog
Dec 4 at 4:06




Yes but this is where awk becomes powerful because you can precede the print program with a pattern based on $2 the user number. I think you can put if statements in your program
– rhubarbdog
Dec 4 at 4:06










2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










This code should work for you.



for username in $( awk -F':' '$3 >= 1000 {print $1}' /etc/passwd )
do
if ! grep -q "$username" list.txt; then
echo "$username"
fi
done


Keep the usernames to check in list.txt as one user per line






share|improve this answer



















  • 1




    This works almost perfectly, when i run it, i get the "nobody" user in the output aswell, is there any easy way to get around this?
    – Tech_Person
    Dec 4 at 4:14












  • Simply change the awk condition to $3 >= 1000 && $1 != "nobody"
    – Kishor V
    Dec 4 at 10:36










  • ok, this entire thing is exactly what i was looking for thankyou!
    – Tech_Person
    Dec 5 at 0:17


















up vote
1
down vote













grep allows specifying patterns from file. Thus you can use grep -f option for that purpose and -v for inverse pattern matching, i.e. print lines not matching the pattern. Thus,



 printf "^%sn" 'John' 'Ben' > /tmp/list.txt
grep -v -f /tmp/list.txt /etc/passwd | cut -d ':' -f 1
rm l/tmp/ist.txt


We could also take into account UID greater than 999, since by default that refers to human users unless changed by your sysadmin.



awk -F ':' '$1 !~ /John|Ben|nobody/  && $3 > 999' /etc/passwd





share|improve this answer























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


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1098286%2fcreate-a-file-to-automatically-check-user-list-for-users-specified%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








    up vote
    2
    down vote



    accepted










    This code should work for you.



    for username in $( awk -F':' '$3 >= 1000 {print $1}' /etc/passwd )
    do
    if ! grep -q "$username" list.txt; then
    echo "$username"
    fi
    done


    Keep the usernames to check in list.txt as one user per line






    share|improve this answer



















    • 1




      This works almost perfectly, when i run it, i get the "nobody" user in the output aswell, is there any easy way to get around this?
      – Tech_Person
      Dec 4 at 4:14












    • Simply change the awk condition to $3 >= 1000 && $1 != "nobody"
      – Kishor V
      Dec 4 at 10:36










    • ok, this entire thing is exactly what i was looking for thankyou!
      – Tech_Person
      Dec 5 at 0:17















    up vote
    2
    down vote



    accepted










    This code should work for you.



    for username in $( awk -F':' '$3 >= 1000 {print $1}' /etc/passwd )
    do
    if ! grep -q "$username" list.txt; then
    echo "$username"
    fi
    done


    Keep the usernames to check in list.txt as one user per line






    share|improve this answer



















    • 1




      This works almost perfectly, when i run it, i get the "nobody" user in the output aswell, is there any easy way to get around this?
      – Tech_Person
      Dec 4 at 4:14












    • Simply change the awk condition to $3 >= 1000 && $1 != "nobody"
      – Kishor V
      Dec 4 at 10:36










    • ok, this entire thing is exactly what i was looking for thankyou!
      – Tech_Person
      Dec 5 at 0:17













    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted






    This code should work for you.



    for username in $( awk -F':' '$3 >= 1000 {print $1}' /etc/passwd )
    do
    if ! grep -q "$username" list.txt; then
    echo "$username"
    fi
    done


    Keep the usernames to check in list.txt as one user per line






    share|improve this answer














    This code should work for you.



    for username in $( awk -F':' '$3 >= 1000 {print $1}' /etc/passwd )
    do
    if ! grep -q "$username" list.txt; then
    echo "$username"
    fi
    done


    Keep the usernames to check in list.txt as one user per line







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 4 at 4:10

























    answered Dec 4 at 4:05









    Kishor V

    1362




    1362








    • 1




      This works almost perfectly, when i run it, i get the "nobody" user in the output aswell, is there any easy way to get around this?
      – Tech_Person
      Dec 4 at 4:14












    • Simply change the awk condition to $3 >= 1000 && $1 != "nobody"
      – Kishor V
      Dec 4 at 10:36










    • ok, this entire thing is exactly what i was looking for thankyou!
      – Tech_Person
      Dec 5 at 0:17














    • 1




      This works almost perfectly, when i run it, i get the "nobody" user in the output aswell, is there any easy way to get around this?
      – Tech_Person
      Dec 4 at 4:14












    • Simply change the awk condition to $3 >= 1000 && $1 != "nobody"
      – Kishor V
      Dec 4 at 10:36










    • ok, this entire thing is exactly what i was looking for thankyou!
      – Tech_Person
      Dec 5 at 0:17








    1




    1




    This works almost perfectly, when i run it, i get the "nobody" user in the output aswell, is there any easy way to get around this?
    – Tech_Person
    Dec 4 at 4:14






    This works almost perfectly, when i run it, i get the "nobody" user in the output aswell, is there any easy way to get around this?
    – Tech_Person
    Dec 4 at 4:14














    Simply change the awk condition to $3 >= 1000 && $1 != "nobody"
    – Kishor V
    Dec 4 at 10:36




    Simply change the awk condition to $3 >= 1000 && $1 != "nobody"
    – Kishor V
    Dec 4 at 10:36












    ok, this entire thing is exactly what i was looking for thankyou!
    – Tech_Person
    Dec 5 at 0:17




    ok, this entire thing is exactly what i was looking for thankyou!
    – Tech_Person
    Dec 5 at 0:17












    up vote
    1
    down vote













    grep allows specifying patterns from file. Thus you can use grep -f option for that purpose and -v for inverse pattern matching, i.e. print lines not matching the pattern. Thus,



     printf "^%sn" 'John' 'Ben' > /tmp/list.txt
    grep -v -f /tmp/list.txt /etc/passwd | cut -d ':' -f 1
    rm l/tmp/ist.txt


    We could also take into account UID greater than 999, since by default that refers to human users unless changed by your sysadmin.



    awk -F ':' '$1 !~ /John|Ben|nobody/  && $3 > 999' /etc/passwd





    share|improve this answer



























      up vote
      1
      down vote













      grep allows specifying patterns from file. Thus you can use grep -f option for that purpose and -v for inverse pattern matching, i.e. print lines not matching the pattern. Thus,



       printf "^%sn" 'John' 'Ben' > /tmp/list.txt
      grep -v -f /tmp/list.txt /etc/passwd | cut -d ':' -f 1
      rm l/tmp/ist.txt


      We could also take into account UID greater than 999, since by default that refers to human users unless changed by your sysadmin.



      awk -F ':' '$1 !~ /John|Ben|nobody/  && $3 > 999' /etc/passwd





      share|improve this answer

























        up vote
        1
        down vote










        up vote
        1
        down vote









        grep allows specifying patterns from file. Thus you can use grep -f option for that purpose and -v for inverse pattern matching, i.e. print lines not matching the pattern. Thus,



         printf "^%sn" 'John' 'Ben' > /tmp/list.txt
        grep -v -f /tmp/list.txt /etc/passwd | cut -d ':' -f 1
        rm l/tmp/ist.txt


        We could also take into account UID greater than 999, since by default that refers to human users unless changed by your sysadmin.



        awk -F ':' '$1 !~ /John|Ben|nobody/  && $3 > 999' /etc/passwd





        share|improve this answer














        grep allows specifying patterns from file. Thus you can use grep -f option for that purpose and -v for inverse pattern matching, i.e. print lines not matching the pattern. Thus,



         printf "^%sn" 'John' 'Ben' > /tmp/list.txt
        grep -v -f /tmp/list.txt /etc/passwd | cut -d ':' -f 1
        rm l/tmp/ist.txt


        We could also take into account UID greater than 999, since by default that refers to human users unless changed by your sysadmin.



        awk -F ':' '$1 !~ /John|Ben|nobody/  && $3 > 999' /etc/passwd






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 4 at 4:24

























        answered Dec 4 at 4:04









        Sergiy Kolodyazhnyy

        68.9k9143303




        68.9k9143303






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1098286%2fcreate-a-file-to-automatically-check-user-list-for-users-specified%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

            flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

            Mangá

             ⁒  ․,‪⁊‑⁙ ⁖, ⁇‒※‌, †,⁖‗‌⁝    ‾‸⁘,‖⁔⁣,⁂‾
”‑,‥–,‬ ,⁀‹⁋‴⁑ ‒ ,‴⁋”‼ ⁨,‷⁔„ ‰′,‐‚ ‥‡‎“‷⁃⁨⁅⁣,⁔
⁇‘⁔⁡⁏⁌⁡‿‶‏⁨ ⁣⁕⁖⁨⁩⁥‽⁀  ‴‬⁜‟ ⁃‣‧⁕‮ …‍⁨‴ ⁩,⁚⁖‫ ,‵ ⁀,‮⁝‣‣ ⁑  ⁂– ․, ‾‽ ‏⁁“⁗‸ ‾… ‹‡⁌⁎‸‘ ‡⁏⁌‪ ‵⁛ ‎⁨ ―⁦⁤⁄⁕