Bash on Ubuntu on Windows ( Windows Subsystem for Linux)


1

#  Bash on ubuntu on Windows

http://www.cnblogs.com/anonymous-ufo/p/6143480.html

 

 

 

 

 

1

1

 

如何啟用Bash on ubuntu on Windows

微軟在 Build 2016 大會上宣布將 Bash Shell 帶到 Windows 10 當中進行原生集成,並進行了相關效果演示。消息一經發布,不僅在 Windows 社區,更是在 Linux 社區引起了轟動。現在我們來說說具體的 Bash on ubuntu on Windows 使用方式。

注意:Bash on Ubuntu on Windows( WSL 層)只能運行在 64 位版本Windows 10 中。

開啟Bash for Windows

現在所有先決條件都已准備就緒,准備開始實際安裝 Windows Subsystem for Linux。不過在安裝之前,建議大家先對操作系統進行 Windows Update,看下是否有適用的更新安裝。

打開「控制面板」-「卸載程序」-點擊左側的「啟用或關閉 Windows 功能」- 在彈出的窗口中勾選「Windows subsystem for Linux(Beta)」組件進行安裝。

如果想偷懶的話,可以直接在 PowerShell 中執行如下命令啟用 Windows subsystem for Linux 功能:

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

不論采用何種方式啟用此功能,都需要重啟系統之后才能生效。

使用Bash on ubuntu on Windows

首次啟用 Bash on ubuntu on Windows 之后它會自動提示要下載安裝 Ubuntu on Windows,只需敲 y 同意便會自動從 Windows Store 中下載安裝。具體的 License 許可信息可以查看命令提示的 http://aka.ms/uowterms 鏈接。

 

下載安裝完成后,我就可以直接執行 Linux Shell 命令、安裝程序等你能想到的幾乎所有事情。

 

在此我對之前的文章內容進行一些補充,如果在打開 Bash 命令行時沒有自動從 Windows Store 中下載安裝 Ubuntu 系統文件,還可以在 CDM 中執行如下命令手動安裝:

lxrun /install /y

安裝好之后可以使用 lsb_release -a 命令查看系統版本。

windows-10-bash-shell-2

管理Ubuntu系統和軟件包

Ubuntu 系統和基於 Debian 的 Linux 發行版使用 apt-get 命令來安裝軟件,apt 即(Advanced Package Tool)高級包管理工具的簡稱,由於 Bash on Windows 使用的是 Ubuntu 14.04 系統,所以也完全兼容 apt 系列命令,其中就包括:

  • apt-get update
  • apt-get upgrade
  • apt-get install
  • apt-cache search
  • apt-get remove
  • apt-get purge
  • apt-get autoremove

由於引入新的系統環境可能會引入新的安全隱患,所以在安裝好 Ubuntu on Windows 環境后,建議大家先對系統進行更新:

  1. apt-get update
  2. apt-get upgrade

windows-10-bash-shell-3

上述兩條命令可以從更新庫中獲取最新的軟件包列表,並將安全更新和最新可用版本的軟件包應用到當前系統環境中。

除了系統更新外,對軟件包的管理也與 Ubuntu 系統無異,例如要安裝 ruby 就可以使用如下命令:

apt-get install ruby

windows-10-bash-shell-4

如何使用 apt 系列命令管理 Linux 軟件包,可以直接參考 Linux 軟件包管理基本操作入門

在Bash中訪問Windows文件

Bash 環境文件和 Windows 文件通常是分開的,但還是有辦法在 Windows 中訪問 Bash 環境文件,或者在 Bash 環境訪問 Windows 文件。

當 Bash 將 Ubuntu 系統文件下載到磁盤中時,它會自動為 Linux 兼容環境創建一個目錄,這些文件夾默認是隱藏狀態,但我們也可以在 Windows 用戶賬戶的 AppData 文件夾中找到他們。

說明 路徑
Ubuntu 系統文件 C:\Users\USERNAME\AppData\Local\Lxss\rootfs
Ubuntu 用戶主目錄 C:\Users\USERNAME\AppData\Local\Lxss\home\USERNAME
root 賬戶主目錄 C:\Users\USERNAME\AppData\Local\Lxss\root

其實 Ubuntu Bash Shell 也完全可以訪問存儲於 Windows 系統分區中的文件,但 Bash 環境並不支持 c:\ 這樣的分區寫法,因此我們只能以 /mnt/c 這樣的方式代表 C 盤,以此類推,/mnt/d 當然就代表 D 盤了。

windows-10-bash-shell-5

當然對文件或文件夾的常規操作也與普通的 Linux 終端命令類似,例如:

操作 Bash環境 CMD環境
更改目錄 cd cd或chdir
列出 ls dir
移動或重命名 mv move或rename
復制 cp copy
刪除 rm del或erase
創建目錄 mkdir mkdir
文本編輯 vi或nano edit

在Bash Shell之外運行Linux命令

由於 Windows 與 Ubuntu「合體」這一先天優勢,在 Bash on Ubuntu on Windows 環境之外其實也是可以執行 Linux 命令的,這一些都要歸功於  bash -c 命令行,通過它我們可以直接在 CMD 甚至 PowerShell 中直接執行 Linux 命令:

bash -c "命令"

windows-10-bash-shell-6

 

 

1

 

如何在Windows 10使用Zsh或其它Shell

Bash shell 雖然是 Bash on Ubuntu on Windows 環境的默認 Shell 環境,但也就意味着不是不能更改。一旦默認的 Bash 環境安裝好之后,便可以使用 apt-get 安裝其它軟件包。要安裝 Zsh,可以在 Bash 中執行如下命令:

sudo apt-get install zsh

bash-on-ubuntu-on-windows-2-2

當然,如果你希望使用其它 Shell,其實 Fish shell 也可以在 Ubuntu on Windows 環境中正常使用,安裝命令如下:

sudo apt-get install fish

注意:其它 Shell 需要大家自己去嘗試了,多多動手實踐哦!

安裝好之后,要啟動 Zsh 或其它 Shell,只需在 Bash 提示符中執行 Shell 名稱即可。例如要使用 Zsh,只需執行:

zsh

首次安裝后進進 zsh shell 環境時需要創建一個配置文件,建議大家輸入 2 創建一個「推薦的」默認配置文件。

如果要離開 zsh shell 退回到 bash 環境,只需執行退出命令即可:

exit

受限於微軟對 Ubuntu on Windows 功能的實現方式,我們目前還不能繞過 Bash 直接啟動 Zsh。因此要使用 zsh shell 需要先啟動 bash 之后再手動切換過來,當然我們也可以通過編輯 .bashrc 文件的方式來實現自動切換。

你可以使用 vim 或 nano 直接編輯 .bashrc 文件,在該文件中添加如下內容:

  1. if [ -t 1 ]; then
  2. exec zsh
  3. fi

bash-on-ubuntu-on-windows-2-3

更改完成后,每次打開 bash 之后即可自動切換了 zsh shell。

更改Ubuntu on Windows用戶賬戶

當你在首次安裝 Bash on Ubuntu on Windows 環境並創建了用戶名和密碼之后,在啟動 Shell 環境時會自動使用當時創建的用戶賬戶進行登錄,但我們還是可以在需要時對用戶賬戶和密碼進行更改。然而具體的操作步驟與標准 Linux 命令不同,我們必需使用專屬的 lxrun 命令在 Bash 環境之外進行配置。

Windows 10 Bash Shell用戶賬戶如何工作

當你通過 bash.exe 或 lxrun /install 命令在 Windows 中配置好 Bash 之后,Windows 會自動要求你創建名為「UNIX user account」的 Bash Shell 用戶賬戶。如果你創建的賬戶為 billyfu 密碼為 sysgeek,則你的 Linux 用戶賬戶為 billyfu,主目錄為 /home/billyfu/,而且該賬戶會與你目前 Windows 賬戶和密碼完全獨立。

每個 Windows 用戶本身就有自己的用戶環境,包括獨立的配置文件、已安裝的程序和配置設置。如果同一計算機上的多個 Windows 用戶都配置了 Ubuntu on Windows 環境,他們將會被要求創建自己所屬用戶賬戶的 Bash 環境。

換句話說,每個 Windows 用戶都有自己獨立的 Ubuntu Bash 沙箱環境。

更改Ubuntu on Windows用戶賬戶

此前已經提到過,要變更 Ubuntu Bash shell 中的默認用戶帳戶,需要在 CDM 或 Powershell 中進行配置:

1使用 Windows + X 選擇並打開「命令提示符(管理員)」

2命令如下命令即可替換默認賬戶

lxrun /setdefaultuser username

3如果你的 Bash 環境中並無該賬戶,則會立即創建並要求輸入密碼

bash-on-ubuntu-on-windows-2-4

4如果你想直接使用 root 賬戶作為默認用戶,可以使用如下命令:

lxrun /setdefaultuser root

除了上述介紹的 lxrun 命令之外,其它用戶賬戶方面的操作都與標准 Linux 環境類似,使用使用 passwd 命令可以更改賬戶密碼、su 命令可以切換用戶、adduser 可以添加新賬戶等。

卸載和重裝Ubuntu on Windows

隨着我們不斷的折騰,可能會把 Ubuntu on Windows 環境給玩壞,如果隨時有需要,可以使用如下的命令來進行卸載或重裝:

  • lxrun /uninstall : 卸載環境(保存用戶 Home 目錄)
  • lxrun /uninstall /full :完全卸載 Ubuntu 環境
  • lxrun /install :重新安裝 Bash on Ubuntu on Windows 環境

bash-on-ubuntu-on-windows-2-5

 

1

1

1

1

Linux Shell腳本系列

http://linux.vbird.org/linux_basic/0320bash.php#bash

硬體、核心與使用者的相關性圖示

什么是(Unix) Shell

Unix Shell 即大家俗稱的 Shell 是源於 Unix/Linux 的命令行解釋器,用戶在 Shell 中執行的一系列命令都會被 Shell 命令行解釋器直接執行。現代流行的命令行解釋功能都已被內置到 Linux Shell 中進行支持,例如:通配符、 管道、 變量、條件操作、 命令及幫助文件等。簡單來說,我們可以把 Linux 理解為用戶與 Linux 操作系統進行交互的一個接口。

常見Shell類型

Linux 中的各種 Shell 類型有很多,但 Bourne Shell 和 C shell 應用最為廣泛,事實上 Bourne Shell 已經成為 Shell 附加功能及一些衍生功能的編碼基礎。

  • Bourne Shell (sh:Bourne shell 又名 sh,在 Linux 中的標准路徑為 /bin/sh,它由 Stephen Bourne 編寫創建,其之所以如此出名是因為 sh 作為其它 Shell 的大量符號/硬鏈接而存在。(Bourne Shell 的 License 仍在討論中。)
  • C Shell (csh):C Shell 又名 csh,由 Billy Joy 編寫創建並被 BSD Unix 廣泛使用。C Shell 引入許多功能來完善交互,如:別名,歷史操作,任務控制等。它完全由 C 語言寫成,並使用 BSD license。
  • Fish ():Fish 是友好人機交互的代表,它改變了用戶與 Linux Shell 在通用變量、友好錯誤信息、Tab 補全、語法高亮、智能終端和剪貼板處理等方面的交互方式。(Fish 基於 GPL v2)
  • Bourne-Again Shell (Bash ):作為 GNU 項目的一部分,Bourne-Again Shell 是目前大多數 Linux 發行版和 Mac OS X 默認使用的 Shell。(它主要由 C 語言寫成並基於 GPLv3 發布)

其它不太常用的 Linux Shell 還包括:

  • Debian Almquist shell 別名 dash
  • Korn Shell 別名 ksh
  • Z shell 別名 zh
  • Busybox
  • ……

什么是Shell腳本

Shell 腳本主要由 Linux Shell 解釋執行,可被用於自動化的程序執行、文件操作、環境配置等任務,Linux 管理員通常使用 Shell 腳本來執行一些重復性的體力勞動。

Shell 腳本可將多個要執行的命令和任務提交到 Linux Shell 自動執行,而無需用戶手動觸發。你還可以在單個腳本中將一個命令的輸出定義為另一個命令的輸入。

總的來說,Shell 腳本編寫是 Linux 管理員節省時間和資源的必備技能。

如何編寫Shell腳本

在新手階段,我們比較建議大家使用 gedit、vi/vim、nano 或 Emacs 來編寫 Shell 腳本,而不要使用其它 IDE 編輯器。Bash 腳本必需以如下代碼作為首行:

#!/bin/bash

上述字符中的 #! 被稱為 shebang,后面跟的 /bin/bash 表明要使用 /bin/bash 來執行。除首行(意義特殊)用於指定腳本類型外,其余以 # 打頭的內容會被當作注釋被 Shell 忽略執行。

腳本寫成后,你可將該文件保存成任意擴展名甚至無擴展名,但業界公認慣例以 .sh 作為 Linux Shell 腳本的擴展名,建議大家也別搞特殊。而且要在賦予腳本執行權限之后才能被執行,建議大家采用 775 權限,千萬不要 777:

chmod 755 sysgeekdemo.sh

編寫首個Shell腳本

打開你使用的文本編輯器,粘貼如下內容:

  1. #!/bin/bash
  2. echo "Hello World!"

將其保存成 1st.sh 再執行看看效果:

  1. chmod 755 1st.sh
  2. ./1st.sh

Linux Shell

執行成功了,是不是非常簡單。

下面我們再建立一個 2nd.sh 並粘入如下內容:

  1. #!/bin/bash
  2. echo "What is your name?"
  3. read a;
  4. echo "Hey $a! what is your Favorite Website?"
  5. read b;
  6. echo -e "That's Nice to know @$a that $b is your favorite Website"

大家看看執行之后發生了什么呢?

Linux Shell

  1. echo 后的「What is your name?」進行了正確輸出,隨后你輸入的名字被存到了變量 a 當中,讓命令可以從文件描述符中進行讀取。
  2. echo 后跟的 「Hey $a! what is your Favorite Website?」中的 $a 變量進行了賦值輸出,並將輸入的網址存入了變量 b,在第三行進行輸出。

 

 

 

1

Linux系統提供多種不同的Shell以供選擇。常用的有Bourne Shell(簡稱sh)、C-Shelll(簡稱csh)、Korn Shell(簡稱ksh)和Bourne Again Shell (簡稱bash)。
  (1)Bourne Shell(sh)是AT&T Bell實驗室的 Steven Bourne為AT&T的Unix開發的,它是Unix的默認Shell,也是其它Shell的開發基礎。Bourne Shell在編程方面相當優秀,但在處理與用戶的交互方面不如其它幾種Shell。
  
(2)C Shell(csh)是加州伯克利大學的Bill Joy為BSD Unix開發的,與sh不同,它的語法與C語言很相似。它提供了Bourne Shell所不能處理的用戶交互特征,如命令補全、命令別名、歷史命令替換等。但是,C Shell與BourneShell並不兼容。
  
(3)Korn Shell(ksh)是AT&T Bell實驗室的David Korn開發的,它集合了C Shell和Bourne Shell的優點,並且與Bourne Shell向下完全兼容。Korn Shell的效率很高,其命令交互界面和編程交互界面都很好。
  
(4)Bourne Again Shell(即bash)是自由軟件基金會(GNU)開發的一個Shell,它是Linux系統中一個默認的Shell。Bash不但與Bourne Shell兼容,還繼承了C Shell、Korn Shell等優點。以上就是幾種常見的shell類型的全文介紹,希望對您學習和使用linux系統開發有所幫助.

 

 

1

Types of Shells in Linux

In addition to graphical user interfaces like Gnome, KDE and MATE, the Linux operating system also offers several shells. These command-line interfaces provide powerful environments for software development and system maintenance. Though shells have many commands in common, each type has unique features. Over time, individual programmers come to prefer one type of shell over another; some develop new, enhanced shells based on previous ones. Unix also has an ecosystem of different shells; Linux carries this practice into the open-source software arena.

sh

The Bourne shell, called "sh," is one of the original shells, developed for Unix computers by Stephen Bourne at AT&T's Bell Labs in 1977. Its long history of use means many software developers are familiar with it. It offers features such as input and output redirection, shell scripting with string and integer variables, and condition testing and looping.

bash

The popularity of sh motivated programmers to develop a shell that was compatible with it, but with several enhancements. Linux systems still offer the sh shell, but "bash" -- the "Bourne-again Shell," based on sh -- has become the new default standard. One attractive feature of bash is its ability to run sh shell scripts unchanged. Shell scripts are complex sets of commands that automate programming and maintenance chores; being able to reuse these scripts saves programmers time. Conveniences not present with the original Bourne shell include command completion and a command history.

csh and tcsh

Developers have written large parts of the Linux operating system in the C and C++ languages. Using C syntax as a model, Bill Joy at Berkeley University developed the "C-shell," csh, in 1978.

Ken Greer, working at Carnegie-Mellon University, took csh concepts a step forward with a new shell, tcsh, which Linux systems now offer. Tcsh fixed problems in csh and added command completion, in which the shell makes educated "guesses" as you type, based on your system's directory structure and files. Tcsh does not run bash scripts, as the two have substantial differences.

ksh

David Korn developed the Korn shell, or ksh, about the time tcsh was introduced. Ksh is compatible with sh and bash. Ksh improves on the Bourne shell by adding floating-point arithmetic, job control, command aliasing and command completion. AT&T held proprietary rights to ksh until 2000, when it became open source.

About the Author

Chicago native J.T. Barett has a Bachelor of Science in physics from Northeastern Illinois University and has been writing since 1991. He has contributed to "Foresight Update," a nanotechnology newsletter from the Foresight Institute. He also contributed to the book, "Nanotechnology: Molecular Speculations on Global Abundance."

 

 

1

1


©xgqfrms 2012-2020

www.cnblogs.com 發布文章使用:只允許注冊用戶才可以訪問!


 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM