Converting a working command into shell script file which gets arguments

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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












Hi my working command is:



grep -l "my text" file*.ext|xargs ls -lart


this command is very useful for me and I wanted to create a shell file which does it with fewer typing, like this:



fn "my text" file*.ext


I came to this shell script file:



grep -l "$1" "$2"|xargs ls -lart


which is not working, even the first part is not working:



grep -l "$1" "$2"


which returns null, please help me with this







share|improve this question

























    up vote
    0
    down vote

    favorite












    Hi my working command is:



    grep -l "my text" file*.ext|xargs ls -lart


    this command is very useful for me and I wanted to create a shell file which does it with fewer typing, like this:



    fn "my text" file*.ext


    I came to this shell script file:



    grep -l "$1" "$2"|xargs ls -lart


    which is not working, even the first part is not working:



    grep -l "$1" "$2"


    which returns null, please help me with this







    share|improve this question





















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Hi my working command is:



      grep -l "my text" file*.ext|xargs ls -lart


      this command is very useful for me and I wanted to create a shell file which does it with fewer typing, like this:



      fn "my text" file*.ext


      I came to this shell script file:



      grep -l "$1" "$2"|xargs ls -lart


      which is not working, even the first part is not working:



      grep -l "$1" "$2"


      which returns null, please help me with this







      share|improve this question











      Hi my working command is:



      grep -l "my text" file*.ext|xargs ls -lart


      this command is very useful for me and I wanted to create a shell file which does it with fewer typing, like this:



      fn "my text" file*.ext


      I came to this shell script file:



      grep -l "$1" "$2"|xargs ls -lart


      which is not working, even the first part is not working:



      grep -l "$1" "$2"


      which returns null, please help me with this









      share|improve this question










      share|improve this question




      share|improve this question









      asked Jul 29 at 9:15









      hmmftg

      1033




      1033




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Your script will need to take more than two arguments.



          Before the shell executes grep in the command



          grep -l "my text" file*.ext


          the filename globbing pattern file*.ext is expanded to all matching filenames. The command line that is finally run will therefore be



          grep -l "my text" file1.ext file2.ext file3.ext


          (if those are the files that matches the pattern)



          Therefore, your script may look like



          #!/bin/sh

          pattern=$1
          shift

          grep -l -e "$pattern" "$@" | xargs ls -lrt


          Here, the pattern is saved into a variable, and then shifted off the list of command line arguments. The remaining arguments is a list of filenames (if the filename globbing pattern on the command line matches) and is available in "$@".



          Make sure you double quote $@. This will make $@ expand to the list of individually quoted command line arguments.



          I've removed the -a option from ls as it's not needed when giving explicit filenames to list. I'm also using grep with -e to specify the pattern, as a pattern with an initial dash (-) may otherwise confuse grep.




          If it wasn't for the sorting, I would have suggested the following find command instead of your pipeline (correctly handles filenames with spaces and newlines):



          find . -type f -name "$filepattern" -exec grep -q -e "$pattern" ';' -ls


          This looks for files with names matching what's in $filepattern, and with contents matching what's in $pattern, and then produces ls -l-like output. The search is performed recursively in the current directory.



          As part of a script:



          #!/bin/sh

          pattern=$1
          filepattern=$2

          find . -type f -name "$filepattern" -exec grep -q -e "$pattern" ';' -ls


          This script would need to be called in such a way that the filename globbing pattern isn't expanded by the shell when calling it:



          $ ./script.sh 'my text' 'file*.ext'





          share|improve this answer






























            up vote
            1
            down vote













            To make it a bit safer, though relying on GNU extensions:



            #! /bin/sh -
            pattern=$1?pattern required; shift
            : "$1?no filename specified"
            grep -l --null -e "$pattern" -- "$@" |
            xargs -r0 ls -lrtd --


            With --null/-0, we avoid problems with file names that contain blanks or quotes or backslashes.



            With -e "$pattern", we avoid problems with patterns that start with -.



            With --, we avoid problems with file names that start with -.



            With -d, we avoid possible confusions if files of type directory happen to match.



            With -r, we avoid listing the current directory if there's no matching file.



            Note that for a big number of files, xargs may choose to run more than one ls invocation with part of the file list, which means the output would not be properly listed by time.



            Also note that - would mean stdin to grep, but the file called - in the current directory for ls. GNU grep -l would output (standard input) (or its translation in your language). If you want to specify the file called - in the current directory, use ./- instead of -.






            share|improve this answer





















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



              );








               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f459150%2fconverting-a-working-command-into-shell-script-file-which-gets-arguments%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
              1
              down vote



              accepted










              Your script will need to take more than two arguments.



              Before the shell executes grep in the command



              grep -l "my text" file*.ext


              the filename globbing pattern file*.ext is expanded to all matching filenames. The command line that is finally run will therefore be



              grep -l "my text" file1.ext file2.ext file3.ext


              (if those are the files that matches the pattern)



              Therefore, your script may look like



              #!/bin/sh

              pattern=$1
              shift

              grep -l -e "$pattern" "$@" | xargs ls -lrt


              Here, the pattern is saved into a variable, and then shifted off the list of command line arguments. The remaining arguments is a list of filenames (if the filename globbing pattern on the command line matches) and is available in "$@".



              Make sure you double quote $@. This will make $@ expand to the list of individually quoted command line arguments.



              I've removed the -a option from ls as it's not needed when giving explicit filenames to list. I'm also using grep with -e to specify the pattern, as a pattern with an initial dash (-) may otherwise confuse grep.




              If it wasn't for the sorting, I would have suggested the following find command instead of your pipeline (correctly handles filenames with spaces and newlines):



              find . -type f -name "$filepattern" -exec grep -q -e "$pattern" ';' -ls


              This looks for files with names matching what's in $filepattern, and with contents matching what's in $pattern, and then produces ls -l-like output. The search is performed recursively in the current directory.



              As part of a script:



              #!/bin/sh

              pattern=$1
              filepattern=$2

              find . -type f -name "$filepattern" -exec grep -q -e "$pattern" ';' -ls


              This script would need to be called in such a way that the filename globbing pattern isn't expanded by the shell when calling it:



              $ ./script.sh 'my text' 'file*.ext'





              share|improve this answer



























                up vote
                1
                down vote



                accepted










                Your script will need to take more than two arguments.



                Before the shell executes grep in the command



                grep -l "my text" file*.ext


                the filename globbing pattern file*.ext is expanded to all matching filenames. The command line that is finally run will therefore be



                grep -l "my text" file1.ext file2.ext file3.ext


                (if those are the files that matches the pattern)



                Therefore, your script may look like



                #!/bin/sh

                pattern=$1
                shift

                grep -l -e "$pattern" "$@" | xargs ls -lrt


                Here, the pattern is saved into a variable, and then shifted off the list of command line arguments. The remaining arguments is a list of filenames (if the filename globbing pattern on the command line matches) and is available in "$@".



                Make sure you double quote $@. This will make $@ expand to the list of individually quoted command line arguments.



                I've removed the -a option from ls as it's not needed when giving explicit filenames to list. I'm also using grep with -e to specify the pattern, as a pattern with an initial dash (-) may otherwise confuse grep.




                If it wasn't for the sorting, I would have suggested the following find command instead of your pipeline (correctly handles filenames with spaces and newlines):



                find . -type f -name "$filepattern" -exec grep -q -e "$pattern" ';' -ls


                This looks for files with names matching what's in $filepattern, and with contents matching what's in $pattern, and then produces ls -l-like output. The search is performed recursively in the current directory.



                As part of a script:



                #!/bin/sh

                pattern=$1
                filepattern=$2

                find . -type f -name "$filepattern" -exec grep -q -e "$pattern" ';' -ls


                This script would need to be called in such a way that the filename globbing pattern isn't expanded by the shell when calling it:



                $ ./script.sh 'my text' 'file*.ext'





                share|improve this answer

























                  up vote
                  1
                  down vote



                  accepted







                  up vote
                  1
                  down vote



                  accepted






                  Your script will need to take more than two arguments.



                  Before the shell executes grep in the command



                  grep -l "my text" file*.ext


                  the filename globbing pattern file*.ext is expanded to all matching filenames. The command line that is finally run will therefore be



                  grep -l "my text" file1.ext file2.ext file3.ext


                  (if those are the files that matches the pattern)



                  Therefore, your script may look like



                  #!/bin/sh

                  pattern=$1
                  shift

                  grep -l -e "$pattern" "$@" | xargs ls -lrt


                  Here, the pattern is saved into a variable, and then shifted off the list of command line arguments. The remaining arguments is a list of filenames (if the filename globbing pattern on the command line matches) and is available in "$@".



                  Make sure you double quote $@. This will make $@ expand to the list of individually quoted command line arguments.



                  I've removed the -a option from ls as it's not needed when giving explicit filenames to list. I'm also using grep with -e to specify the pattern, as a pattern with an initial dash (-) may otherwise confuse grep.




                  If it wasn't for the sorting, I would have suggested the following find command instead of your pipeline (correctly handles filenames with spaces and newlines):



                  find . -type f -name "$filepattern" -exec grep -q -e "$pattern" ';' -ls


                  This looks for files with names matching what's in $filepattern, and with contents matching what's in $pattern, and then produces ls -l-like output. The search is performed recursively in the current directory.



                  As part of a script:



                  #!/bin/sh

                  pattern=$1
                  filepattern=$2

                  find . -type f -name "$filepattern" -exec grep -q -e "$pattern" ';' -ls


                  This script would need to be called in such a way that the filename globbing pattern isn't expanded by the shell when calling it:



                  $ ./script.sh 'my text' 'file*.ext'





                  share|improve this answer















                  Your script will need to take more than two arguments.



                  Before the shell executes grep in the command



                  grep -l "my text" file*.ext


                  the filename globbing pattern file*.ext is expanded to all matching filenames. The command line that is finally run will therefore be



                  grep -l "my text" file1.ext file2.ext file3.ext


                  (if those are the files that matches the pattern)



                  Therefore, your script may look like



                  #!/bin/sh

                  pattern=$1
                  shift

                  grep -l -e "$pattern" "$@" | xargs ls -lrt


                  Here, the pattern is saved into a variable, and then shifted off the list of command line arguments. The remaining arguments is a list of filenames (if the filename globbing pattern on the command line matches) and is available in "$@".



                  Make sure you double quote $@. This will make $@ expand to the list of individually quoted command line arguments.



                  I've removed the -a option from ls as it's not needed when giving explicit filenames to list. I'm also using grep with -e to specify the pattern, as a pattern with an initial dash (-) may otherwise confuse grep.




                  If it wasn't for the sorting, I would have suggested the following find command instead of your pipeline (correctly handles filenames with spaces and newlines):



                  find . -type f -name "$filepattern" -exec grep -q -e "$pattern" ';' -ls


                  This looks for files with names matching what's in $filepattern, and with contents matching what's in $pattern, and then produces ls -l-like output. The search is performed recursively in the current directory.



                  As part of a script:



                  #!/bin/sh

                  pattern=$1
                  filepattern=$2

                  find . -type f -name "$filepattern" -exec grep -q -e "$pattern" ';' -ls


                  This script would need to be called in such a way that the filename globbing pattern isn't expanded by the shell when calling it:



                  $ ./script.sh 'my text' 'file*.ext'






                  share|improve this answer















                  share|improve this answer



                  share|improve this answer








                  edited Jul 29 at 9:55


























                  answered Jul 29 at 9:25









                  Kusalananda

                  101k13199311




                  101k13199311






















                      up vote
                      1
                      down vote













                      To make it a bit safer, though relying on GNU extensions:



                      #! /bin/sh -
                      pattern=$1?pattern required; shift
                      : "$1?no filename specified"
                      grep -l --null -e "$pattern" -- "$@" |
                      xargs -r0 ls -lrtd --


                      With --null/-0, we avoid problems with file names that contain blanks or quotes or backslashes.



                      With -e "$pattern", we avoid problems with patterns that start with -.



                      With --, we avoid problems with file names that start with -.



                      With -d, we avoid possible confusions if files of type directory happen to match.



                      With -r, we avoid listing the current directory if there's no matching file.



                      Note that for a big number of files, xargs may choose to run more than one ls invocation with part of the file list, which means the output would not be properly listed by time.



                      Also note that - would mean stdin to grep, but the file called - in the current directory for ls. GNU grep -l would output (standard input) (or its translation in your language). If you want to specify the file called - in the current directory, use ./- instead of -.






                      share|improve this answer

























                        up vote
                        1
                        down vote













                        To make it a bit safer, though relying on GNU extensions:



                        #! /bin/sh -
                        pattern=$1?pattern required; shift
                        : "$1?no filename specified"
                        grep -l --null -e "$pattern" -- "$@" |
                        xargs -r0 ls -lrtd --


                        With --null/-0, we avoid problems with file names that contain blanks or quotes or backslashes.



                        With -e "$pattern", we avoid problems with patterns that start with -.



                        With --, we avoid problems with file names that start with -.



                        With -d, we avoid possible confusions if files of type directory happen to match.



                        With -r, we avoid listing the current directory if there's no matching file.



                        Note that for a big number of files, xargs may choose to run more than one ls invocation with part of the file list, which means the output would not be properly listed by time.



                        Also note that - would mean stdin to grep, but the file called - in the current directory for ls. GNU grep -l would output (standard input) (or its translation in your language). If you want to specify the file called - in the current directory, use ./- instead of -.






                        share|improve this answer























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          To make it a bit safer, though relying on GNU extensions:



                          #! /bin/sh -
                          pattern=$1?pattern required; shift
                          : "$1?no filename specified"
                          grep -l --null -e "$pattern" -- "$@" |
                          xargs -r0 ls -lrtd --


                          With --null/-0, we avoid problems with file names that contain blanks or quotes or backslashes.



                          With -e "$pattern", we avoid problems with patterns that start with -.



                          With --, we avoid problems with file names that start with -.



                          With -d, we avoid possible confusions if files of type directory happen to match.



                          With -r, we avoid listing the current directory if there's no matching file.



                          Note that for a big number of files, xargs may choose to run more than one ls invocation with part of the file list, which means the output would not be properly listed by time.



                          Also note that - would mean stdin to grep, but the file called - in the current directory for ls. GNU grep -l would output (standard input) (or its translation in your language). If you want to specify the file called - in the current directory, use ./- instead of -.






                          share|improve this answer













                          To make it a bit safer, though relying on GNU extensions:



                          #! /bin/sh -
                          pattern=$1?pattern required; shift
                          : "$1?no filename specified"
                          grep -l --null -e "$pattern" -- "$@" |
                          xargs -r0 ls -lrtd --


                          With --null/-0, we avoid problems with file names that contain blanks or quotes or backslashes.



                          With -e "$pattern", we avoid problems with patterns that start with -.



                          With --, we avoid problems with file names that start with -.



                          With -d, we avoid possible confusions if files of type directory happen to match.



                          With -r, we avoid listing the current directory if there's no matching file.



                          Note that for a big number of files, xargs may choose to run more than one ls invocation with part of the file list, which means the output would not be properly listed by time.



                          Also note that - would mean stdin to grep, but the file called - in the current directory for ls. GNU grep -l would output (standard input) (or its translation in your language). If you want to specify the file called - in the current directory, use ./- instead of -.







                          share|improve this answer













                          share|improve this answer



                          share|improve this answer











                          answered Jul 29 at 11:01









                          Stéphane Chazelas

                          277k52511841




                          277k52511841






















                               

                              draft saved


                              draft discarded


























                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f459150%2fconverting-a-working-command-into-shell-script-file-which-gets-arguments%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

                              How to check contact read email or not when send email to Individual?

                              Bahrain

                              Postfix configuration issue with fips on centos 7; mailgun relay