single="ls -l"
$single
=============
multi="ls -l | grep e"
echo $multi > tmp.sh
bash tmp.sh
=============
cmd="ls -l | grep e"
bash -c "$cmd"
=============
$ cmd="echo foo{bar,baz}"
$ $cmd
foo{bar,baz}
$ eval "$cmd"
foobar foobaz
=============
=============
eval "$1"
executes the command in the current script. It can set and use shell variables from the current script, set environment variables for the current script, set and use functions from the current script, set the current directory, umask, limits and other attributes for the current script, and so on. bash "$1"
executes the command in a completely separate script, which inherits environment variables, file descriptors and other process environment (but does not transmit any change back) but does not inherit internal shell settings (shell variables, functions, options, traps, etc.).
There is another way, (eval "$1")
, which executes the command in a subshell: it inherits everything from the calling script but does not transmit any change back.
For example, assuming that the variable dir
isn't exported and $1
is cd "$foo"; ls
, then:
-
cd /starting/directory; foo=/somewhere/else; eval "$1"; pwd
lists the content of/somewhere/else
and prints/somewhere/else
. -
cd /starting/directory; foo=/somewhere/else; (eval "$1"); pwd
lists the content of/somewhere/else
and prints/starting/directory
. -
cd /starting/directory; foo=/somewhere/else; bash -c "$1"; pwd
lists the content of/starting/directory
(becausecd ""
doesn't change the current directory) and prints/starting/directory
.
=============
# 注意:脚本文件必须是UNIX格式;脚本文件中没有空行,没有注释;
cat /studies.sh | while read line do while [ 1 -eq 1 ] do declare -i fileLines fileLines=`qstat -u hy | grep lq | sed -n '$=' ` echo -e "$fileLines \c" if (($fileLines<50)) then echo "$line" eval "$line" pwd break else sleep 30 fi done done
REF:
http://*.com/questions/3469705/how-to-run-script-commands-from-variables
http://*.com/questions/4668640/how-to-execute-command-stored-in-a-variable
http://unix.stackexchange.com/questions/124590/variable-as-command-eval-vs-bash-c
http://superuser.com/questions/679958/execute-a-command-stored-in-a-variable
http://www.cyberciti.biz/tips/howto-running-commands-from-a-variable.html
http://www.tldp.org/LDP/abs/html/commandsub.html
http://ubuntuforums.org/showthread.php?t=1018434