Unix Shell Script Syntax for if and grep and -gt

There are two errors in your script:

  • Nested backticks are not allowed.
  • $2/$file_z instead of $(<$2/$file_z)

Fixed:

for j in `<$2/$file_z`
do
 if grep -q "$1" "$j" > /dev/null
 # if grep "$1" "$j" > /dev/null # if your grep does not support -q
 then
  echo $j
  cp $j $3
 fi
done

More optimal:

for j in `<$2/$file_z`
do
 grep -l "$1" "$j" /dev/null && cp $j $3
done

grep itself can print the name of the file.

The same you can write also without for:

cp -v $(grep -l "$1" `<$2/$file_z`) $3

The command makes the same as your entire script.

Tags: , , , , , ,