0

I'm having trouble with KSH88

script="find . ! \( "$result" \) -mtime "$older" -xdev -type f -size +"$minsize"M -exec ls -lh {} \; | head -100 | awk '{print \$8}' | sort -rn"

files_to_delete=`$script`

When I Echo my files_to_delete variable I get :

find . ! \( -name '*.jar' -o -name '*.zip' -o -name '*.rar' -o -name '*.log' -o -name '*.xml' \) -mtime 10 -xdev -type f -size +100M -exec ls -lh {} \; | head -100 | awk '{print $8}' | sort -rn

which is what I want, when I execute it on the command line it works, but when I execute it in my KSH I get

find: bad option \(
find: [-H | -L] path-list predicate-list
Santo
  • 182
  • 1
  • 1
  • 13
  • 3
    Hint: Don't use variables! Use a function or an array to store the commands! – Inian Aug 03 '18 at 08:04
  • How to store it in an array or in a function ? I have an array of my exceptions (*.jar *.zip...) but I don't know how to use the -name and -o dynamically in the function ! P.S I read the duplicate link and didn't understand – Santo Aug 03 '18 at 08:56
  • 1
    Please try putting "eval " in front of the "$script", so it becomes files_to_delete=\`eval $script\` I don't have a pure ksh88 environment to try this on, but it does make a difference in bash. – simon3270 Aug 03 '18 at 11:31
  • Why do you not replace `\(` with a `'('`? – Jdamian Aug 03 '18 at 11:42
  • The backslash escape is needed because find needs to have the ( and ) as parameters. If you remove the \, the shell tries to interpret it. – simon3270 Aug 03 '18 at 20:21
  • Simon3270: It's working \o/ thanks ! – Santo Aug 06 '18 at 09:25

1 Answers1

1

Put "eval " in front of the "$script", so it becomes

  files_to_delete=`eval $script`

This forces the shell to evaluate the command string.

If your shell supports it, it woudl be better to use files_to_delete=$(eval $script). The ` version is easier to miss when scanning the script quickly, and much harder to nest (commands within commands).

simon3270
  • 722
  • 1
  • 5
  • 8