美文网首页00-OCshell软件开发者
#编程基础#如何在OS X下创建并运行一个shell脚本

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

作者: KomalZheng | 来源:发表于2016-06-09 23:53 被阅读6213次

什么是OSX?

OSX是苹果为Mac开发的专属操作系统,基于Unix操作系统

什么是shell脚本?

Wikipedia:

In computing, a shell is a user interface for access to an operating system's services. In general, operating system shells use either a command-line interface (CLI) or 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 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.

相关文章

网友评论

  • 我明白了我是一条鲶鱼:为什么我只能执行/bin下面的命令,把命令移动到其他地方就执行不了了?
  • LeonardLT:为什么不是 ./helloworld.sh
    而是 . helloworld.sh
    有些博客的写法是./helloworld.sh呀?
    林枫1:没有修改权限的情况下是不能够用./helloworld.sh来运行脚本的,只有用chmod u+x helloworld.sh给helloworld.sh修改了执行权限才可用./helloworld.sh运行这个脚本,而. helloworld.sh是一个dot命令,本质不一样
    KomalZheng:@LeonardLT ./helloworld.sh 是直接打开当前文件夹下的该文件, ./是当前文件夹
    而. helloworld.sh 中的那个dot是一个命令

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

本文链接:https://www.haomeiwen.com/subject/iyhsdttx.html