如何创建和保存 bash 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10922222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:12:07  来源:igfitidea点击:

How to create and save a bash script

bashunix

提问by MarioDS

The teacher never explained us anything about unix and bash. He told us something about the history. Then he said how to fill in an IP address in a program called putty, which is the shell we used to work on the server. He basically gave us 7 series of exercises. We also had a book that was pretty useless. I mostly used internet and SO to help myself out, along with the commands I knew from DOS and the man/info pages.

老师从来没有向我们解释任何有关 unix 和 bash 的信息。他告诉我们一些关于历史的事情。然后他说如何在一个叫putty的程序里填一个IP地址,这个程序就是我们以前在服务器上工作的shell。他基本上给了我们7个系列的练习。我们还有一本很没用的书。我主要使用互联网和 SO 来帮助自己,以及我从 DOS 和 man/info 页面知道的命令。

The question is, what is the preferredtext editor to make scripts? Internet says any will do but I know only cat. And then, how do I even begin? What I need is a crash course or something. I'm sorry I know this isn't the best kind of question to ask here but it's all I can think of atm. Don't blame me, but please do bash the teacher (pun intended).

问题是,制作脚本的首选文本编辑器是什么?互联网说任何人都可以,但我只知道cat。然后,我该如何开始?我需要的是速成班什么的。抱歉,我知道这不是在这里问的最佳问题,但我只能想到 atm。不要怪我,但请不要抨击老师(双关语)。

回答by Kyle

Try using vias your text editor. Change to the directory you want your script to be saved in and then type in `vi myScript.sh". This will start the vi text editor. You can type in all your shell commands here.

尝试vi用作您的文本编辑器。切换到您希望保存脚本的目录,然后输入“vi myScript.sh”。这将启动 vi 文本编辑器。您可以在此处输入所有 shell 命令。

Here's a link to a vi cheat sheet - http://linuxfordummies.org/tag/vi/

这是 vi 备忘单的链接 - http://linuxfordummies.org/tag/vi/

Once your in vi, you can type in one command on each line, you can use enter to move to the next line. After you have your script typed in, press : on the keyboard to open a prompt for vi. Once you have the : (colon) prompt at the bottom of the screen, enter wqfor write/quit. This will save myScript.sh to the directory you started in. You should then be able to execute the script just by calling myScript.sh

进入 vi 后,您可以在每一行输入一个命令,您可以使用 Enter 移动到下一行。输入脚本后,按键盘上的 : 打开 vi 提示。在屏幕底部出现 :(冒号)提示后,输入wqwrite/quit。这会将 myScript.sh 保存到您开始的目录中。然后您应该能够通过调用来执行脚本myScript.sh

I hope this helps, best of luck!

我希望这会有所帮助,祝你好运!

回答by Sean Mee

You could use the vi text editor, but it can be a bit tricky if you're starting out. I would try gedit if your using a GNOME environment

您可以使用 vi 文本编辑器,但如果您刚开始使用它可能会有点棘手。如果您使用 GNOME 环境,我会尝试 gedit