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.
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 filenametells 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 agrepcommand; perhaps you want something as simple asgrep "${1:-undefined}" "${filename}"?"$1"=grepwill overwrite whatever the user passed in on the command line.