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
    vim 脚本的名字.sh

    Here Insert Picture Description


如果出现了没有办法保存的情况,一般都是没有写的权限。这个时候后你要用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