Using whoami to search for files that mention user

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












I'm currently working through some exercises to try and improve my shell scripting. The requirements of the script are as follows:




  1. It must get the user's name using the whoami command and store it in a variable called username.

  2. It must take a single parameter which is the name of the file to be searched.

  3. It must grep to search the specified file for occurrences of the user's name and print them.



This part is relatively simple, and I've used the following to get it working:



username=$(whoami)
echo 'Enter the name of the file you would like to search: '
read fileName
cat "fileName" | grep "$username"


However there is a catch, the exercise states the following:




Note: for this task there's no need to worry about missing parameters of error checking. The script, apart from shebang and any comments you choose to ass, shoulc consist of two lines.




How can I reduce this to only two lines?










share|improve this question







New contributor




NigerianWizard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    up vote
    3
    down vote

    favorite












    I'm currently working through some exercises to try and improve my shell scripting. The requirements of the script are as follows:




    1. It must get the user's name using the whoami command and store it in a variable called username.

    2. It must take a single parameter which is the name of the file to be searched.

    3. It must grep to search the specified file for occurrences of the user's name and print them.



    This part is relatively simple, and I've used the following to get it working:



    username=$(whoami)
    echo 'Enter the name of the file you would like to search: '
    read fileName
    cat "fileName" | grep "$username"


    However there is a catch, the exercise states the following:




    Note: for this task there's no need to worry about missing parameters of error checking. The script, apart from shebang and any comments you choose to ass, shoulc consist of two lines.




    How can I reduce this to only two lines?










    share|improve this question







    New contributor




    NigerianWizard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I'm currently working through some exercises to try and improve my shell scripting. The requirements of the script are as follows:




      1. It must get the user's name using the whoami command and store it in a variable called username.

      2. It must take a single parameter which is the name of the file to be searched.

      3. It must grep to search the specified file for occurrences of the user's name and print them.



      This part is relatively simple, and I've used the following to get it working:



      username=$(whoami)
      echo 'Enter the name of the file you would like to search: '
      read fileName
      cat "fileName" | grep "$username"


      However there is a catch, the exercise states the following:




      Note: for this task there's no need to worry about missing parameters of error checking. The script, apart from shebang and any comments you choose to ass, shoulc consist of two lines.




      How can I reduce this to only two lines?










      share|improve this question







      New contributor




      NigerianWizard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I'm currently working through some exercises to try and improve my shell scripting. The requirements of the script are as follows:




      1. It must get the user's name using the whoami command and store it in a variable called username.

      2. It must take a single parameter which is the name of the file to be searched.

      3. It must grep to search the specified file for occurrences of the user's name and print them.



      This part is relatively simple, and I've used the following to get it working:



      username=$(whoami)
      echo 'Enter the name of the file you would like to search: '
      read fileName
      cat "fileName" | grep "$username"


      However there is a catch, the exercise states the following:




      Note: for this task there's no need to worry about missing parameters of error checking. The script, apart from shebang and any comments you choose to ass, shoulc consist of two lines.




      How can I reduce this to only two lines?







      bash shell-script shell






      share|improve this question







      New contributor




      NigerianWizard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      NigerianWizard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      NigerianWizard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 33 mins ago









      NigerianWizard

      161




      161




      New contributor




      NigerianWizard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      NigerianWizard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      NigerianWizard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote













          The pathname of the file is given as a parameter. This means that it's given on the command line of the script. This means that you shouldn't interactively ask the user for it.



          #!/bin/sh
          grep -wF -e "$LOGNAME" "$1"


          Alternatively, if you really want to use whoami:



          #!/bin/sh
          grep -wF -e "$(whoami)" "$1"


          The -w option to grep make it match complete words only. grep -w 'AA' would match AA but not AAA.



          The -F option to grep makes the utility take the given expression as a fixed string and not as a regular expression.



          The -e option signifies that the next string on the command line is the pattern. Without it, a pattern starting with a dash may be mistakenly interpreted as a set of command line options.



          The $LOGNAME (or $USER on many systems) variable holds the username of the current user.



          The value of $1 is the first command line argument given to the script on the command line.



          Note that this does not search for files mentioning a particular user (as in the title of the question), but rather searches for lines in a file that mentions this user.






          share|improve this answer





























            up vote
            0
            down vote













            The simplest solution:



            #!/bin/bash
            username=$(whoami)
            grep $username $1


            You can provide grep with a filename as a optional parameter (see man grep). The variable $1 refers to the firs positional parameter of a script.



            You call it as follows:



            name-finder.sh filename


            You can make it even an one-liner:



            username=$(whoami); grep $username $1


            Or



            grep $(whoami) $1





            share|improve this answer




















            • Since you haven quoted any of the expansions, this would fail on filenames containing spaces or possibly filename globbing chraracters. It would also fail if the username contained spaces (although this would admittedly be unlikely).
              – Kusalananda
              13 mins ago











            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',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );






            NigerianWizard is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f473947%2fusing-whoami-to-search-for-files-that-mention-user%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote













            The pathname of the file is given as a parameter. This means that it's given on the command line of the script. This means that you shouldn't interactively ask the user for it.



            #!/bin/sh
            grep -wF -e "$LOGNAME" "$1"


            Alternatively, if you really want to use whoami:



            #!/bin/sh
            grep -wF -e "$(whoami)" "$1"


            The -w option to grep make it match complete words only. grep -w 'AA' would match AA but not AAA.



            The -F option to grep makes the utility take the given expression as a fixed string and not as a regular expression.



            The -e option signifies that the next string on the command line is the pattern. Without it, a pattern starting with a dash may be mistakenly interpreted as a set of command line options.



            The $LOGNAME (or $USER on many systems) variable holds the username of the current user.



            The value of $1 is the first command line argument given to the script on the command line.



            Note that this does not search for files mentioning a particular user (as in the title of the question), but rather searches for lines in a file that mentions this user.






            share|improve this answer


























              up vote
              3
              down vote













              The pathname of the file is given as a parameter. This means that it's given on the command line of the script. This means that you shouldn't interactively ask the user for it.



              #!/bin/sh
              grep -wF -e "$LOGNAME" "$1"


              Alternatively, if you really want to use whoami:



              #!/bin/sh
              grep -wF -e "$(whoami)" "$1"


              The -w option to grep make it match complete words only. grep -w 'AA' would match AA but not AAA.



              The -F option to grep makes the utility take the given expression as a fixed string and not as a regular expression.



              The -e option signifies that the next string on the command line is the pattern. Without it, a pattern starting with a dash may be mistakenly interpreted as a set of command line options.



              The $LOGNAME (or $USER on many systems) variable holds the username of the current user.



              The value of $1 is the first command line argument given to the script on the command line.



              Note that this does not search for files mentioning a particular user (as in the title of the question), but rather searches for lines in a file that mentions this user.






              share|improve this answer
























                up vote
                3
                down vote










                up vote
                3
                down vote









                The pathname of the file is given as a parameter. This means that it's given on the command line of the script. This means that you shouldn't interactively ask the user for it.



                #!/bin/sh
                grep -wF -e "$LOGNAME" "$1"


                Alternatively, if you really want to use whoami:



                #!/bin/sh
                grep -wF -e "$(whoami)" "$1"


                The -w option to grep make it match complete words only. grep -w 'AA' would match AA but not AAA.



                The -F option to grep makes the utility take the given expression as a fixed string and not as a regular expression.



                The -e option signifies that the next string on the command line is the pattern. Without it, a pattern starting with a dash may be mistakenly interpreted as a set of command line options.



                The $LOGNAME (or $USER on many systems) variable holds the username of the current user.



                The value of $1 is the first command line argument given to the script on the command line.



                Note that this does not search for files mentioning a particular user (as in the title of the question), but rather searches for lines in a file that mentions this user.






                share|improve this answer














                The pathname of the file is given as a parameter. This means that it's given on the command line of the script. This means that you shouldn't interactively ask the user for it.



                #!/bin/sh
                grep -wF -e "$LOGNAME" "$1"


                Alternatively, if you really want to use whoami:



                #!/bin/sh
                grep -wF -e "$(whoami)" "$1"


                The -w option to grep make it match complete words only. grep -w 'AA' would match AA but not AAA.



                The -F option to grep makes the utility take the given expression as a fixed string and not as a regular expression.



                The -e option signifies that the next string on the command line is the pattern. Without it, a pattern starting with a dash may be mistakenly interpreted as a set of command line options.



                The $LOGNAME (or $USER on many systems) variable holds the username of the current user.



                The value of $1 is the first command line argument given to the script on the command line.



                Note that this does not search for files mentioning a particular user (as in the title of the question), but rather searches for lines in a file that mentions this user.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 10 mins ago

























                answered 23 mins ago









                Kusalananda

                108k14210333




                108k14210333






















                    up vote
                    0
                    down vote













                    The simplest solution:



                    #!/bin/bash
                    username=$(whoami)
                    grep $username $1


                    You can provide grep with a filename as a optional parameter (see man grep). The variable $1 refers to the firs positional parameter of a script.



                    You call it as follows:



                    name-finder.sh filename


                    You can make it even an one-liner:



                    username=$(whoami); grep $username $1


                    Or



                    grep $(whoami) $1





                    share|improve this answer




















                    • Since you haven quoted any of the expansions, this would fail on filenames containing spaces or possibly filename globbing chraracters. It would also fail if the username contained spaces (although this would admittedly be unlikely).
                      – Kusalananda
                      13 mins ago















                    up vote
                    0
                    down vote













                    The simplest solution:



                    #!/bin/bash
                    username=$(whoami)
                    grep $username $1


                    You can provide grep with a filename as a optional parameter (see man grep). The variable $1 refers to the firs positional parameter of a script.



                    You call it as follows:



                    name-finder.sh filename


                    You can make it even an one-liner:



                    username=$(whoami); grep $username $1


                    Or



                    grep $(whoami) $1





                    share|improve this answer




















                    • Since you haven quoted any of the expansions, this would fail on filenames containing spaces or possibly filename globbing chraracters. It would also fail if the username contained spaces (although this would admittedly be unlikely).
                      – Kusalananda
                      13 mins ago













                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    The simplest solution:



                    #!/bin/bash
                    username=$(whoami)
                    grep $username $1


                    You can provide grep with a filename as a optional parameter (see man grep). The variable $1 refers to the firs positional parameter of a script.



                    You call it as follows:



                    name-finder.sh filename


                    You can make it even an one-liner:



                    username=$(whoami); grep $username $1


                    Or



                    grep $(whoami) $1





                    share|improve this answer












                    The simplest solution:



                    #!/bin/bash
                    username=$(whoami)
                    grep $username $1


                    You can provide grep with a filename as a optional parameter (see man grep). The variable $1 refers to the firs positional parameter of a script.



                    You call it as follows:



                    name-finder.sh filename


                    You can make it even an one-liner:



                    username=$(whoami); grep $username $1


                    Or



                    grep $(whoami) $1






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 19 mins ago









                    Piotr Sawicki

                    516




                    516











                    • Since you haven quoted any of the expansions, this would fail on filenames containing spaces or possibly filename globbing chraracters. It would also fail if the username contained spaces (although this would admittedly be unlikely).
                      – Kusalananda
                      13 mins ago

















                    • Since you haven quoted any of the expansions, this would fail on filenames containing spaces or possibly filename globbing chraracters. It would also fail if the username contained spaces (although this would admittedly be unlikely).
                      – Kusalananda
                      13 mins ago
















                    Since you haven quoted any of the expansions, this would fail on filenames containing spaces or possibly filename globbing chraracters. It would also fail if the username contained spaces (although this would admittedly be unlikely).
                    – Kusalananda
                    13 mins ago





                    Since you haven quoted any of the expansions, this would fail on filenames containing spaces or possibly filename globbing chraracters. It would also fail if the username contained spaces (although this would admittedly be unlikely).
                    – Kusalananda
                    13 mins ago











                    NigerianWizard is a new contributor. Be nice, and check out our Code of Conduct.









                     

                    draft saved


                    draft discarded


















                    NigerianWizard is a new contributor. Be nice, and check out our Code of Conduct.












                    NigerianWizard is a new contributor. Be nice, and check out our Code of Conduct.











                    NigerianWizard is a new contributor. Be nice, and check out our Code of Conduct.













                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f473947%2fusing-whoami-to-search-for-files-that-mention-user%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    List of Gilmore Girls characters

                    Confectionery