首頁>Club>
PowerShell與Linux Shell有什麼不同
53
回覆列表
  • 1 # 海陽頂端

    Shell是LINUX系統的使用者介面,提供了使用者與核心進行互動操作的一種介面。它接收使用者輸入的命令並把它送入核心去執行。實際上Shell是一個命令直譯器,它解釋由使用者輸入的命令並且把它們送到核心。

    Windows PowerShell 是WINOWS一種命令列外殼程式和指令碼環境,使命令列使用者和指令碼編寫者可以利用 .NET Framework的強大功能。微軟這次沒有低調,強硬的命名它為PowerShell ,看樣子想在SHELL介面裡打敗BASH。

    一、理念論

    從理念上 ps 比 bash 先進一代,ps 有完整的、強型別的程式設計支援,bash 裡面全是字串。Powershell 裡面很多的函式比 UNIX 更加正交,如 % 和 ?,可以提供極其可怕的抽象能力,你 bash 就是做不到。

    不過針對理念這一說法,也有人提:說PS的OOP理念比shell純字串先進的同學請想一想:JavaScript的物件字面量,是用字串表示物件,這樣的一種技術為什麼大受歡迎?答案是從使用者友好的角度看,字串要優於物件,而命令列恰恰是人機互動介面,這就是為什麼shell比PS易用的原因,至於比較“理念”哪個先進,對於靠程式碼吃飯的人來說就是扯淡。

    二、用法論

    1、bash 快,易用,提示好,難學。寫 script,看 script 的時候你想砸電腦。ps 寫的 script 清晰,好讀,有一點程式設計基礎,就算幾乎沒學過 powershell 都能猜出來啥是幹嘛的,但是比較慢。。。所以說平常互動的話 bash(bash-it, oh-my-git, oh-my-zsh)更好用一些,但是真正寫起 script 的時候感覺還是 powershell 爽。

    2、如果你受夠了bash靠文字解析和環境變數來解決邏輯問題,你會無比渴望一個OO的Shell。——PS簡直是大救星。隨著PS用多了,你會發現,PS的OO有些走火入魔,你又會開始懷念簡單粗暴的bash。

    三、技術論

    這個不是一兩句話能說清楚的。PS包含了BASH很多命令了,甚至我看有想法把BASH所有的命令都移到WIN下,讓BASH成為它的一個子集。不過也有人指出肯定是GNU Bash好呀,畢竟Bash作為大量Linux發行版預設的Shell,久經考驗。如果兩個具休技術細節比較的話,需要長篇大論了。這裡有兩者具體的技術的比較:http://hyperpolyglot.org/shell。

  • 2 # cqchi

    Windows PowerShell 是一種指令碼環境,使使用者可以利用.NET Framework的強大功能。

    PowerShell 由一個命令列shell和內建在這個.NET框架上的程式語言組成。

    PowerShell 採用的cmdlet讓能夠更深入到系統程序中,這些程序可以製作成可執行的檔案或指令碼(script)。一條cmdlet是一條輕量命令,比如顯示當前目錄的Get-Location,訪問檔案內容的Get-Content和結束執行程序的Stop-Process。

    Unix shell不基於.netframework。

  • 3 # 獨立的網際網路從業者

    首先最大的區別,PowerShell是Dos命令的拓展,而Linux Shell是Unix命令的一種實現,它們兩的命令完全不同。Linux Shell(Bash)是透過環境變數和文字解析來解決邏輯上問題,而PowerShell(Batch)是完全的面向物件(OO)的。其區別主要有以下幾點:

    1 bash 易用,速度快,但是難學,而且bash程式碼特別難讀,讓你讀別人的shell程式碼,你肯定會想要將自己的電腦砸掉。讓你看下Linux Bash原始碼體驗下

    *** 258,292 ****

    int result;

    - using_history ();

    -

    result = history_expand (line, &expansion);

    - strcpy (line, expansion);

    - free (expansion);

    if (result)

    ! fprintf (stderr, "%s\n", line);

    ! if (result < 0)

    ! continue;

    ! add_history (line);

    @}

    ! if (strcmp (line, "quit") == 0) done = 1;

    ! if (strcmp (line, "save") == 0) write_history (0);

    ! if (strcmp (line, "read") == 0) read_history (0);

    ! if (strcmp (line, "list") == 0)

    @{

    ! register HIST_ENTRY **the_list = history_list ();

    register int i;

    if (the_list)

    for (i = 0; the_list[i]; i++)

    ! fprintf (stdout, "%d: %s\n",

    ! i + history_base, the_list[i]->line);

    @}

    ! if (strncmp (line, "delete", strlen ("delete")) == 0)

    @{

    int which;

    ! if ((sscanf (line + strlen ("delete"), "%d", &which)) == 1)

    @{

    HIST_ENTRY *entry = remove_history (which);

    --- 435,473 ----

    int result;

    result = history_expand (line, &expansion);

    if (result)

    ! fprintf (stderr, "%s\n", expansion);

    ! if (result < 0 || result == 2)

    ! @{

    ! free (expansion);

    ! continue;

    ! @}

    ! add_history (expansion);

    ! strncpy (line, expansion, sizeof (line) - 1);

    ! free (expansion);

    @}

    ! if (strcmp (line, "quit") == 0)

    ! done = 1;

    ! else if (strcmp (line, "save") == 0)

    ! write_history ("history_file");

    ! else if (strcmp (line, "read") == 0)

    ! read_history ("history_file");

    ! else if (strcmp (line, "list") == 0)

    @{

    ! register HIST_ENTRY **the_list;

    register int i;

    + the_list = history_list ();

    if (the_list)

    for (i = 0; the_list[i]; i++)

    ! printf ("%d: %s\n", i + history_base, the_list[i]->line);

    @}

    ! else if (strncmp (line, "delete", 6) == 0)

    @{

    int which;

    ! if ((sscanf (line + 6, "%d", &which)) == 1)

    @{

    HIST_ENTRY *entry = remove_history (which);

    diff -rc2 bash-1.14.1/lib/readline/doc/hsuser.texinfo bash-1.14.2/lib/readline/doc/hsuser.texinfo

    *** bash-1.14.1/lib/readline/doc/hsuser.texinfo Fri Jul 1 11:21:14 1994

    --- bash-1.14.2/lib/readline/doc/hsuser.texinfo Thu Jul 21 15:47:19 1994

    ***************

    看上面的程式碼你肯定想殺人。

    2 PowerShell執行速度比較慢,但是由於它是面向物件的,非常易讀,即使你沒有學過PowerShell但有一些程式設計基礎,你也能大概瞭解該段PowerShell是幹嘛的。下面看一段PowerShell的程式碼,你最起碼有點賞心悅目的感覺。

    $n=1

    while($n -lt 6)

    {

    if($n -eq 4)

    {

    $n=$n+1

    continue

    }

    else

    {

    $n

    }

    $n=$n+1

    }

    但是PowerShell有個大的槽點,和cmd一樣竟然不支援複製貼上命令,太不人性化了。

  • 4 # 小胖00003

    管理windows時,使用cmd相當於使用低階指令碼,使用powershell相當於使用高階指令碼,因為powershell是可以直接呼叫.net中的類庫的。

    管理linux時,使用shell也相當於使用低階指令碼,使用python或perl相當於使用高階指令碼。

    powershell就類似於在linux中自帶的python或perl的作用了,使用高階的api或程式邏輯來簡化用cmd或shell難以實現和不能實現的指令碼。

  • 中秋節和大豐收的關聯?
  • 上瞼下垂矯正有什麼要注意的?