Skip to content

Commit

Permalink
执行系统指令:小如麻雀的函数
Browse files Browse the repository at this point in the history
  • Loading branch information
ixxmu committed Oct 10, 2023
1 parent 9a78e98 commit 31b0245
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docs/2023-10/执行系统指令_小如麻雀的函数.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: "执行系统指令:小如麻雀的函数"
date: 2023-10-10T01:03:08Z
draft: ["false"]
tags: [
"fetched",
"YuLabSMU"
]
categories: ["Acdemic"]
---
执行系统指令:小如麻雀的函数 by YuLabSMU
------
<div><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" data-mpa-powered-by="yiban.io"><p data-tool="mdnice编辑器">再给大家介绍一下<code>yulab.utils</code>包中的一个小函数。小到只有几行,但是非常方便。</p><p data-tool="mdnice编辑器">首先安装包是:</p><pre data-tool="mdnice编辑器"><span></span><code>install.packages('yulab.utils')<br></code></pre><p data-tool="mdnice编辑器">装好之后,加载:</p><pre data-tool="mdnice编辑器"><span></span><code>library(yulab.utils)<br></code></pre><p data-tool="mdnice编辑器">然后就可以使用了,这次给大家介绍的是<code>exec()</code>函数,它能够运行系统命令。假设你在使用R的时候,想跑一下命令行程序,切换终端总是有点不爽是吧?那么你就可以在R里面运行它,这个不难,用<code>system()</code>指令。<code>exec()</code>也是调用一句<code>system()</code>来干这活。但我给它搞得稍微方便一点,仅此而已。</p><p data-tool="mdnice编辑器">使用就是把指令传给它,比如用<code>ls</code>列出文件,输出会直接打印出来:</p><pre data-tool="mdnice编辑器"><span></span><code>&gt; <span>exec</span>(<span>'ls -l'</span>)<br>total 32<br>-rw-r--r-- 1 HUAWEI HUAWEI 1157 Oct  6 10:34 DESCRIPTION<br>-rw-r--r-- 1 HUAWEI HUAWEI  953 Jun 20 10:26 Makefile<br>-rw-r--r-- 1 HUAWEI HUAWEI 1562 Aug  5 16:05 NAMESPACE<br>-rw-r--r-- 1 HUAWEI HUAWEI 4054 Oct  6 10:35 NEWS.md<br>drwxr-xr-x 1 HUAWEI HUAWEI    0 Oct  6 10:32 R<br>-rw-r--r-- 1 HUAWEI HUAWEI 1631 Jun  5 15:53 README.Rmd<br>-rw-r--r-- 1 HUAWEI HUAWEI 1854 Jun  5 15:53 README.md<br>drwxr-xr-x 1 HUAWEI HUAWEI    0 Sep 13 14:23 inst<br>drwxr-xr-x 1 HUAWEI HUAWEI    0 Sep 13 14:23 man<br></code></pre><p data-tool="mdnice编辑器">如果我想把输出存起来呢?因为我可能从输出中抽取出想要的信息,直接赋值就好了:</p><pre data-tool="mdnice编辑器"><span></span><code>x &lt;- exec('ls -l')<br></code></pre><p data-tool="mdnice编辑器">每一行输出是一个字符串:</p><pre data-tool="mdnice编辑器"><span></span><code>&gt; x[1]<br>[1] "total 32"<br>&gt; x[2]<br>[1] "-rw-r--r-- 1 HUAWEI HUAWEI 1157 Oct 6 10:34 DESCRIPTION"<br>&gt; x[3]<br>[1] "-rw-r--r-- 1 HUAWEI HUAWEI 953 Jun 20 10:26 Makefile"<br></code></pre><p data-tool="mdnice编辑器">那么我就可以用正则表达式去抽取结果了,比如我想把修改文件的时间拿出来:</p><pre data-tool="mdnice编辑器"><span></span><code>sub(".*(\\w{3}\\s+\\d{1-2}).*", "\\1", x[-1])<br>[1] "Oct 6" "Jun 2" "Aug 5" "Oct 6" "Oct 6" "Jun 5" "Jun 5" "Sep 1"<br>[9] "Sep 1"<br></code></pre><p data-tool="mdnice编辑器">当然另一种做法是我把结果给整理了,变成格式化的输出,然后想要啥就直接拿啥,比如当前这个例子,显然就可以用空白来分割出字段,让我们试试:</p><pre data-tool="mdnice编辑器"><span></span><code>&gt; strsplit(x[2:3], "\\s+")<br>[[1]]<br>[1] "-rw-r--r--" "1" "HUAWEI" "HUAWEI" "1157"<br>[6] "Oct" "6" "10:34" "DESCRIPTION"<br><br>[[2]]<br>[1] "-rw-r--r--" "1" "HUAWEI" "HUAWEI" "953"<br>[6] "Jun" "20" "10:26" "Makefile"<br></code></pre><p data-tool="mdnice编辑器">那么全部一起上,再规整一下,下面这个直接就是输出一个矩阵了,想要啥,直接就是下标去取了。</p><pre data-tool="mdnice编辑器"><span></span><code>&gt; strsplit(x[-1], "\\s+") |&gt; rbindlist()<br> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]<br> [1,] "-rw-r--r--" "1" "HUAWEI" "HUAWEI" "1157" "Oct" "6" "10:34"<br> [2,] "-rw-r--r--" "1" "HUAWEI" "HUAWEI" "953" "Jun" "20" "10:26"<br> [3,] "-rw-r--r--" "1" "HUAWEI" "HUAWEI" "1562" "Aug" "5" "16:05"<br> [4,] "-rw-r--r--" "1" "HUAWEI" "HUAWEI" "4054" "Oct" "6" "10:35"<br> [5,] "drwxr-xr-x" "1" "HUAWEI" "HUAWEI" "0" "Oct" "6" "10:32"<br> [6,] "-rw-r--r--" "1" "HUAWEI" "HUAWEI" "1631" "Jun" "5" "15:53"<br> [7,] "-rw-r--r--" "1" "HUAWEI" "HUAWEI" "1854" "Jun" "5" "15:53"<br> [8,] "drwxr-xr-x" "1" "HUAWEI" "HUAWEI" "0" "Sep" "13" "14:23"<br> [9,] "drwxr-xr-x" "1" "HUAWEI" "HUAWEI" "0" "Sep" "13" "14:23"<br> [,9]<br> [1,] "DESCRIPTION"<br> [2,] "Makefile"<br> [3,] "NAMESPACE"<br> [4,] "NEWS.md"<br> [5,] "R"<br> [6,] "README.Rmd"<br> [7,] "README.md"<br> [8,] "inst"<br> [9,] "man"<br></code></pre></section><p><img data-backh="511" data-backw="578" data-galleryid="" data-ratio="0.8837719298245614" data-s="300,640" data-type="png" data-w="912" data-src="https://mmbiz.qpic.cn/mmbiz_png/MPBFtnFrw4lXrUAdk6aL3ONcJntOO7d6PSesPLVjBGC21iaicDp2zYNMvn5cTp7NbFLFzfO27b0Gux5ulx94gZ7Q/640?wx_fmt=png" src="https://mmbiz.qpic.cn/mmbiz_png/MPBFtnFrw4lXrUAdk6aL3ONcJntOO7d6PSesPLVjBGC21iaicDp2zYNMvn5cTp7NbFLFzfO27b0Gux5ulx94gZ7Q/640?wx_fmt=png"></p><p><mp-style-type data-value="3"></mp-style-type></p></div>
<hr>
<a href="https://mp.weixin.qq.com/s/8h7hPXsrwDjzVoC4Qfv58A",target="_blank" rel="noopener noreferrer">原文链接</a>

0 comments on commit 31b0245

Please sign in to comment.