shell小技巧

1.seq嵌入for循环

for i in $(seq 0 10)
do
xxxxxx
done

2.expr不支持非整型运算

###############
c=echo “13.2-10.1” | bc
echo $c
###############
a=13.2
b=10.1
c=$(echo “$a-$b” | bc)
echo $c

3. bc计算精度

echo “6/9” | bc 的计算结果会是0,可以事先设置精度
echo “scale=3;6/9” | bc就会得到指定精度的结果

4. gawk中调用shell变量

awk -v awk变量名= shell变量名

 #!/bin/bash

var4bash=test

awk -v var4awk=”$var4bash”  ‘BEGIN { print  var4awk}’ 

家乡风物2

macOS无法打开X11图形?

在macOS系统中用vpn登陆linux服务器后出现这样的提示:

Warning: untrusted X11 forwarding setup failed: xauth key data not generated

且无法打开程序的图形界面,后来搜到了这样的解决方案:

https://stackoverflow.com/questions/27384725/ssh-x-warning-untrusted-x11-forwarding-setup-failed-xauth-key-data-not-gener

具体操作就是:

1. Edit ~/.ssh/config, add

 XAuthLocation = /opt/X11/bin/xauth  to the host config

2. Ensure xauth is installed on the destination host

3. ssh -X your_server works in a secure manner

bbe-更改二进制数据流

某个程序输出的文本结果中出现了特殊字符^ @,用grep匹配字符串时说是二进制文本,只好想办法去掉。

方法一:在vim下打开文件,可以看出本身就是个正常的文本文件,仅只是多了那几个特殊字符,输入:%s/\x00//g回车后即可删除。本来想照猫画虎用sed来处理,毕竟几百个文件都用vim操作是不现实的,结果发现用sed删除字符不成功,https://unix.stackexchange.com/questions/346291/editing-binary-streams-containing-x00-bytes

这里表示sed只能用来处理text文件。这个网站提供了另一种处理工具:

方法二:bbe,是一种类似于sed的流编辑工具,能处理二进制字符,可以这么操作bb -e ‘s/\x00//g’ output

另,在gawk中输出单引号,可以用\47