Using whoami to search for files that mention user
Clash 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:
- It must get the user's name using the whoami command and store it in a variable called username.
- It must take a single parameter which is the name of the file to be searched.
- 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
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.
add a comment |Â
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:
- It must get the user's name using the whoami command and store it in a variable called username.
- It must take a single parameter which is the name of the file to be searched.
- 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
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.
add a comment |Â
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:
- It must get the user's name using the whoami command and store it in a variable called username.
- It must take a single parameter which is the name of the file to be searched.
- 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
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:
- It must get the user's name using the whoami command and store it in a variable called username.
- It must take a single parameter which is the name of the file to be searched.
- 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
bash shell-script shell
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.
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.
add a comment |Â
add a comment |Â
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.
add a comment |Â
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
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
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
edited 10 mins ago
answered 23 mins ago


Kusalananda
108k14210333
108k14210333
add a comment |Â
add a comment |Â
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
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
add a comment |Â
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
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
add a comment |Â
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
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
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
add a comment |Â
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
add a comment |Â
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.
NigerianWizard is a new contributor. Be nice, and check out our Code of Conduct.
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%2f473947%2fusing-whoami-to-search-for-files-that-mention-user%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