Accepting logfile as parameter and calling a function if filename is passed but file doesn't exist
Clash 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
linux bash shell-script
add a comment |Â
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
linux bash shell-script
add a comment |Â
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
linux bash shell-script
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
linux bash shell-script
edited Sep 13 at 21:16
Kusalananda
107k14209331
107k14209331
asked Sep 13 at 9:25
Zanders2001
353
353
add a comment |Â
add a comment |Â
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"
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 callexit 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
add a comment |Â
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"
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 callexit 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
add a comment |Â
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"
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 callexit 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
add a comment |Â
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"
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"
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 callexit 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
add a comment |Â
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 callexit 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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password