Linux运行变量中的命名脚本

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 (because cd "" 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://stackoverflow.com/questions/3469705/how-to-run-script-commands-from-variables

http://stackoverflow.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

 

相关文章

系ubuntu 下面打开终端输入:sudo apt-get install sendmail...
依家我有1个软件goagent目录(大家懂得) 放在/home/gateman/...
其实我想讲的是 cp -L关于-L参数的解释:-L, --dereferenc...
原地址:http://www.rjgc.net/control/content/content.php?...
chroot,即 change root directory (更改 root 目录)。在 li...
简单解析下, stdin就是标准输入, stdout就是标准。举个例子...