技术标签: python shell ubuntu linux 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环境中的其他位置访问。
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环境随附vi和nano文本编辑器。 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关闭编辑器。
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运行它)
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中文件位置的指南 。
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脚本
问题:有一个数组,里面是一些重复的数字。问是否存在一个数,这个数出现的次数超过了数组大小的一半?如果存在的话,这个数是多少?例如对于数组[1,1,3,1,3,1,2],数字1的出现的次数超过了数组的一半;而对于数组[6,6,6,7,7,7],没有数字超过数组的一半(数字6和7都出现了3次,出现的次数等于数组的一半,而没有超过数组的一半)如果题目要求的是数字出现的次数大于等于数组的一半,...
Qt通过鼠标绘制线条、矩形、多边形本质都是根据鼠标的坐标位移,使用QPainter的自带的函数进行绘制。具体代码如下:graphicspainter.h#ifndef GRAPHICSPAINTER_H#define GRAPHICSPAINTER_H#include <QWidget>#include "shape.h"#include "paintrectline.h"class GraphicsPainter : public QWidget{ Q_OBJECT
pytorch-lightning中使用wandb实现超参数搜索
作者:郭霖自从安卓6.0之后,HttpClient的功能被完全移除了,标志着此功能被正式启用,我们使用官方推荐的HttpURLConnection获取HttpURLConnection实例new出一个URL对象,传入目标的网络地址,然后调用openConnection()方法即可设置HTTP请求所使用的方法常用的方法主要有两个:GET和POST。GET表示希望从服务器那里获取数据,POST则表示希...
在Android Studio中我们可以在底部LogCat中很直观地查看到当前连接的设备还有一种方法是直接在cmd命令行中输入adb devices命令查看如下所示,我电脑目前连接了两台设备,一台我的手机真机,一台是模拟器C:\Users\Him>adb devicesList of devices attachedBY2QLS149K055335 de...
目前微服务是一个十分火热的关键词,各大互联网公司都普遍采用了微服务技术作为大型系统架构的解决方案,那么,对于微服务,你们是否也想我一样,有以下疑问呢:微服务是什么?微服务解决了什么问题?微服务有什么特点?下面,我们带着这三个问题,慢慢来剖析微服务。一、单体架构的不足首先我们从传统工程的单体架构来说起,什么是单体架构呢?一个归档包包含了应用所有功能的应用程序,我们通常称之为单体应用...
前言随着手机阅读的普遍应用,微信公众号阅读,更为普遍,微信和qq一样,都是基于腾讯自研X5内核,不是google原生webview(其实就是进行了二次定制)。实质上也是混合应用的一种,现在很多app产品也开始流行采用X5内核作为其内嵌web浏览服务,所以掌握X5内核的混合应用自动化也是重中之重的一种技能X5内核应用自动化方式和普通混合应用有非常多的差异,接下来以微信公众号举例介绍怎么来开展X5内核的自动化准备工作 1、java-client 3.4.16依赖包 2、微信应用版
为什么80%的码农都做不了架构师?>>> ...
parseInt() 是一个内置的 JavaScript 函数,用于从数字字符串中解析整数。 例如,让我们从数字字符串 '100' 中解析整数:const number = parseInt('100'); number; // 100正如预期的那样,'100' 被解析为整数 100。 parseInt(numericalString, radix) 还接受第二个参数:数字字符串参数所在的基数。 radix 参数允许我们解析不同数字基数的整数,最常见的是 2、8、10 和 16。 让我们使用 pa
原文地址:http://swanmsg.blog.sohu.com/162994305.html之前做过ClickOnce部署应用程序的项目,今天做一次全面的总结。那么这些都是微软提供方便分布式部署的相关解决方法,这种方法既有弊端,也有优点。最大的缺点:远程部署,不能更换安装目录;并且每次安装或更新都是C盘"C:\Documents and Settings\Adminis
如果是jetson nano,你刚开始的时候可能没有root用户,因此需要设置一下root用户和密码1.设置root用户和密码首先用快捷键crtl+alt+T 打开终端,显示的是用户名@电脑名:~$,符号$就表示现在处于普通用户权限。然后输入:sudo passwd root终端会显示Enter new UNIX password: 设置root用户密码,按Enter终端显示Retype new UNIX password: 再输入一遍确定之后终端会显示passwd: pass
ERROR exception 135 Internal Server Error: /xadmin/operation/usercourse/Traceback (most recent call last): File "/home/python/.virtualenvs/dl_class/lib/python3.6/site-packages/django/core/handlers/ex...