欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

如何在Shell中把变量用作命令来运行

最编程 2024-07-25 18:19:24
...
########以下是脚本##
#/bin/bash
#filename test.sh
cmd1="cd /home/skybug"
cmd2="ls -al"
${cmd1}
${cmd2}
#########以上是脚本内容###
sh test.sh
可以看到 cmd1 cmd2 都执行了
如果是
###########
以下是脚本##
#/bin/bash
#filename test.sh
cmd1="cd /home/skybug &&   ls -al"
${cmd1}
########### 
以上是脚本内容###
sh test.sh
会发现没有执行。
把cmd1重定向到awk后用system()函数来执行
###########以下是脚本##
#/bin/bash
#filename test.sh
cmd1="cd /home/skybug && ls -al"
echo {cmd1} |awk ‘{run=$0;system(run)}’  #0为整行,把整行内容赋值给run变量然后system(run)来执行全部命令
########## 以上是脚本内容###
sh test.sh看结果
可执行


      本文转自天山三害 51CTO博客,原文链接:http://blog.51cto.com/skybug/1337783 ,如需转载请自行联系原作者




推荐阅读