Mac中的Linux命令


=Start=

缘由

虽然Mac系统和Linux系统都是UNIX-like系统,很多地方都很像,解决方案也比较通用——大同小异,但有时候就是那么一点点的不同,导致本来在Linux上可以work的程序放到Mac上就无法work了,最近碰到了不少这样的案例,想着每次遇到的时候都得重新Google还是不太方便,于是就想找个地方记录下来,方便自己和后来者。

内容
1.用sed进行就地替换(sed的’-i’选项)
$ chapter1/1_1_local_machine_info.py
env: python\r: No such file or directory
#上面的问题很明显——行尾的换行符是Windows格式的'\r\n',而不是Linux或Mac的'\n',如果想要正常运行,需要将文件中的'\r'删除

#错误一
$ sed -i 's/\r$//' chapter1/1_1_local_machine_info.py
sed: 1: "chapter1/1_1_local_mach ...": command c expects \ followed by text
$ sed -i 's/\r//' chapter1/1_1_local_machine_info.py
sed: 1: "chapter1/1_1_local_mach ...": command c expects \ followed by text
#解释 http://stackoverflow.com/questions/4247068/sed-command-failing-on-mac-but-works-on-linux

#错误二
$ sed -i '' 's/\r//' chapter1/1_1_local_machine_info.py
$ chapter1/1_1_local_machine_info.py
zsh: chapter1/1_1_local_machine_info.py: bad interpreter: /us/bin/env: no such file or directory
#解释 http://stackoverflow.com/questions/21621722/removing-carriage-return-on-mac-os-x-using-sed

#正确方法
$ sed -i '' $'s/\r//' chapter1/1_1_local_machine_info.py
#批量替换
$ find /path/to -type f -iname "*.py" -print0 | xargs -0 -I {} sed -i '.bak' $'s/\r//' {}
2.在Mac上启动MySQL/Apache等服务
#Mac上启动MySQL服务
mysql.server status
mysql.server start

#Mac上启动Apache服务
apachectl -v
apachectl status
apachectl start
3.查看 Mac 系统的端口开放情况
#查看 Mac 系统的端口开放情况
netstat -nat
netstat -nat | grep 3306
netstat -nat | grep LISTEN
lsof -n -P -i TCP -s TCP:LISTEN
参考链接:

 

备注1(Mac OS和Linux的不同之处):

Mac OS is based on a BSD code base, while Linux is an independent development of a unix-like system. This means that these systems are similar, but not binary compatible.

=EOF=

, , ,

《“Mac中的Linux命令”》 有 2 条评论

  1. How can I get the tac command on OS X?
    https://unix.stackexchange.com/questions/114041/how-can-i-get-the-tac-command-on-os-x
    https://blog.csdn.net/dongwuming/article/details/50836723
    https://stackoverflow.com/questions/7533462/can-you-use-the-tac-command-in-terminal
    `
    在 Mac 上实现 Linux 下的 tac 命令的功能:
    # 方法一:使用 coreutils 中的 gtac
    # brew install coreutils
    $ echo ‘2018年11月20日
    2018年11月21日
    2018年11月27日
    ‘ | gtac

    # 方法二:使用 tail -r 替代
    $ echo ‘2018年11月20日
    2018年11月21日
    2018年11月27日
    ‘ | tail -r
    `

回复 hi 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注