0

I'm trying to search a file to find a word using grep. I'm trying to use it inside of a script but I am unable to get it to work. I'm a beginner at this so my code is probably not good. Here's what I have so far

#!/bin/bash
echo "apple", "book", "cat" >> file.txt

read -r filename
filename='file.txt'
"$4"=filename
"$1"=grep
"$2"=-R

So ideally I want the user to be able to type the name of the script on the command line along with the word they are searching for, which in this case would be "book".

user:~/dir$ ./wordfind.sh book

I may be doing this completely wrong, but when I run the above in the command line, it freezes and I have to Ctrl-Z to get out of it. Thanks.

6
  • 2
    if you check against a regexp where is this regexp? Commented Dec 11, 2020 at 19:29
  • if you're going to hardcode filename='file.txt' then why ask the user for a filename (read -r filename)? btw, the script is likely 'hanging' because it's waiting for you to enter a filename (ie, read -r filename tells the script to wait for the user to input something and hit <enter>); also not sure what you're trying to accomplish with the "${4|1|2}"=.... constructs, nor how any of that's supposed to run a grep command; perhaps you want something as simple as grep "${1:-undefined}" "${filename}"? Commented Dec 11, 2020 at 19:39
  • Hi and welcome! What is the expected output? Commented Dec 11, 2020 at 19:52
  • 1
    @Madman I'm trying to get it to find the word "rice" in the file.txt. The $1, $2, etc are in the script so you don't have to type out the entire command on the command line. Instead of typing ./wordfind.sh grep -R rice file.txt , I want to just be able to type ./wordfind rice , and it finds the word rice. I hope that makes sense. Commented Dec 11, 2020 at 19:56
  • Also (re-)using positional variables in assignments is a bad idea; in your case "$1"=grep will overwrite whatever the user passed in on the command line. Commented Dec 11, 2020 at 20:16

1 Answer 1

1

The question is not clear enough.

This answer assumes that you want to call your script with a string or regex as the first argument, i.e. the search term must be quoted if it contains spaces or special characters.

You could change the script wordfind.sh to

#!/bin/bash

# note: I changed >> to > here to avoid duplicating the data on repeated calls
echo "apple", "book", "cat" > file.txt
echo "dad", "food", "girl" >> file.txt
echo "god", "hey", "linux" >> file.txt
echo "out", "owl", "rice" >> file.txt
echo "say", "toy", "zebra" >> file.txt

# Check if exactly one argument was specified.
if [ $# -ne 1 ]
then
   echo "usage: $0 'regex'"
   exit 1
fi

grep "$1" file.txt

Examples:

$ ./wordfind.sh rice
out, owl, rice

$ ./wordfind.sh 'z.*a'
say, toy, zebra

$ ./wordfind.sh 'd, f'
dad, food, girl

Sign up to request clarification or add additional context in comments.

2 Comments

That's exactly what I was looking for! Thank you for this! I'm sorry that my explanation wasn't very good, I'm still very new and trying to learn! I appreciate your patience and help!
@Andrew No need to apologize. Please improve your question as suggested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.