博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ruby 使用反引号执行 Shell 命令并保存输出结果到变量
阅读量:4230 次
发布时间:2019-05-26

本文共 2318 字,大约阅读时间需要 7 分钟。

 Shell 命令执行结果返回 

[root@master GitTest]# cat hello.txt hello worldhello worldhello world. I am Looking------------------------------------------------------------[root@master GitTest]# cat test.rbhello_world = `cat hello.txt`puts hello_world------------------------------------------------------------[root@master GitTest]# ruby test.rbhello worldhello worldhello world. I am Looking

Git 命令执行结果返回 

# test.rbauthor = `git log --pretty=format:"%an" HEAD -1`print('I am the HEAD commit author: ' + author)puts
# 在你的 Git 仓库下运行哟[root@master GitTest]# ls -altotal 32drwxr-xr-x.  4 root root  165 Aug 29 18:17 .dr-xr-x---. 22 root root 4096 Aug 29 18:17 ..-rw-r--r--.  1 root root  133 Aug 11 22:59 b.txt-rw-r--r--.  1 root root   94 Jul 29 23:19 c.txtdrwxr-xr-x.  8 root root  220 Aug 29 16:42 .git-rw-r--r--.  1 root root   50 Aug 29 15:52 hello.txt-rw-r--r--.  1 root root   36 Aug 11 22:44 looking.txt-rw-r--r--.  1 root root   19 Mar 23 17:18 myGit.txt-rw-r--r--.  1 root root    0 Mar 23 17:18 newBranch.txt-rw-r--r--.  1 root root  116 Mar 23 17:18 readme.txtdrwxr-xr-x.  2 root root   35 Jul 29 23:32 test-rw-r--r--.  1 root root   47 Mar 23 17:18 world.txt[root@master GitTest]# ruby test.rbI am the HEAD commit author: looking

Git 在指定目录执行命令结果返回

此时 test.rb 脚本可以不在对应的 git 仓库目录下,但是脚本里需要指定具体仓库目录。

# test.rbauthor = `git log --pretty=format:"%an" HEAD -1`print('I am the HEAD commit author: ' + author)puts------------------------------------------------------------[root@master ~]# ruby test.rbfatal: not a git repository (or any of the parent directories): .gitI am the HEAD commit author:
# test.rbauthor = `git -C /root/GitTest log --pretty=format:"%an" HEAD -1`print('I am the HEAD commit author: ' + author)puts------------------------------------------------------------[root@master ~]# ruby test.rbI am the HEAD commit author: looking

其实用 x%() 也是可以达到相同效果的(%x 使用方法执行一段 shell 脚本并返回标准输出内容):

# test.rbauthor = `git -C /root/GitTest log --pretty=format:"%an" HEAD -1`author2 = %x(git -C /root/GitTest log --pretty=format:"%an" HEAD -1)print('I am the HEAD commit author: ' + author.to_s)putsprint('I am the HEAD commit author: ' + author2.to_s)puts------------------------------------------------------------[root@master ruby_learning]# ruby test.rbI am the HEAD commit author: lookingI am the HEAD commit author: looking

 

转载地址:http://rcjqi.baihongyu.com/

你可能感兴趣的文章
Web前端学习笔记——JavaScript之正则表达式、伪数组、垃圾回收
查看>>
Web前端学习笔记——JavaScript 之继承、函数进阶
查看>>
Web前端学习笔记——JavaScript之面向对象游戏案例:贪吃蛇
查看>>
不做单元测试?小心得不偿失!嵌入式系统单元测试工具,自动生成测试用例
查看>>
一种实用的联网汽车无线攻击方法及车载安全协议
查看>>
光靠欺骗检测是不够的:对抗多目标跟踪的攻击
查看>>
基于微区块链的V2X地理动态入侵检测
查看>>
面向V2C场景的ADAS数字孪生模型构建方法
查看>>
Comma2k19数据集使用
查看>>
面向自动驾驶车辆验证的抽象仿真场景生成
查看>>
一种应用于GPS反欺骗的基于MLE的RAIM改进方法
查看>>
自动驾驶汽车GPS系统数字孪生建模(一)
查看>>
自动驾驶汽车GPS系统数字孪生建模(二)
查看>>
CUDA 学习(五)、线程块
查看>>
CUDA 学习(八)、线程块调度
查看>>
CUDA 学习(九)、CUDA 内存
查看>>
CUDA 学习(十一)、共享内存
查看>>
游戏感:虚拟感觉的游戏设计师指南——第十四章 生化尖兵
查看>>
游戏感:虚拟感觉的游戏设计师指南——第十五章 超级马里奥64
查看>>
游戏感:虚拟感觉的游戏设计师指南——第十七章 游戏感的原理
查看>>