0%

bash中一串命令执行用()和{}区别

  • ()只是对一串命令重新开一个子shell进行执行
  • {}对一串命令在当前shell执行
  • ()和{}都是把一串的命令放在括号里面,并且命令之间用;号隔开
  • ()最后一个命令可以不用分号
  • {}最后一个命令要用分号
  • {}的第一个命令和左括号之间必须要有一个空格
  • ()里的各命令不必和括号有空格
  • ()和{}中括号里面的某个命令的重定向只影响该命令,但括号外的重定向则影响到括号里的所有命令

References:
[1]shell中的(),{}几种语法用法

使用数字序列替换

vim查找替换时,可以使用一个数字序列来替换匹配的内容

1
:let i=1 \[range\]g/PATTERN/s//\\=i/g let i=i+1

插入数字序列

1
:put =range(11,15)

可以在文件当前行后插入5行:

1
2
3
4
5
11
12
13
14
15

函数式替换

在替换命令 s/// 中可以使用函数表达式来书写替换内容,格式为

1
:s/替换字符串/\\=函数式

在函数式中可以使用 submatch(1)、submatch(2) 等来引用 \1、\2 等的内容,而submatch(0)可以引用匹配的整个内容。

举个栗子,将文件从第一行开始的行首替换为如下样式:

1
2
3
4
mem\[0\]=
mem\[1\]=
mem\[2\]=
...

可以执行如下替换:

1
:%s/^/\\='mem\['.(line(".")-1).'\]='

References:
[1]Making a list of numbers
[2]vi/vim的巧妙使用-数值加减,递增,序列等

xmodmap(X modify key map)可以修改X下的键位映射
比如0现在用的键盘没有右边的CTRL,很难用,右侧的INSERT键刚好在空格右侧不远的地方,可以把它修改为右CTRL

可以使用xev程序来查看当前的keymap,可以看到右侧的INSERT键位映射为:

1
2
3
4
5
6
KeyPress event, serial 33, synthetic NO, window 0x2a00001,
root 0xdc, subw 0x0, time 102519, (936,455), root:(986,569),
state 0x10, keycode 118 (keysym 0xff63, Insert), same_screen YES,
XLookupString gives 0 bytes:
XmbLookupString gives 0 bytes:
XFilterEvent returns: False

导出原映射

1
$ xmodmap -pke > ~/.Xmodmap

修改映射

控制键要先clear,最后再add
编辑.Xmodmap文件,文件开头处添加

1
clear Control

将keycode 118修改为

1
keycode 118 = Control_R NoSymbol Control_R

然后文件尾部添加

1
add Control = Control_L Control_R

测试配置

修改好映射文件后

1
$ xmodmap ~/.Xmodmap

GDM,XDM和LightDM在开启xsession时会自动读取$HOME/.Xmodmap,但不稳定,时好时坏:(

使用startx时激活你自己的映射表,请添加下面的文件和内容:

1
2
3
4
~/.xinitrc
if \[ -f $HOME/.Xmodmap \]; then
/usr/bin/xmodmap $HOME/.Xmodmap
fi

References:
[1]Xmodmap
[2]linux下修改键位映射

$-是bash内置变量,其值为bash shell打开的选项的一个集合

1
2
3
4
5
6
7
8
9
$ echo "$-"
himBH

---
H - histexpand
m - monitor
h - hashall
B - braceexpand
i - interactive

可以使用set -/+ options来打开或者关闭这些shell选项

[1]Bash 为何要发明 shopt 命令

升级MacOS后brew upgrade经常会出现如下错误:

1
2
3
4
5
The bottle needs the Apple Command Line Tools to be installed.
You can install them, if desired, with:
xcode-select --install

xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

是因为系统升级后没有安装相应版本的Apple Command Line Tools
解决办法就是像错误提示里说的一样:

1
$ xcode-select --install

字体链接

M$的字体是专有的,有版权的,因此应该使用开源字体。

Windows支持字体链接:当一种字体中不存在某个字时,可以尝试从另一个字体文件中寻找相应的字形。所以只要把当前系统中的中文字体设为”fallback”字体,汉字通常就能正确显示了。方法也很简单,只需创建一个文本文件,如chn_font.reg如下:

1
2
3
4
5
6
7
8
9
10
11
REGEDIT4

\[HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows NT\\CurrentVersion\\FontLink\\SystemLink\]
"Lucida Sans Unicode"="wqy-microhei.ttc"
"Microsoft Sans Serif"="wqy-microhei.ttc"
"MS Sans Serif"="wqy-microhei.ttc"
"Tahoma"="wqy-microhei.ttc"
"Tahoma Bold"="wqy-microhei.ttc"
"SimSun"="wqy-microhei.ttc"
"Arial"="wqy-microhei.ttc"
"Arial Black"="wqy-microhei.ttc"

注意请将wqy-microhei.ttc替换为你系统中的字体文件名,如文泉驿正黑是wqy-zenhei.ttc(请在/usr/share/fonts及其子文件夹中寻找相应字体文件)。
MacOSX上可以使用PingFang.ttc。

如果想使用其它字体,也可直接将相应的ttf或ttc文件复制到~/.wine/drive_c/windows/Fonts/,再用其文件名替换上面的wqy-zenhei.ttc即可。

最后,打开注册表wine regedit,导入上面的注册表文件即可。中文应该都能完美显示了(包括Picasa中文显示为方框、软件安装程序汉字无法显示等问题均可被解决)。

1
$ wine regedit chn_font.reg

注意上面的注册表键值只能使用字体的文件名,而不能使用字体名,这是由“字体链接”本身的特性决定的。

References:
[1]Wine的中文显示与字体设置

模板单元文件和实例单元文件

使用模板,一个模板单元(unit)文件可以创建多个实例化的单元文件,从而简化系统配置。

模板单元文件的文件名中包含一个@符号,@位于单元基本文件名和扩展名之间,比如:

1
example@.service

当从模板单元文件创建实例单元文件时,在@符号和单元扩展名(包括符号.)之前加入实例名,比如:

1
example@instance1.service

表明实例单元文件example@instance1.service实例化自模板单元文件example@.service,其实例名为instance1

实例单元文件一般是模板单元文件的一个符号链接,符号链接命中包含实例名,systemd就会传递实例名给模板单元文件。

在相应的target中创建实例单元文件符号链接之后,需要运行一下命令将其装载:

1
$ sudo systemctl daemon-reload

模板标识符/参数

模板单元文件中可以使用一些标识符,当被实例化为实例单元文件并运行时,systemd会将标识符的实际值传递给对应的标识符,比如在模板单元文件中是用%i,实际运行实例单元文件时,会将实例名传递给%i标识符。

有以下可用的标识符:

%n: Anywhere where this appears in a template file, the full resulting unit name will be inserted.
%N: This is the same as the above, but any escaping, such as those present in file path patterns, will be reversed.
%p: This references the unit name prefix. This is the portion of the unit name that comes before the @ symbol.
%P: This is the same as above, but with any escaping reversed.
%i: This references the instance name, which is the identifier following the @ in the instance unit. This is one of the most commonly used specifiers because it will be guaranteed to be dynamic. The use of this identifier encourages the use of configuration significant identifiers. For example, the port that the service will be run at can be used as the instance identifier and the template can use this specifier to set up the port specification.
%I: This specifier is the same as the above, but with any escaping reversed.
%f: This will be replaced with the unescaped instance name or the prefix name, prepended with a /.
%c: This will indicate the control group of the unit, with the standard parent hierarchy of /sys/fs/cgroup/ssytemd/ removed.
%u: The name of the user configured to run the unit.
%U: The same as above, but as a numeric UID instead of name.
%H: The host name of the system that is running the unit.
%%: This is used to insert a literal percentage sign.

References:
[1] Understanding Systemd Units and Unit Files

1
2
3
4
5
6
7
$ git clone https://github.com/shadowsocksrr/shadowsocksr-libev
$ cd shadowsocksr-libev
$ sudo apt-get install --no-install-recommends build-essential autoconf libtool libssl-dev \\
gawk debhelper dh-systemd init-system-helpers pkg-config asciidoc xmlto apg libpcre3-dev
$ dpkg-buildpackage -b -us -uc -i
$ cd ..
$ sudo dpkg -i shadowsocks-libev*.deb