PowerShell VS Bash



PowerShell VS Bash

说明下,现在windows 10上有PowerShell 和 Bash, Linux和Mac上也有bash和PowerShell


很多人说bash很好用啊,特别是sed/awk之类的程序,应该很少人用PowerShell吧?

但是微软又不傻,PowerShell肯定有它的强大之处。

首先,PowerShell是一个工具也是一门脚本语言,微软基于.net/.net core开发的,bash是linux开源的一个命令行程序或者文件,当然你也可以写shell脚本。

现在bash能做的事情,PowerShell也能做,PowerShell的强大之处是它可以管理windows服务器(特别是域domain),现在的开源PowerShell 也可以管理Linux和Mac(通过PSRP)。

其次,PowerShell能做下面这些事:

.NET/.NET CORE编程, Registry, COM, WMI, ADSI. Exchange, Sharepoint, Systems Center, Hyper-V, SQL. VMware vCenter, Cisco UCS, Citrix XenApp and XenDesktop.,Azure, Excel 和 Office applications.基本上所有的微软产品都提供PowerShell接口。

另外,PowerShell是处理的是一个对象,而bash则不是。


现在你可以使用PowerShell来管理所有的机器(Windows/Linux/Mac),性能方面,我们来写个测试程序

先看输出。运行下面脚本/root/test.sh

#!/bin/bash
START=$(date +%s%N);
for i in {1..10000}
do
   #true
   echo "Welcome $i times"
done
END=$(date +%s%N);
echo $((END-START)) | awk '{print int($1/1000000) "ms"}'


Putty上bash输出:1147ms

Putty上PowerShell输出:1366ms

目前来说bash是快了一点。


看下处理字符串的能力:

http://jon.netdork.net/2012/03/07/powershell-vs-bash-string-processing-face-off/


PowerShell VS Bash

生成deleted_imgs.csv

echo 'Vehicle_ID,URL,DateDeleted,PhotoDeletedID
58331549,\3860\1111111174WC448544WC44854279176192.jpg,2012-03-03 11:37:35.080000000,224516659
58331549,\3860\1111111174WC448544WC44854279176194.jpg,2012-03-03 11:37:38.420000000,224516660
58331995,\2635\1111111199E1648779E164877279175843.jpg,2012-03-03 11:41:35.510000000,224516661
58050027,\5524\56840113_02_222222225WF265239_9.jpg,2012-03-03 12:42:41.537000000,224516931'>/root/deleted_imgs.csv

PowerShell处理:

$ck = [System.Diagnostics.Stopwatch]::StartNew()

$base = "/root"
$file = "/root/deleted_imgs.csv"
$sql = "delete from photo_deleted where PhotoDeletedID = {0}"

$p_file = "/root/ps_file.txt"
$p_sql = "/root/ps_sql.txt"

$pf = [System.IO.File]::CreateText($p_file)
$ps = [System.IO.File]::CreateText($p_sql)

$fh = [System.IO.File]::OpenText($file)
try {
    for (;;)
    {
        $line = $fh.ReadLine()
        if ($line -eq $null) { break; }
        
        if ($line -like '*_02_*') {
            $data = $line.Split(',')
        
            $file = $base + $data[1]
            $rem = $sql -f $data[3]
            
            $pf.WriteLine($file)
            $ps.WriteLine($rem)

        }
    }
}finally {
    $fh.Close()
}

$pf.Close()
$ps.Close()

$ck.Stop()
$ck.Elapsed


Bash处理:

#!/bin/bash

exec 5<> "/root/bash_sql2.txt"
exec 6<> "/root/bash_files2.txt"

OIFS=${IFS}
IFS=","
while read -r line
do
        #echo "${line}"
        if [[ "${line}" == *_02_* ]];
        then
                DATA=(${line})
                ID="${DATA[3]}"
                URL="${DATA[1]}"
#               ID=`echo "${line}" | cut -d, -f4`
#               URL=`echo "${line}" | cut -d, -f2`

                echo "delete from photo_deleted where PhotoDeletedID = ${ID}" >&5
                echo "/root${URL}" >&6
        fi

done < "/root/deleted_imgs.csv"

IFS=${OIFS}

exec 5>&-
exec 6>&-


在同一台Ubuntu上运行结果:Bash:2毫秒,PowerShell:一次0.75毫秒,一次4.0毫秒,一次1.58毫秒,一次0.76毫秒,一次4.8毫秒,看来这个PowerShell版本好像不稳定额。。。(因为还在开发中)我使用的版本是5.1.10032.0,不是最新的,下次我用最新的版本试下,看是不是还是这样不稳定。


PS /workspace/PowerShell-1> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.10032.0
PSEdition                      PowerShellCore
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   3.0.0.0
GitCommitId                    v6.0.0-alpha.7
CLRVersion
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1