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 '=echoe"fileLines \c"     if ((fileLines<50))thenecho"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

 

posted @   emanlee  阅读(697)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
历史上的今天:
2012-10-17 matlab当前目录下的相对路径
2009-10-17 JavaScript: Checkbox onChange event is differently processed by IE and FF
2008-10-17 How to grant permissions to a custom assembly that is referenced in a report in Reporting Services
点击右上角即可分享
微信分享提示