Reputation: 774
The following bash script finds a .txt
file from the given directory path, then changes one word (change mountain to sea) from the .txt
file
#!/bin/bash
FILE=`find /home/abc/Documents/2011.11.* -type f -name "abc.txt"`
sed -e 's/mountain/sea/g' $FILE
The output I am getting is ok in this case. My problem is if I want to give the directory path as command line argument then it is not working. Suppose, I modify my bash script to:
#!/bin/bash
FILE=`find $1 -type f -name "abc.txt"`
sed -e 's/mountain/sea/g' $FILE
and invoke it like:
./test.sh /home/abc/Documents/2011.11.*
Error is:
./test.sh: line 2: /home/abc/Documents/2011.11.10/abc.txt: Permission denied
Can anybody suggest how to access directory path as command line argument?
Upvotes: 1
Views: 25923
Reputation: 780984
Your first line should be:
FILE=`find "$@" -type f -name "abc.txt"`
The wildcard will be expanded before calling the script, so you need to use "$@"
to get all the directories that it expands to and pass these as the arguments to find
.
Upvotes: 2
Reputation: 785146
You don't need to pass .*
to your script.
Have your script like this:
#!/bin/bash
# some sanity checks here
path="$1"
find "$path".* -type f -name "abc.txt" -exec sed -i.bak 's/mountain/sea/g' '{}' \;
And run it like:
./test.sh "/home/abc/Documents/2011.11"
PS: See how sed can be invoked directly from find itself using -exec
option.
Upvotes: 0