I'm working on a bash script that will search for a specific file using wildcard path to search, once it finds that filename it searches inside the file for a specific term and then replaces it.
Here's what i've put together already:
#!/bin/sh
PATH=${1}
FILENAME="$2"
SEARCHFOR="$3"
/usr/bin/clear
echo "Searching $PATH"
echo "For the file $FILENAME"
echo "With the string $SEARCHFOR"
echo "=========================="
echo " RESULTS "
echo "=========================="
/usr/bin/find $PATH -type f -name "$FILENAME" | /usr/bin/xargs /bin/grep -l "$SEARCHFOR"
I haven't added the replacement yet but I figured i would use SED instead of grep to do that, just using grep for testing purposes.
With the code above i have to use quotes for any type of wildcard paths. Is there a way around this?
./script '/home/*/public_html' php.ini module.so
I was thinking maybe using arguments but there's got to be another way to do it.
My ultimate goal is to have a bash script i can pass a wildcard path or filename to, it will search for that file, once it finds the file it searches for a specific term and replaces it if found.
Sounds so simple and it should be but i'm banging my head against the desk because it's been so long since i've messed with bash...aaggghh help!
$PATHoverrides thePATHvariable defined by your shell which tells it what folders to look in when running a command. For example, when you typepython myscript.pythe shell searches the semicolon-separated directories in$PATHfor an executable namedpythonand uses that -- it eliminates the need to type/usr/bin/python myscript.py. By overwriting it, you'll likely break the parts of the script that follow your definition. TLDR: You should really consider using a different name for your variable./usr/bin/clearinstead of justclearand the reasoning behind William Pursell's answer.