macos 运行linux,MacOS 上运行shell

### 什么是shell脚本?

##### Wikipedia:

> In [computing](https://link.jianshu.com?t=https://en.wikipedia.org/wiki/Computing), a **shell** is a [user interface](https://link.jianshu.com?t=https://en.wikipedia.org/wiki/User_interface) for access to an [operating system](https://link.jianshu.com?t=https://en.wikipedia.org/wiki/Operating_system)'s services. In general, operating system shells use either a [command-line interface](https://link.jianshu.com?t=https://en.wikipedia.org/wiki/Command-line_interface) (CLI) or [graphical user interface](https://link.jianshu.com?t=https://en.wikipedia.org/wiki/Graphical_user_interface) (GUI), depending on a computer's role and particular operation.

### 一个简单的shell脚本例子

> 创建一个helloworld.sh

```

#!/bin/bash

echo "hello world"

```

> 在终端下运行(以下几种方式都可以)

```

. helloworld.sh

```

```

source helloworld.sh

```

```

bash helloworld.sh

```

> 输出

```

hello world

```

### 语法解释

------

##### 1. 指定命令解释器

```

#!/bin/bash

```

- 这句只能放在第一行

- \#! 指示解释此脚本的shell命令解释器

- /bin/bash 指代的是bash命令解释器

- 若要双击执行,用chmod修改文件属性

```

#给当前用户的file文件添加可执行权限

chmod u+x file

```

------

##### 2. 执行脚本

```

. helloworld.sh

```

```

source helloworld.sh

```

> A dot in that context means to "source" the contents of that file into the current shell. With [source](https://link.jianshu.com?t=http://ss64.com/bash/source.html) itself being a shell builtin command. And source and the dot operator being synonyms.

------

### 附

#### bash与sh的区别

> `bash`

> `Bourne Again Shell`是linux标准的默认shell,它基于Bourne shell,吸收了C shell和Korn shell的一些特性。bash完全兼容Bourne shell, 也就是说用Bourne shell的脚本不加修改就可以在bash中执行。

> `sh`

> `Bourne shell`是UNIX标准的默认shell,对它评价是:简洁(concise)、紧凑(compact) 、快速(fast),它由AT&T编写,属于系统管理shell。

>

`bash` and `sh` are two different shells. Basically `bash` is `sh`, with more features and better syntax. Most commands work the same, but they are different.

***

# 参考资料

- ZhengHuaxing:编程基础#如何在OS X下创建并运行一个shell脚本

https://www.jianshu.com/p/60d240383180

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值