Accepting logfile as parameter and calling a function if filename is passed but file doesn't exist

Multi tool use
Multi tool use

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











up vote
1
down vote

favorite












I'm currently a bit confused regarding arguments and parameters for a question.



I currently have to accept the log filename as a parameter (For example, /var/log/secure-<yyyymmdd>) but default to /var/log/secure if no arguments are given.



How do I accept the log filename as a parameter? I think it might be the phrasing of the question, but I don't quite get what it means.



This is a snippet of the current code I have done so far before this question:



#!/bin/bash
grep " su: " /var/log/secure | while read -r abc; do
echo "$abc"
done


For the 2nd part, I have to create a function that exits with code 2 (I'm working on that currently) and afterwards, call that function if a filename is passed but the file doesn't exist. Would I do something like this? It feels wrong but not sure where.



IF [ -d /var/log/secure ]
then

else
runfunction









share|improve this question



























    up vote
    1
    down vote

    favorite












    I'm currently a bit confused regarding arguments and parameters for a question.



    I currently have to accept the log filename as a parameter (For example, /var/log/secure-<yyyymmdd>) but default to /var/log/secure if no arguments are given.



    How do I accept the log filename as a parameter? I think it might be the phrasing of the question, but I don't quite get what it means.



    This is a snippet of the current code I have done so far before this question:



    #!/bin/bash
    grep " su: " /var/log/secure | while read -r abc; do
    echo "$abc"
    done


    For the 2nd part, I have to create a function that exits with code 2 (I'm working on that currently) and afterwards, call that function if a filename is passed but the file doesn't exist. Would I do something like this? It feels wrong but not sure where.



    IF [ -d /var/log/secure ]
    then

    else
    runfunction









    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'm currently a bit confused regarding arguments and parameters for a question.



      I currently have to accept the log filename as a parameter (For example, /var/log/secure-<yyyymmdd>) but default to /var/log/secure if no arguments are given.



      How do I accept the log filename as a parameter? I think it might be the phrasing of the question, but I don't quite get what it means.



      This is a snippet of the current code I have done so far before this question:



      #!/bin/bash
      grep " su: " /var/log/secure | while read -r abc; do
      echo "$abc"
      done


      For the 2nd part, I have to create a function that exits with code 2 (I'm working on that currently) and afterwards, call that function if a filename is passed but the file doesn't exist. Would I do something like this? It feels wrong but not sure where.



      IF [ -d /var/log/secure ]
      then

      else
      runfunction









      share|improve this question















      I'm currently a bit confused regarding arguments and parameters for a question.



      I currently have to accept the log filename as a parameter (For example, /var/log/secure-<yyyymmdd>) but default to /var/log/secure if no arguments are given.



      How do I accept the log filename as a parameter? I think it might be the phrasing of the question, but I don't quite get what it means.



      This is a snippet of the current code I have done so far before this question:



      #!/bin/bash
      grep " su: " /var/log/secure | while read -r abc; do
      echo "$abc"
      done


      For the 2nd part, I have to create a function that exits with code 2 (I'm working on that currently) and afterwards, call that function if a filename is passed but the file doesn't exist. Would I do something like this? It feels wrong but not sure where.



      IF [ -d /var/log/secure ]
      then

      else
      runfunction






      linux bash shell-script






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 13 at 21:16









      Kusalananda

      107k14209331




      107k14209331










      asked Sep 13 at 9:25









      Zanders2001

      353




      353




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          To set a variable to the first command line argument, you would do



          pathname=$1


          To set it to a default value in case the argument is not available or if it's empty, use



          pathname=$1:-/var/log/secure


          The general form is $parameter:-word. This is a standard POSIX parameter expansion, and it's also documented in the manual of your shell.



          The -d test tests whether the given string corresponds to an existing directory path (or is a symbolic link to one that exists). The -f test is for regular files in the same way, and -e covers anything (will be true if the name exists, regardless of what it is a name of).



          To negate the sense of a test, you would use !, so you get



          if [ ! -f "$pathname" ]; then
          myfunction
          fi


          or, using short-circuit syntax,



          [ ! -f "$pathname" ] && myfunction


          This would call myfunction if the string in the pathname variable did not designate an existing regular file (or a link to one).




          A complete script that takes a command line argument (a path) with a default value and exits with exit status 2 if the given path does not correspond to an existing regular file.



          #!/bin/bash

          pathname=$1:-/var/log/secure

          if [ ! -f "$pathname" ]; then
          printf 'There is no file called "%s"n' "$pathname"
          exit 2
          fi

          printf 'The file "%s" existsn' "$pathname"





          share|improve this answer






















          • Thanks, so following the if statement, would this work pathname=$1:-/var/log/secure exitcode() exit 2 if [ ! -f "$pathname" ]; then exitcode fi (Sorry, not sure how to format a comment but this is how it looks i.imgur.com/5cHQpJD.png )
            – Zanders2001
            Sep 13 at 20:17











          • @Zanders2001 Yes, but I so real point in using the function when you can just conditionally call exit 2 directly.
            – Kusalananda
            Sep 13 at 20:19










          • I think its just to get a general idea of it, this is our first script that we've worked on in class (And my first bash script). Also regarding the first question, I'm still a bit confused about accepting log filename as a parameter. This is what I have so far i.imgur.com/WTx6nPR.png
            – Zanders2001
            Sep 13 at 20:32











          • @Zanders2001 See updated answer.
            – Kusalananda
            Sep 13 at 21:01










          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%2f468752%2faccepting-logfile-as-parameter-and-calling-a-function-if-filename-is-passed-but%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          5
          down vote



          accepted










          To set a variable to the first command line argument, you would do



          pathname=$1


          To set it to a default value in case the argument is not available or if it's empty, use



          pathname=$1:-/var/log/secure


          The general form is $parameter:-word. This is a standard POSIX parameter expansion, and it's also documented in the manual of your shell.



          The -d test tests whether the given string corresponds to an existing directory path (or is a symbolic link to one that exists). The -f test is for regular files in the same way, and -e covers anything (will be true if the name exists, regardless of what it is a name of).



          To negate the sense of a test, you would use !, so you get



          if [ ! -f "$pathname" ]; then
          myfunction
          fi


          or, using short-circuit syntax,



          [ ! -f "$pathname" ] && myfunction


          This would call myfunction if the string in the pathname variable did not designate an existing regular file (or a link to one).




          A complete script that takes a command line argument (a path) with a default value and exits with exit status 2 if the given path does not correspond to an existing regular file.



          #!/bin/bash

          pathname=$1:-/var/log/secure

          if [ ! -f "$pathname" ]; then
          printf 'There is no file called "%s"n' "$pathname"
          exit 2
          fi

          printf 'The file "%s" existsn' "$pathname"





          share|improve this answer






















          • Thanks, so following the if statement, would this work pathname=$1:-/var/log/secure exitcode() exit 2 if [ ! -f "$pathname" ]; then exitcode fi (Sorry, not sure how to format a comment but this is how it looks i.imgur.com/5cHQpJD.png )
            – Zanders2001
            Sep 13 at 20:17











          • @Zanders2001 Yes, but I so real point in using the function when you can just conditionally call exit 2 directly.
            – Kusalananda
            Sep 13 at 20:19










          • I think its just to get a general idea of it, this is our first script that we've worked on in class (And my first bash script). Also regarding the first question, I'm still a bit confused about accepting log filename as a parameter. This is what I have so far i.imgur.com/WTx6nPR.png
            – Zanders2001
            Sep 13 at 20:32











          • @Zanders2001 See updated answer.
            – Kusalananda
            Sep 13 at 21:01














          up vote
          5
          down vote



          accepted










          To set a variable to the first command line argument, you would do



          pathname=$1


          To set it to a default value in case the argument is not available or if it's empty, use



          pathname=$1:-/var/log/secure


          The general form is $parameter:-word. This is a standard POSIX parameter expansion, and it's also documented in the manual of your shell.



          The -d test tests whether the given string corresponds to an existing directory path (or is a symbolic link to one that exists). The -f test is for regular files in the same way, and -e covers anything (will be true if the name exists, regardless of what it is a name of).



          To negate the sense of a test, you would use !, so you get



          if [ ! -f "$pathname" ]; then
          myfunction
          fi


          or, using short-circuit syntax,



          [ ! -f "$pathname" ] && myfunction


          This would call myfunction if the string in the pathname variable did not designate an existing regular file (or a link to one).




          A complete script that takes a command line argument (a path) with a default value and exits with exit status 2 if the given path does not correspond to an existing regular file.



          #!/bin/bash

          pathname=$1:-/var/log/secure

          if [ ! -f "$pathname" ]; then
          printf 'There is no file called "%s"n' "$pathname"
          exit 2
          fi

          printf 'The file "%s" existsn' "$pathname"





          share|improve this answer






















          • Thanks, so following the if statement, would this work pathname=$1:-/var/log/secure exitcode() exit 2 if [ ! -f "$pathname" ]; then exitcode fi (Sorry, not sure how to format a comment but this is how it looks i.imgur.com/5cHQpJD.png )
            – Zanders2001
            Sep 13 at 20:17











          • @Zanders2001 Yes, but I so real point in using the function when you can just conditionally call exit 2 directly.
            – Kusalananda
            Sep 13 at 20:19










          • I think its just to get a general idea of it, this is our first script that we've worked on in class (And my first bash script). Also regarding the first question, I'm still a bit confused about accepting log filename as a parameter. This is what I have so far i.imgur.com/WTx6nPR.png
            – Zanders2001
            Sep 13 at 20:32











          • @Zanders2001 See updated answer.
            – Kusalananda
            Sep 13 at 21:01












          up vote
          5
          down vote



          accepted







          up vote
          5
          down vote



          accepted






          To set a variable to the first command line argument, you would do



          pathname=$1


          To set it to a default value in case the argument is not available or if it's empty, use



          pathname=$1:-/var/log/secure


          The general form is $parameter:-word. This is a standard POSIX parameter expansion, and it's also documented in the manual of your shell.



          The -d test tests whether the given string corresponds to an existing directory path (or is a symbolic link to one that exists). The -f test is for regular files in the same way, and -e covers anything (will be true if the name exists, regardless of what it is a name of).



          To negate the sense of a test, you would use !, so you get



          if [ ! -f "$pathname" ]; then
          myfunction
          fi


          or, using short-circuit syntax,



          [ ! -f "$pathname" ] && myfunction


          This would call myfunction if the string in the pathname variable did not designate an existing regular file (or a link to one).




          A complete script that takes a command line argument (a path) with a default value and exits with exit status 2 if the given path does not correspond to an existing regular file.



          #!/bin/bash

          pathname=$1:-/var/log/secure

          if [ ! -f "$pathname" ]; then
          printf 'There is no file called "%s"n' "$pathname"
          exit 2
          fi

          printf 'The file "%s" existsn' "$pathname"





          share|improve this answer














          To set a variable to the first command line argument, you would do



          pathname=$1


          To set it to a default value in case the argument is not available or if it's empty, use



          pathname=$1:-/var/log/secure


          The general form is $parameter:-word. This is a standard POSIX parameter expansion, and it's also documented in the manual of your shell.



          The -d test tests whether the given string corresponds to an existing directory path (or is a symbolic link to one that exists). The -f test is for regular files in the same way, and -e covers anything (will be true if the name exists, regardless of what it is a name of).



          To negate the sense of a test, you would use !, so you get



          if [ ! -f "$pathname" ]; then
          myfunction
          fi


          or, using short-circuit syntax,



          [ ! -f "$pathname" ] && myfunction


          This would call myfunction if the string in the pathname variable did not designate an existing regular file (or a link to one).




          A complete script that takes a command line argument (a path) with a default value and exits with exit status 2 if the given path does not correspond to an existing regular file.



          #!/bin/bash

          pathname=$1:-/var/log/secure

          if [ ! -f "$pathname" ]; then
          printf 'There is no file called "%s"n' "$pathname"
          exit 2
          fi

          printf 'The file "%s" existsn' "$pathname"






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 15 at 13:39

























          answered Sep 13 at 9:33









          Kusalananda

          107k14209331




          107k14209331











          • Thanks, so following the if statement, would this work pathname=$1:-/var/log/secure exitcode() exit 2 if [ ! -f "$pathname" ]; then exitcode fi (Sorry, not sure how to format a comment but this is how it looks i.imgur.com/5cHQpJD.png )
            – Zanders2001
            Sep 13 at 20:17











          • @Zanders2001 Yes, but I so real point in using the function when you can just conditionally call exit 2 directly.
            – Kusalananda
            Sep 13 at 20:19










          • I think its just to get a general idea of it, this is our first script that we've worked on in class (And my first bash script). Also regarding the first question, I'm still a bit confused about accepting log filename as a parameter. This is what I have so far i.imgur.com/WTx6nPR.png
            – Zanders2001
            Sep 13 at 20:32











          • @Zanders2001 See updated answer.
            – Kusalananda
            Sep 13 at 21:01
















          • Thanks, so following the if statement, would this work pathname=$1:-/var/log/secure exitcode() exit 2 if [ ! -f "$pathname" ]; then exitcode fi (Sorry, not sure how to format a comment but this is how it looks i.imgur.com/5cHQpJD.png )
            – Zanders2001
            Sep 13 at 20:17











          • @Zanders2001 Yes, but I so real point in using the function when you can just conditionally call exit 2 directly.
            – Kusalananda
            Sep 13 at 20:19










          • I think its just to get a general idea of it, this is our first script that we've worked on in class (And my first bash script). Also regarding the first question, I'm still a bit confused about accepting log filename as a parameter. This is what I have so far i.imgur.com/WTx6nPR.png
            – Zanders2001
            Sep 13 at 20:32











          • @Zanders2001 See updated answer.
            – Kusalananda
            Sep 13 at 21:01















          Thanks, so following the if statement, would this work pathname=$1:-/var/log/secure exitcode() exit 2 if [ ! -f "$pathname" ]; then exitcode fi (Sorry, not sure how to format a comment but this is how it looks i.imgur.com/5cHQpJD.png )
          – Zanders2001
          Sep 13 at 20:17





          Thanks, so following the if statement, would this work pathname=$1:-/var/log/secure exitcode() exit 2 if [ ! -f "$pathname" ]; then exitcode fi (Sorry, not sure how to format a comment but this is how it looks i.imgur.com/5cHQpJD.png )
          – Zanders2001
          Sep 13 at 20:17













          @Zanders2001 Yes, but I so real point in using the function when you can just conditionally call exit 2 directly.
          – Kusalananda
          Sep 13 at 20:19




          @Zanders2001 Yes, but I so real point in using the function when you can just conditionally call exit 2 directly.
          – Kusalananda
          Sep 13 at 20:19












          I think its just to get a general idea of it, this is our first script that we've worked on in class (And my first bash script). Also regarding the first question, I'm still a bit confused about accepting log filename as a parameter. This is what I have so far i.imgur.com/WTx6nPR.png
          – Zanders2001
          Sep 13 at 20:32





          I think its just to get a general idea of it, this is our first script that we've worked on in class (And my first bash script). Also regarding the first question, I'm still a bit confused about accepting log filename as a parameter. This is what I have so far i.imgur.com/WTx6nPR.png
          – Zanders2001
          Sep 13 at 20:32













          @Zanders2001 See updated answer.
          – Kusalananda
          Sep 13 at 21:01




          @Zanders2001 See updated answer.
          – Kusalananda
          Sep 13 at 21:01

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f468752%2faccepting-logfile-as-parameter-and-calling-a-function-if-filename-is-passed-but%23new-answer', 'question_page');

          );

          Post as a guest













































































          blzQsR c S8lT8MoL7,U9k,UT4h16,006NXHzYSBHmwknv9zOwbjM9zSDiLaCRogwMAES2NIttPzj
          Lj,dKviJfr

          Popular posts from this blog

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

          How many registers does an x86_64 CPU actually have?

          Displaying single band from multi-band raster using QGIS