shell bash脚本_如何在Windows 10上创建和运行Bash Shell脚本

shell bash脚本

shell bash脚本

With the arrival of Windows 10’s Bash shell, you can now create and run Bash shell scripts on Windows 10. You can also incorporate Bash commands into a Windows batch file or PowerShell script.

随着Windows 10 Bash Shell的到来,您现在可以在Windows 10上创建并运行Bash Shell脚本。您还可以将Bash命令合并到Windows批处理文件或PowerShell脚本中。

Even if you know what you’re doing, this isn’t necessarily as simple as it seems. Windows and UNIX use different end-of-line characters, and the Windows file system is accessible in a different location in the Bash environment.

即使您知道自己在做什么,也不一定像看起来那样简单。 Windows和UNIX使用不同的行尾字符,并且Windows文件系统可在Bash环境中的其他位置访问。

如何在Windows 10上编写Bash脚本 (How to Write a Bash Script on Windows 10)

When writing shell scripts on Windows, bear in mind that Windows and UNIX-like systems like Linux use different “end of line” characters in text files in shell scripts.

在Windows上编写Shell脚本时,请记住Windows和类似Linux的UNIX系统在Shell脚本的文本文件中使用不同的“行尾”字符。

In other words, this means that you can’t simply write a shell script in Notepad. Save the file in Notepad and it won’t be interpreted properly by Bash. However, you can use more advanced text editors–for example, Notepad++ allows you to give a file UNIX end-of-line characters by clicking Edit > EOL Conversion > UNIX/OSX Format.

换句话说,这意味着您不能简单地在记事本中编写Shell脚本。 将文件保存在记事本中,Bash无法正确解释。 但是,您可以使用更高级的文本编辑器-例如, Notepad ++允许您通过单击编辑> EOL转换> UNIX / OSX格式来给文件提供UNIX行尾字符。

However, you’re better off just writing the shell script in the Bash environment itself. The Ubuntu-based Bash environment comes with both the vi and nano text editors. The vi editor is more powerful, but if you’ve never used it before, you may want to start with nano. It’s easier to use if you’re new.

但是,最好只在Bash环境本身中编写Shell脚本。 基于Ubuntu的Bash环境随附vinano文本编辑器。 vi编辑器功能更强大,但如果您以前从未使用过它,则可能要从nano开始。 如果您是新手,使用起来会更容易。

For example, to create a bash script in nano, you’d run the following command in bash:

例如,要在nano中创建bash脚本,可以在bash中运行以下命令:

nano ~/myscript.sh

This would open the Nano text editor pointed at a file named “myscript.sh” in your user account’s home directory. (The “~” character represents your home directory, so the full path is /home/username/myscript.sh.)

这将打开Nano文本编辑器,该编辑器指向用户帐户主目录中名为“ myscript.sh”的文件。 (“〜”字符表示您的主目录,因此完整路径为/home/username/myscript.sh。)

Start your shell script with the line:

从以下行开始您的shell脚本:

#!/bin/bash

Enter the commands you want to run, each one on its own line. The script will run each command in turn. Add a “#” character before a line to treat it as a “comment”, something which helps you and other people understand the script but which isn’t run as a command. For more advanced tricks, consult a more detailed guide to Bash scripts on Linux. The same techniques will work in Bash on Ubuntu on Windows.

输入要运行的命令,每个命令都单独一行。 该脚本将依次运行每个命令。 在行之前添加“#”字符以将其视为“注释”,这可以帮助您和其他人理解脚本,但不能作为命令运行。 有关更多高级技巧,请查阅有关Linux上Bash脚本的更详细指南 。 相同的技术将在Windows上的Ubuntu的Bash中工作。

Note that there’s no way to run Windows programs from within the Bash environment. You’re restricted to Linux terminal commands and utilities, just as you would be on a typical Linux system.

请注意,无法在Bash环境中运行Windows程序。 就像在典型的Linux系统上一样,您只能使用Linux终端命令和实用程序。

For example, let’s just use a basic “hello world” script as an example here:

例如,我们在这里仅使用一个基本的“ hello world”脚本作为示例:

#!/bin/bash
# set the STRING variable
STRING="Hello World!"
# print the contents of the variable on screen
echo $STRING

If you’re using the Nano text editor, you can save the file by pressing Ctrl+O and then Enter. Close the editor by pressing Ctrl+X.

如果使用的是Nano文本编辑器,则可以通过按Ctrl + O,然后按Enter来保存文件。 通过按Ctrl + X关闭编辑器。

使脚本可执行,然后运行 (Make the Script Executable and then Run It)

You’ll probably want the make the script executable so you can run it more easily. On Linux, that means you need to give the script file the executable permission. To do so, run the following command in the terminal, pointing it at your script:

您可能需要使脚本可执行,以便您可以更轻松地运行它。 在Linux上,这意味着您需要授予脚本文件可执行权限。 为此,请在终端中运行以下命令,将其指向您的脚本:

chmod +x ~/myscript.sh

To run the script, you can now just run it in the terminal by typing its path. Whenever you want to launch the script in the future, just open the Bash shell and type the path to the script.

要运行脚本,您现在可以在终端中通过键入其路径来运行它。 每当您以后想要启动脚本时,只需打开Bash shell并键入脚本的路径即可。

~/myscript.sh

(If the script is in the current directory, you can run it with ./myscript.sh)

(如果脚本位于当前目录中,则可以使用./myscript.sh运行它)

如何在Bash脚本中使用Windows文件 (How to Work With Windows Files in a Bash Script)

To access Windows files in the script, you’ll need to specify their path under /mnt/c, not their Windows path. For example, if you wanted to specify the C:\Users\Bob\Downloads\test.txt file, you’d need to specify the /mnt/c/Users/Bob/Downloads/test.txt path. Consult our guide to file locations in Windows 10’s Bash shell for more details.

要访问脚本中的Windows文件,您需要在/ mnt / c下指定其路径,而不是Windows路径。 例如,如果要指定C:\ Users \ Bob \ Downloads \ test.txt文件,则需要指定/mnt/c/Users/Bob/Downloads/test.txt路径。 有关更多详细信息,请参阅Windows 10 Bash shell中文件位置的指南

如何将Bash命令合并到批处理或PowerShell脚本中 (How to Incorporate Bash Commands into a Batch or PowerShell Script)

Lastly, if you have an existing batch file or PowerShell script you want to incorporate commands into, you can run Bash commands directly using the bash -c command.

最后,如果您想将命令合并到现有的批处理文件或PowerShell脚本中 ,则可以使用bash -c命令直接运行Bash命令。

For example, to run a Linux command in a Command Prompt or PowerShell window, you can run the following command:

例如,要在命令提示符或PowerShell窗口中运行Linux命令,可以运行以下命令:

bash -c "command"

This trick allows you to add Bash commands into batch files or PowerShell scripts. The Bash shell window will appear when a Bash command is running.

此技巧使您可以将Bash命令添加到批处理文件或PowerShell脚本中。 运行Bash命令时,将显示Bash Shell窗口。

Update: If you have multiple Linux environments installed, you can use the wslconfig command to choose the default Linux environment used when you run the bash -c command.

更新 :如果已安装多个Linux环境,则可以使用wslconfig命令选择运行bash -c命令时使用的默认Linux环境



To create a shortcut to a Bash script from within Windows, just create a shortcut like normal. For the shortcut’s target, use the bash -c command we outlined above and point it at the Bash script you created.

要在Windows中为Bash脚本创建快捷方式,只需像普通方式那样创建快捷方式。 对于快捷方式的目标,请使用我们上面概述的bash -c命令,并将其指向您创建的Bash脚本。

For example, you’d point a shortcut at ” bash -c "~/myscript.sh" ” to run the example script above. You can also just run this command from a Command Prompt or PowerShell window, too.

例如,您可以将快捷方式指向“ bash -c "~/myscript.sh" ”,以运行上面的示例脚本。 您也可以只在命令提示符或PowerShell窗口中运行此命令。

翻译自: https://www.howtogeek.com/261591/how-to-create-and-run-bash-shell-scripts-on-windows-10/

shell bash脚本

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Shell脚本是指在Unix或类Unix操作系统中使用的脚本,用于执行一系列的命令。它可以包含各种命令、逻辑控制结构和变量定义等。Shell脚本可以用于自动化任务、系统管理、数据处理等多种用途。 Bash脚本是一种Shell脚本的变种,它是基于Bourne Shell的改进版本。Bash脚本具有更强大的功能和更丰富的语法,同时保留了兼容性。因此,大多数Linux系统使用Bash作为默认的Shell解释器。 引用提到了一些bash脚本的编写流程、代码规范和编写习惯的培养等内容,这对于那些没有太强的bash脚本编程基础的人来说是非常有帮助的。 引用和引用讲述了在bash脚本中使用双引号和单引号包围变量时的不同效果。双引号会解析变量和命令,而单引号则原样输出变量和命令。 综上所述,Shell脚本是一种用于执行一系列命令的脚本语言,而Bash脚本是一种基于Bourne Shell的改进版本,具有更强大的功能和更丰富的语法。在编写bash脚本时,可以根据需要使用双引号或单引号来处理变量和命令。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Bash Shell脚本实战11例](https://download.csdn.net/download/phoenixkiki/10585048)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [bashshell脚本](https://blog.csdn.net/cyuyangundan/article/details/124772034)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值