简体   繁体   中英

ksh/bash run command line from variable

I set in cmd variable the find syntax: , I use ksh shell/bash

 # cmd=" find /usr/cti/conf -name \"*.tgz*\" "
 # echo $cmd
   find /usr/cti/conf/ -name "*.tgz*"

so why when I want to run the cmd as the following I not actually activate the find ...

 # exec $cmd
  appserver1a:/var/tmp/    ROOT #     ( this exit from the shell )

it's also not works when I run with double brackets

  exec "$cmd"
  ksh:  find /usr/cti/conf/backup -name "*.tgz*" :  not found

what is the resolution for this?

Remark I not want to set the cmd like this ( this is works )

     cmd=`  find /usr/cti/conf/backup -name "*.tgz*"  `

or

      cmd=$( find /usr/cti/conf/backup -name "*.tgz*" )

Store your command string in the variable:

cmd="find /usr/cti/conf/backup -name \"*.tgz*\""

Then, evaluate the variable contents:

eval "$cmd"

UPDATE: the safer option, according to alvits and Gordon :

cmd=(find /usr/cti/conf/backup -name "*.tgz*")
${cmd[@]}

Both solutions - the one provided below and the one posted by Eugeniu Rosca - seem to work for me.

cmd="find /usr/cti/conf/backup -name \"*.tgz*\""
exec $cmd

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM