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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值