Search code examples
bashshellsolarisksh

KSH88 variable inside script


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

Solution

  • 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).