天天看点

微软爱上 Linux:当 PowerShell 来到 Linux 时

powershell 是一个微软开发的自动化任务和配置管理系统。它基于 .net 框架,由命令行语言解释器(shell)和脚本语言组成。

<a target="_blank"></a>

首先,导入该公共仓库的 gpg 密钥,然后将 microsoft ubuntu 仓库注册到 apt 的源中来安装 powershell:

<code>$ curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -</code>

<code>$ curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | sudo tee /etc/apt/sources.list.d/microsoft.list</code>

<code>$ sudo apt-get update</code>

<code>$ sudo apt-get install -y powershell</code>

<code>$ curl https://packages.microsoft.com/config/ubuntu/14.04/prod.list | sudo tee /etc/apt/sources.list.d/microsoft.list</code>

首先,将 microsoft redhat 仓库注册到 yum 包管理器仓库列表中,然后安装 powershell:

<code>$ sudo curl https://packages.microsoft.com/config/rhel/7/prod.repo &gt; /etc/yum.repos.d/microsoft.repo</code>

<code>$ sudo yum install -y powershell</code>

在这一节中,我们将会简单介绍下 powershell;我们将会看到如何启动 powershell,运行一些基础命令,操作文件、目录和进程。然后学习怎样列出所有可用的命令、显示命令帮助和别名。

输入以下命令来启动 powershell:

<code>$ powershell</code>

微软爱上 Linux:当 PowerShell 来到 Linux 时

在 linux 中启动 powershell

你可以通过以下命令来查看 powershell 版本:

<code>$psversiontable</code>

微软爱上 Linux:当 PowerShell 来到 Linux 时

查看 powershell 版本

在 linux 中运行基本的 powershell 命令。

<code>get-date [# 显示当前日期]</code>

<code>get-uptime [# 显示开机时间]</code>

<code>get-location [# 显示当前工作目录]</code>

1、 可以通过两种方法创建空文件:

<code>new-item tecmint.tex</code>

<code>或者</code>

<code>""&gt;tecmint.tex</code>

然后往里面添加内容并查看文件内容。

<code>set-content tecmint.tex -value "tecmint linux how tos guides"</code>

<code>get-content tecmint.tex</code>

微软爱上 Linux:当 PowerShell 来到 Linux 时

在 powershell 中创建新文件

2、 在 powershell 中删除一个文件

<code>remove-item tecmint.tex</code>

微软爱上 Linux:当 PowerShell 来到 Linux 时

在 powershell 中删除一个文件

3、 创建目录

<code>mkdir tecmint-files</code>

<code>cd tecmint-files</code>

<code>“”&gt;domains.list</code>

<code>ls</code>

微软爱上 Linux:当 PowerShell 来到 Linux 时

在 powershell 中创建目录

4、 执行长格式的列表操作,列出文件/目录详细情况,包括模式(文件类型)、最后修改时间等,使用以下命令:

<code>dir</code>

微软爱上 Linux:当 PowerShell 来到 Linux 时

powershell 中列出目录长列表

5、 显示系统中所有的进程:

<code>get-process</code>

微软爱上 Linux:当 PowerShell 来到 Linux 时

在 powershell 中显示运行中的进程

6、 通过给定的名称查看正在运行的进程/进程组细节,将进程名作为参数传给上面的命令,如下:

<code>get-process apache2</code>

微软爱上 Linux:当 PowerShell 来到 Linux 时

在 powershell 中查看指定的进程

输出中各部分的含义:

npm(k) – 进程使用的非分页内存,单位:kb。

pm(k) – 进程使用的可分页内存,单位:kb。

ws(k) – 进程的工作集大小,单位:kb,工作集由进程所引用到的内存页组成。

cpu(s) – 进程在所有处理器上所占用的处理器时间,单位:秒。

id – 进程 id (pid).

processname – 进程名称。

7、 想要了解更多,获取 powershell 命令列表:

<code>get-command</code>

微软爱上 Linux:当 PowerShell 来到 Linux 时

列出 powershell 的命令

8、 想知道如何使用一个命令,查看它的帮助(类似于 unix/linux 中的 man);举个例子,你可以这样获取命令 describe 的帮助:

<code>get-help describe</code>

微软爱上 Linux:当 PowerShell 来到 Linux 时

powershell 帮助手册

9、 显示所有命令的别名,輸入:

<code>get-alias</code>

微软爱上 Linux:当 PowerShell 来到 Linux 时

列出 powershell 命令别名

10、 最后,不过也很重要,显示命令历史记录(曾运行过的命令的列表):

<code>history</code>

微软爱上 Linux:当 PowerShell 来到 Linux 时

显示 powershell 命令历史记录

就是这些了!在这篇文章里,我们展示了如何在 linux 中安装微软的 powershell core 6.0。在我看来,与传统 unix/linux 的 shell 相比,powershell 还有很长的路要走。目前看来,powershell 还需要在命令行操作机器,更重要的是,编程(写脚本)等方面,提供更好、更多令人激动和富有成效的特性。

原文发布时间为:2017-03-13

本文来自云栖社区合作伙伴“linux中国”

继续阅读