0

shell script take two arguments:

  1. Name of file to be find or searched.
  2. Directory Name where the file mentioned as argument1 resides.

I am facing problem in getting output of find command when executed via script, I tried to run same command which is used in script runs fine but inside script I am not able get the output, please help me to correct below code.

param_1=$1
param_2=$2
echo "First param: $1"
echo "Second param: $2"
###list="$(find $param_2 -name $param_1 -type f)"
temp_1="find  $2 -name "
temp_2=" \"$1\""
###temp_3=" -type f"
final_command=$temp_1$temp_2
echo $final_command
list=`$final_command`
for file in $list; do if [ -f "$file" ]; then echo "Found: $file"; fi; done

sh /home/xyz/find.sh one.txt /home/xyz/
gile
  • 5,580
  • 1
  • 25
  • 31
Abhinav
  • 691
  • 1
  • 6
  • 20
  • You're running into [BashFAQ #50](http://mywiki.wooledge.org/BashFAQ/050), among other issues. You can't safely store a list of a filenames in a scalar variable (a string), because any delimiter you might otherwise use to separate those names is actually *valid within the names themselves*. – Charles Duffy Apr 12 '18 at 20:07
  • Also see [Using Find](http://mywiki.wooledge.org/UsingFind) for detailed guidance on relevant best practices. – Charles Duffy Apr 12 '18 at 20:08
  • Charles i am new to shell script can you please correct me what wrong i am doing – Abhinav Apr 12 '18 at 20:08
  • See the links provided above, which provide detailed discussion. Also run your code through http://shellcheck.net/, and pay particular attention to warning [SC2089](https://github.com/koalaman/shellcheck/wiki/SC2089). – Charles Duffy Apr 12 '18 at 20:11
  • ...with respect to iterating over `find`'s output, also see specifically the "Actions in Bulk" section of the Using Find page linked above. – Charles Duffy Apr 12 '18 at 20:12

0 Answers0