directory path as command line argument in bash
22,393
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
.
Author by
user227666
Updated on November 23, 2020Comments
-
user227666 over 3 years
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?
-
Marc B over 10 years
./test.sh *.txt
will make the shell expand that wildcard before test.sh gets invoked, so it'd be functionally identical to./test.sh file1.txt file2.txt file3.txt etc...
-
-
Barmar over 10 yearsThe name of the directory isn't
2011.11
, it's2011.11.10
. He needs the wildcard to make it find the directory with any suffix.