Store a command and execute

Hi All,

I am trying to get latest folders based on some condition. The below sample script working fine for me.

#!/bin/sh 
INSTALLDIR=`pwd`
_UPGRADE_=1
CMD="ls -rtp $INSTALLDIR | grep /"
CMD1="ls -rtp $INSTALLDIR | grep / | grep -v '^server/$'"
 
Get_my_dir(){
   for SDIR in $(ls -rtp $INSTALLDIR | grep /) ; do
   echo source dir : $SDIR
   done
}
 
Get_my_dir

But when i modified the line

for SDIR in $(ls -rtp $INSTALLDIR | grep /) ; do 

to

for SDIR in $( $CMD) ; do 

This gives error. I want prepare my command and store into $CMD variable based on the boolean variable for _UPGRADE_.

Please let me know, how to run the $CMD insist of the hard coded things.

Thanks in advance.

for SDIR in $(eval $CMD); do echo ....; done
1 Like

Thanks a lot ygemici

for SDIR in $(eval $CMD); do echo ....; done

This syntax working fine in Redhat Linux.
But it not working in Solaris. Any one Please suggest the equal command in Solaris

Solaris uses a mouldy old pre-posix shell which doesn't support $( )

Either use the ksh shell Solaris also comes with, or use ` `