shell bash脚本_如何在Windows 10上创建和运行Bash Shell脚本
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環境隨附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.shThis 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/bashEnter 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 $STRINGIf 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.shTo 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腳本
總結
以上是生活随笔為你收集整理的shell bash脚本_如何在Windows 10上创建和运行Bash Shell脚本的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何使用串口调试工具测试HX530串口R
- 下一篇: java信息管理系统总结_java实现科