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. How can I correct the 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/
5
  • You're running into BashFAQ #50, 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. Commented Apr 12, 2018 at 20:07
  • Also see Using Find for detailed guidance on relevant best practices. Commented Apr 12, 2018 at 20:08
  • Charles i am new to shell script can you please correct me what wrong i am doing Commented Apr 12, 2018 at 20:08
  • See the links provided above, which provide detailed discussion. Also run your code through shellcheck.net, and pay particular attention to warning SC2089. Commented Apr 12, 2018 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. Commented Apr 12, 2018 at 20:12

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.