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 calledusername
.- 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 add, should consist of two lines.
How can I reduce this to only two lines?