Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux 相关(vim、shell、shell script) #2

Open
ShannonChenCHN opened this issue Apr 21, 2017 · 9 comments
Open

Linux 相关(vim、shell、shell script) #2

ShannonChenCHN opened this issue Apr 21, 2017 · 9 comments

Comments

@ShannonChenCHN
Copy link
Owner

ShannonChenCHN commented Apr 21, 2017

相关总结

@ShannonChenCHN ShannonChenCHN changed the title How to deal with memory warning? Linux 相关(vim、shell、shell script) Oct 21, 2017
@ShannonChenCHN
Copy link
Owner Author

ShannonChenCHN commented Oct 21, 2017

sed 流式文本编辑器

推荐阅读

实践

#!/bin/bash

# 将 #import "/Users/xianglongchen/Desktop/Playground/ObjCLibA/ObjCLibA/ObjCLibA-Bridging-Header.h" 替换成 #import "ObjCLibA-Bridging-Header.h"
sed -i "" "s/^#import.*ObjCLibA-Bridging-Header\.h\"$/\#import \"ObjCLibA-Bridging-Header\.h\"/g" /Users/xianglongchen/Desktop/Playground/ObjCLibA/ObjCLibA-Swift.h
  1. sed 的正则表达式不能识别 ^#import(.+)ObjCLibA-Bridging-Header\.h\"$,但是能识别 ^#import.*ObjCLibA-Bridging-Header\.h\"$
  2. mac 执行sed -i指令时,总是出现extra characters at the end of command,原因是 Unix 和 Linux 下 sed 指令有一点区别:
    https://blog.csdn.net/lgh1117/article/details/50094595
  3. sed 教程:

@ShannonChenCHN
Copy link
Owner Author

@ShannonChenCHN
Copy link
Owner Author

grep 文本搜索命令行工具

@ShannonChenCHN
Copy link
Owner Author

ShannonChenCHN commented Oct 21, 2017

Shell 脚本

Shell 脚本学习总结

FAQ

1. 如何将一个字符串转成大写?

y="HELLO"
val=$(echo "$y" | tr '[a-z]' '[A-Z]')
# 或者 val=$(echo "$y" | tr '[:upper:]' '[:lower:]')
string="$val world"

2. 在linux下怎么把一个文件的内容复制到另一个文件的末尾?

cat file1.txt >> file2.txt

参考:

2. Mac 终端执行 sed -I 指令时,总是出现 extra characters at the end of command

在 -i 指令后面多加一个 “” 即可:

sed -i "" "s/192.168.0.2/192.168.0.3/g" *.rptdesign  

参考:

3. 如何获取当前脚本文件的路径?

# 通过 readlink 获取绝对路径,再取出目录
work_path=$(dirname $(readlink -f $0))

参考:

4.创建命令别名(alias Shell 命令)

alias Name=String

5. Shell 脚本读取参数

$@: 参数本身的列表,也不包括命令本身
$*: 和 $@ 相同,但 "$*""$@" (加引号)并不同,"$*" 将所有的参数解释成一个字符串,而 "$@" 是一个参数数组。

#!/bin/bash

for arg in "$@"
do
    echo $arg
done

@ShannonChenCHN
Copy link
Owner Author

ShannonChenCHN commented Oct 21, 2017

常用 Mac/Linux 终端命令

指定特定程序打开文件

open -a <application> <file-to-open>

@ShannonChenCHN
Copy link
Owner Author

ShannonChenCHN commented May 3, 2018

Bash Shell 常用快捷键

移动光标

  • ctrl+b: 前移一个字符(backward)
  • ctrl+f: 后移一个字符(forward)
  • alt+b: 前移一个单词(在 Mac 上是 option+⬅️,如果你用的是 iterm ,需要修改一下偏好设置)
  • alt+f: 后移一个单词(在 Mac 上是 option+ ➡️,如果你用的是 iterm ,需要修改一下偏好设置)
  • ctrl+a: 移到行首(a是首字母)
  • ctrl+e: 移到行尾(end)
  • ctrl+xx: 行首到当前光标替换

编辑命令

  • alt+.: 粘帖最后一次命令最后的参数(通常用于mkdir long-long-dir后, cd配合着alt+.
  • alt+d: 删除当前光标到临近右边单词开始(delete)
  • ctrl+w: 删除当前光标到临近左边单词结束(word)
  • ctrl+h: 删除光标前一个字符(相当于backspace)
  • ctrl+d: 删除光标后一个字符(相当于delete)
  • ctrl+u: 删除光标左边所有
  • ctrl+k: 删除光标右边所有
  • ctrl+l: 清屏
  • ctrl+shift+c: 复制(相当于鼠标左键拖拽)
  • ctrl+shift+v: 粘贴(相当于鼠标中键)

其它

  • ctrl+n: 下一条命令
  • ctrl+p: 上一条命令
  • alt+n: 下一条命令(例如输入ls, 然后按'alt+n', 就会找到历史记录下的ls命令)
  • alt+p: 上一条命令(跟alt+n相似)
  • shift+PageUp: 向上翻页
  • shift+PageDown: 向下翻页
  • ctrl+r: 进入历史查找命令记录, 输入关键字。 多次按返回下一个匹配项

zsh

  • d: 列出以前的打开的命令
  • j: jump到以前某个目录,模糊匹配

Vim

移动光标

  • b: 向前移动一个单词
  • w: 向后移动一个单词

删除

  • dw: 从当前光标开始删除到下一个单词头
  • de: 从当前光标开始删除到单词尾

参考

@ShannonChenCHN
Copy link
Owner Author

ShannonChenCHN commented Jul 5, 2018

如何在 Mac 的 Command Line Tool 上使用 Linux 命令?

问题

尽管都是源于 Unix,但是 Mac 上的终端命令跟 Linux 上的命令还有有些细微的差别。

比如 ls 命令启用彩色输出的选项就是如此:

# macOS
ls -G

# Linux
ls --color=auto

解决

使用 homebrew 在某个目录下安装 GNU utilities,主要是 coreutils。

brew install coreutils

一些问题

  1. 安装 GNU coreutilities 后,默认情况下,所有的命令在使用时,都需要加上前缀 “g”才能使用。

可以通过新建 ~/.bashrc 文件并在该文件中添加下面这一行:

export PATH="$(brew --prefix coreutils)/libexec/gnubin:/usr/local/bin:$PATH"

为了让 ~/.bashrc 文件一直生效,需要在 ~/.bash_profile 文件中加入source ~/.bashrc这一行命令。

注:当然,不新建 ~/.bashrc 文件,而是直接新建 ~/.bashrc 文件加入上面的那行代码也是可以的。

如果需要恢复 Mac 自带的 shell 命令,从 ~/.bashrc 文件中删掉上面那行代码。

参考

@ShannonChenCHN
Copy link
Owner Author

ShannonChenCHN commented Jul 5, 2018

Linux

  • Linux 系统目录结构
  • Linux 文件基本属性
  • Linux 文件与目录管理
  • vi/vim
  • 环境变量

@ShannonChenCHN
Copy link
Owner Author

ShannonChenCHN commented Dec 10, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant