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

时间: 2024-08-25 09:21:28

Linux运行变量中的命名脚本的相关文章

linux c程序中获取shell脚本输出的实现方法

linux c程序中获取shell脚本输出的实现方法 1. 前言Unix界有一句名言:“一行shell脚本胜过万行C程序”,虽然这句话有些夸张,但不可否认的是,借助脚本确实能够极大的简化一些编程工作.比如实现一个ping程序来测试网络的连通性,实现ping函数需要写上200~300行代码,为什么不能直接调用系统的ping命令呢?通常在程序中通过 system函数来调用shell命令.但是,system函数仅返回命令是否执行成功,而我们可能需要获得shell命令在控制台上输出的结果.例如,执行外部

Linux环境变量中PS1

Linux环境变量中PS1是很重要的环境变量: PS(Prompt Sign): 是指命令提示符,例如在Fedora 12的终端下:[[email protected] ~]$ ,在设定PS1环境变量时,我们需要用到预设的一些特殊符号来设定PS1,下面是鸟哥书上关于BASH的特殊变量. o \d :代表日期,格式为 Weekday Month Date,例如 "Mon Aug 1" o \H :完整的主机名称.举例来说,鸟哥的练习机 linux.dmtsai.tw ,那么这个主机名称就

linux 笔记--扩展正则表达式,bash脚本—变量,条件判断,算术运算

正则表达式:有两类,一:basic regexp  二:extended(扩展正则表达式)  他们的部分元字符不一样,意义也不相同 grep:使用基本正则表达式或定义的模式中过滤文本的命令,-E;使用扩展正则表达式  -A 数字:不仅显示匹配到的行,还显示后面几行  -B 数字:不仅显示匹配到的行,还显示前几行  -C 数字:不仅显示匹配到的行,还显示前后个几行 扩展正则表达式: 其中 . [] [^] * ^ $ \< \>与grep是相同的, ? 与 \?:意思相同,可以不写\  +:其前

Linux运行级详解

对于那些在DOS/Win9x/NT平台下的高级用户而言,Linux似乎是一个怪物.没有config.sys,没有 autoexec.bat,具有个人特色的机器配置不知道从何开始. 需要说明的是,很多人认为Linux是Unix的一个克隆,但是,这种说法对于Linux是不公平的.Linux比Unix更加开放,功能更强大.我们应该称之为GNU/Linux . Linux启动时,运行一个叫做init的程序,然后由它来启动后面的任务,包括多用户环境.网络等. 那么,到底什么是运行级呢?简单的说,运行级就是

C++中嵌入Lua脚本环境搭建

第一步(环境准备工作): 工具: ●LuaForWindows_v5.1.4-46.exe傻瓜式安装. 作用:此工具可以在windows环境下编译运行Lua脚本程序.安装完成后会有两个图标:Lua和SciTE.Lua是命令行,SciTE是图形运行环境,两个都可以编译运行,看个人喜好. ●VS2012大家都会,此处省略若干字... 第二步(在VS2012下新建并运行C++中嵌入Lua脚本程序): ●打开VS2012,新建一个控制台的C++空项目 ●配置Lua的安装路径和引用相关Lua库 1.右击新

Linux环境变量设置中配置文件分析(/etc/profile,~/.bashrc等)(转)

说明:在研究中发现,对于不同版本的Linux系统有着不同的文件,但是总的入口是不变的/etc/profile,下面只是展示加载顺序的研究过程,所以会有些系统没有这个文件等问题. 一.配置文件与作用域: 1.系统级别: /etc/environment:在登录时操作系统使用的文件,系统在读取profile前,设置环境文件的环境变量. /etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/profile.d目录的配置文件中搜集shell的设置.

[转] linux下shell中使用上下键翻出历史命名时出现^[[A^[[A^[[A^[[B^[[B的问题解决,Linux使用退格键时出现^H解决方法

[From] https://www.zmrbk.com/post-2030.html https://blog.csdn.net/suifengshiyu/article/details/40952771 我的理解是,如果出现如题所描述的问题,这是因为使用了不同的shell程序和对应的stty设置对应关系所综合作用的结果. 这是/bin/sh里面stty -a命令输出的信息: $ stty -a speed 38400 baud; rows 43; columns 209; line = 0;

Linux中编写Bash脚本的10个技巧

Shell 脚本编程 是你在 Linux 下学习或练习编程的最简单的方式.尤其对 系统管理员要处理着自动化任务,且要开发新的简单的实用程序或工具等(这里只是仅举几例)更是必备技能. 本文中,我们将分享 10 个写出高效可靠的 bash 脚本的实用技巧,它们包括: 1. 脚本中多写注释 这是不仅可应用于 shell 脚本程序中,也可用在其他所有类型的编程中的一种推荐做法.在脚本中作注释能帮你或别人翻阅你的脚本时了解脚本的不同部分所做的工作. 对于刚入门的人来说,注释用 # 号来定义. # TecM

Linux中执行shell脚本的4种方法

这篇文章主要介绍了Linux中执行shell脚本的4种方法总结,即在Linux中运行shell脚本的4种方法,需要的朋友可以参考下. bash shell 脚本的方法有多种,现在作个小结.假设我们编写好的shell脚本的文件名为hello.sh,文件位置在/root/bin目录中并已有执行权限(添加权限的方法:chmod +x hello.sh). 方法一:切换到shell脚本所在的目录(此时,称为工作目录)执行shell脚本: ./ 的意思是说在当前的工作目录下执行hello.sh.如果不加上