xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

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:UsersUSERNAMEAppDataLocalLxss ootfs
Ubuntu 用户主目录 C:UsersUSERNAMEAppDataLocalLxsshomeUSERNAME
root 账户主目录 C:UsersUSERNAMEAppDataLocalLxss oot

其实 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 发布文章使用:只允许注册用户才可以访问!


 
原文地址:https://www.cnblogs.com/xgqfrms/p/5859191.html