How to write a shell script [shell]

I hope not white piao, the point of a brush praise or comments to walk, as well as [Reserved] prohibit
this article is to explain how to write shell scripts perform in local and remote, as well as some of the work need to pay attention [shell ] beginning of the article (note I do more, made only slowly, if there is a particular need a certain aspect, you can reply in the comments, if I have the relevant information I will give priority to the issue)
@note: If you want to know how to program to execute the script, see my other article on how to execute a shell script

How to write a shell script
How to do the script can not be saved
Some personal experience with views

Sh script writing

First of all I want to explain this place I use the Mac system, so the windows of children's shoes to understand their own assimilation. (Click here to Amway mac do not need some other third-party software to connect to the specified linux, do not need to set up a virtual machine locally, of course, powershell is a good thing, here is what Amway Mac programming more convenient)
Here is the first mac equipped with vim editor, in addition to the work still need some other editors such as Sublime and basic Atom more mainstream is that these two individuals in the company's Sublime3 really easy to use, and then look at Amway.
We directly to the question, how to write a shell script? Nothing less than the two ways:

  1. Sublime3 our normal open a window like this (I just write the content of Kazakhstan)
    Here Insert Picture Description
    we finished sh code that we write in this interface, we can save the normal way of culture and education will save this file as xxxx. sh (it must be a .sh end, do not ask why, why the end of the doc is word, regulations)
  2. 第二种就是我们在工作的时候是在远程的服务器上的,我们是没有办法在本地编写脚本的,这时候我们就要用到vim这个编辑期(这里要说一下Mac的Unix系统跟我们部署的远程Linux机器的命令还是有区别的,只不过前期的时候不用care,命令基本一致
    首先我们打开我们的Iterm2,或者Command+空格 输入terminal 这两种方法都能打开命令行界面,这里我用Iterm2作为例子,如下所示
    Here Insert Picture Description
    不要在乎我们前面为什么不一样,为什么你的没有蓝色的这个箭头,因为我装了omyzsh,后期我会有教程,等不了的也可以自己搜索教程
    然后我们要输入
    vim 脚本的名字.sh
    这里要记住所有的东西尽量不要出现中文!
    这里要记住所有的东西尽量不要出现中文!
    这里要记住所有的东西尽量不要出现中文!

    以上两步结束我们进入到了新的界面,此时我们要摁一下英文的i进行编辑模式,左下角出现
    Here Insert Picture Description
    这样我们就能在这里面编写代码了,编写好了之后我们要摁一下esc键,然后输入:wq进行保存。

脚本无法保存

如果出现了没有办法保存的情况,一般都是没有写的权限。这个时候后你要用control + z退出去(这种退出是不会保存代码的,所以最好先把代码复制一下)。我们要输入ls -l查看文件的权限,下面我给个图看一下具体的例子
Here Insert Picture Description
这个地方就是针对不同用户的读写权限(理解不了不要紧),以为如果我前面的你都看不懂,一般你就是测试或者自己搭着玩,你就直接chmod 777 脚本的名字.sh这样就行了。然后再进行上面的正常操作就好。

个人坑点

@warning: at work must not casually chmod 777especially small companies, operation and maintenance team is not perfect, the system is not perfect, do not just give permission. Because if your code problems, the consequences are serious, then there are some companies run batch tasks once you delete this script is the script, pulled up a task which finished, I think working for some time, people will understand this problem, which is just one of them. The second is likely to lead to uneven distribution of the underlying rights, rights management is not convenient, it is taboo for a big company, there should not allow a novice to operate when a lot of things. But as you have to understand the seriousness of this white thing, not a programmer does not allow you to make mistakes, but some mistakes can not be forgiven, no exaggeration to say I was too delete all backup data, people are growing up in the wrong, but you have to learn.

Published 21 original articles · won praise 20 · views 1877

Guess you like

Origin blog.csdn.net/weixin_43071838/article/details/104492265