最近做的事情都比较零散,不太系统,如果不及时记录下来的话,再隔一段时间去看的话就容易忘记之前处理/解决问题时用到的知识、技巧,又得花时间,所以下面就把近一段时间碰到的问题、想到的方法记录一些,方便后期随时查看。
1.Vim跳转
需实现的功能:“vim 跳至上一位置”
'' #跳转到光标上次停靠的地方,是两个单引号,而不是一个双引号
参考链接:
- vim操作大全 | 钿畑的博客 http://54im.com/linux/vim%E6%93%8D%E4%BD%9C%E5%A4%A7%E5%85%A8.html
- VIM 跳转 | Andy’s Home http://www.wm177.net/?p=175
- linux下vim命令详解 – 心静茹水 http://seagg.github.io/blog/2013/04/26/vim/
- VIM: quickref http://vimcdoc.sourceforge.net/doc/quickref.html
==
2.C语言基础学习
代码片段:
#include <stdio.h> #include <stdlib.h> /* * return 0: s is not an empty string * return 1: s is an empty string * http://stackoverflow.com/questions/3981510/getline-check-if-line-is-whitespace */ int str_is_empty(const char *s) { while (*s != '\0') { if (!isspace(*s)) return 0; s++; } return 1; } void main() { char * s; const char * a_space = " "; const char * a_newline = "\n"; const char * a_test = " \n\t "; const char * a_str = " a_str "; printf("' ' is %s\n", s = str_is_empty(a_space) ? "empty" : "not_empty"); printf("'\\n' is %s\n", s = str_is_empty(a_space) ? "empty" : "not_empty"); printf("' \\n\\t ' is %s\n", s = str_is_empty(a_test) ? "empty" : "not_empty"); printf("' a_str ' is %s\n", s = str_is_empty(a_str) ? "empty" : "not_empty"); }
参考教程:
- 高质量C++/C编程指南 http://oss.org.cn/man/develop/c&c++/c/c.htm
- Linux C编程一站式学习 http://akaedu.github.io/book/
- qyuhen/book · GitHub https://github.com/qyuhen/book
- Standard C语言标准函数库速查 (Cheat Sheet) http://ganquan.info/standard-c/
- Linux工具快速教程 — http://linuxtools-rst.readthedocs.org/zh_CN/latest/
- https://github.com/justjavac/free-programming-books-zh_CN
==
3.Linux下进行字符串的查找和替换、xargs的-i选项
搜索关键字:
- Linux find and replace
- find xargs -i
参考内容:
$ find /tmp/ -ctime -1 -name "x*" | xargs -I '{}' mv '{}' ~/play/ $ file * | awk -F: '/image/ { print $1 }' | xargs -I '{}' mv '{}' destination $ find /tmp/ -ctime -1 -name 'x*' -print0 | xargs -r0 mv -t ~/play/ # GNU's mv command $ file * | awk -F: '/image/ { print $1 }' | xargs mv -t destination $ find . -name "*.bak" -print0 | xargs -0 -I {} mv {} ~/old.files $ find . -name "*.bak" -print0 | xargs -0 -I file mv file ~/old.files
-I replace-str --replace[=replace-str] -i[replace-str] Replace occurrences of replace-str in the initial arguments with names read from standard input. Also, unquoted blanks do not terminate arguments; instead, the input is split at newlines only. If replace-str is omitted (omitting it is allowed only for ‘-i’), it defaults to ‘{}’ (like for ‘find -exec’). Implies ‘-x’ and ‘-l 1’. The ‘-i’ option is deprecated in favour of the ‘-I’ option.
参考链接:
- http://stackoverflow.com/questions/8588789/linux-find-and-replace
- http://unix.stackexchange.com/questions/90886/how-can-i-find-files-and-then-use-xargs-to-move-them
- http://www.cyberciti.biz/faq/linux-unix-bsd-xargs-construct-argument-lists-utility/
- http://man.linuxde.net/xargs
- http://www.gnu.org/software/findutils/manual/html_node/find_html/xargs-options.html#xargs-options
==
4.Linux下的Immutable files
搜索关键字:
- Linux Immutable files
- Linux lsattr
参考链接:
- Linux 文件系统安全攻略 http://www.ibm.com/developerworks/cn/linux/l-cn-filesystem-sec/
- (总结)Linux的chattr与lsattr命令详解 http://www.ha97.com/5172.html
- http://unix.stackexchange.com/questions/32256/whats-the-meaning-of-output-of-lsattr
- http://www.cyberciti.biz/tips/linux-password-trick.html
- http://unix.stackexchange.com/questions/132795/how-to-search-for-files-with-immutable-attribute-set
- http://www.tecmint.com/chattr-command-examples/
==
5.Awk的函数使用/学习
搜索关键字:
- awk substr
- awk match then substr
- awk str function remove comma
- awk function
参考写法:
$ cat file.log | awk -F'|' '/bin/ {split($2, a, " "); print $1,a[1],substr(a[5], 0, length(a[5])-1),a[10]}' > test.log $ cat file.log | awk -F'|' '/bin/ {split($2, a, " "); print $1,a[1],substr(a[5], 0, length(a[5])-1),a[10]"-"substr(a[11], 0, length(a[11])-2)}' > test.log $ awk '{dic[$3]++}; END{for(x in dic) print dic[x]"\t"x}' test.log
awk的内建函数split允许你把一个字符串分隔为单词并存储在数组中。你可以自己定义域分隔符或者使用现在FS(域分隔符)的值
格式:
split (string, array, field separator)
split (string, array) –>如果第三个参数没有提供,awk就默认使用当前FS值
内建函数substr截取字符串{返回从起始位置起,指定长度的子字符串;若未指定长度,则返回从起始位置到字符串末尾的子字符串}
格式:
substr(s, p) 返回字符串s中从p开始的后缀部分
substr(s, p, n) 返回字符串s中从p开始长度为n的后缀部分
函数length返回整个记录中的字符数
$ echo "123" | awk '{print length}' 3 $ echo "123456" | awk '{print length}' 6
去掉文件中的utf-8 BOM头
awk '{if(NR==1)sub(/^xefxbbxbf/,"") print}' input.txt > output.txt
用sed/awk去掉行尾的逗号
sed 's/,$//' file.txt > file.nocomma awk '{print substr($0, 0, length($0)-1)}' file.txt awk '{gsub(/,$/,""); print}' file.txt
参考链接:
- awk内置函数的使用 – http://gdcsy.blog.163.com/blog/static/12734360920130241521280/
- awk内置函数的使用(split/substr/length) – http://www.cnblogs.com/sunada2005/p/3493941.html
- awk之substr()与length()的配合运用- http://blog.chinaunix.net/uid-10540984-id-325914.html
- http://cs.canisius.edu/ONLINESTUFF/PL_TUTORIALS/AWK/awk.examples
- The AWK Manual – http://www.staff.science.uu.nl/~oostr102/docs/nawk/nawk_92.html
- Awk – A Tutorial http://www.grymoire.com/Unix/Awk.html#uh-40
- http://www.math.utah.edu/docs/info/gawk_13.html
- https://www.gnu.org/software/gawk/manual/html_node/index.html
- =
- http://stackoverflow.com/questions/14840953/how-to-remove-a-character-at-the-end-of-each-line-in-unix
- http://stackoverflow.com/questions/12204192/awk-multiple-delimiter
- http://stackoverflow.com/questions/8009664/split-string-to-array-using-awk
==
6.如何用sed一次进行多个替换操作?
- http://www.cyberciti.biz/faq/howto-sed-substitute-find-replace-multiple-patterns/
- http://stackoverflow.com/questions/10565877/how-to-replace-two-things-at-once-with-sed
sed -e 's/dog/monkey/g' -e 's/orange/cow/g' sed -e 's/dog/monkey/g; s/orange/cow/g'
==
7.如何用sed对一个多行字符串进行替换操作?
http://unix.stackexchange.com/questions/26284/how-can-i-use-sed-to-replace-a-multi-line-string
==
8.如何用sed对换行符’\n’进行替换?
http://stackoverflow.com/questions/1251999/how-can-i-replace-a-newline-n-using-sed
Use this solution with sed:
sed ':a;N;$!ba;s/\n/ /g'
This will read the whole file in a loop, then replaces the newline(s) with a space.Explanation:
- create a label via :a
- append the current and next line to the pattern space via N
- if we are before the last line, branch to the created label $!ba ($! means not to do it on the last line (as there should be one final newline)).
- finally the substitution replaces every newline with a space on the pattern space (which is the whole file).
==
9.Vim中’^M’字符的替换操作
http://stackoverflow.com/questions/64749/m-character-at-end-of-lines
方法一:
:set fileformat=unix :w
方法二(常用):
:%s/^M//g
To get the ^M hold the CTRL key, press V then M (Both while holding the control key) and the ^M will appear. This will find all occurrences and replace them with nothing.(如何得到上面的’^M’字符:在按住Ctrl键的同时按下V和M键即可)
http://stackoverflow.com/questions/71323/how-to-replace-a-character-for-a-newline-in-vim
==
10.Shell编程中的数组
搜索关键字:
bash array
参考内容:
#!/bin/bash users="one two three" arr_s=("/etc/passwd" "/etc/group" "/etc/hosts") limits=( 10,20,26,39,48 ) for ix in ${users[@]}; do echo $ix done for iy in ${arr_s[@]}; do echo $iy done for iz in ${limits[@]}; do echo $iz done
bash中的数组/循环
如果希望在上一个循环中得到的结果可以提供给后面的功能用,有两种方法:
1.数组
用declare -A声明
用圆括号括起来
2.以空格连接的字符串{本质上也是一种数组}
users="one two three" arr_s=("/etc/passwd" "/etc/group" "/etc/hosts") echo "Total counts of array : ${#users[*]}" echo "Total counts of array : ${#arr_s[*]}"
参考链接:
- http://www.cyberciti.biz/faq/bash-for-loop-array/
- http://www.cyberciti.biz/faq/bash-iterate-array/
- http://wiki.bash-hackers.org/syntax/arrays
- http://www.tecmint.com/working-with-arrays-in-linux-shell-scripting/
- http://mywiki.wooledge.org/BashGuide/Arrays
- http://www.thegeekstuff.com/2010/06/bash-array-tutorial/
- http://tldp.org/LDP/abs/html/arrays.html
- http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_10_02.html
- http://stackoverflow.com/questions/8880603/loop-through-array-of-strings-in-bash-script
==
11.如何在Linux下创建一个“不可变更的”文件
可以使用chattr和lsattr命令来管理额外的文件标志位,来避免文件被篡改(意外或者其他情况)。但要注意你不能将chattr作为一个安全措施,因为“不可变更”标志位可以很容易被取消掉。解决这个问题的一个可能的方式是限制chattr命令自身的可用性,或者去掉CAP_LINUX_IMMUTABLE内核功能。关于chattr以及可用的标志位的更多细节,请参考它的man手册。
搜索关键字:
Linux how to limit a command’s execute user
参考链接:
- 如何在Linux下创建一个不可变更的文件
- http://serverfault.com/questions/569491/limit-exec-commands-for-linux-users
- http://serverfault.com/questions/223324/how-do-i-limit-the-users-a-specific-user-can-run-commands-in-linux
- http://unix.stackexchange.com/questions/90998/block-particular-command-in-linux-for-specific-user
- http://superuser.com/questions/767415/limit-user-to-execute-selective-commands-linux
- http://stackoverflow.com/questions/21498667/how-to-limit-user-commands-in-linux
- http://www.cyberciti.biz/faq/protect-command-by-configuring-linux-unix-group-permissions/
《 “Linux下的小知识点总结_0” 》 有 4 条评论
在Linux下让某个文件只能往里面追加数据,但不能删除,适用于各种日志文件
`
# chattr +a /var/log/messages
`
https://en.wikipedia.org/wiki/Chattr
http://unix.stackexchange.com/questions/59864/restrict-file-access-to-append-only
https://linux.cn/article-5590-1.html
http://man.linuxde.net/chattr
你应该要知道的 Linux 知识 – Julia Evans
http://www.zcfy.cc/article/1990
https://jvns.ca/blog/2016/11/21/things-to-learn-about-linux/ #它提醒我还有许多有趣的东西需要去学习
来自Unix/Linux的编程启示录
http://blog.csdn.net/dd864140130/article/details/54922319
`
原则一:小即是美
原则二:让每个程序只做好一件事情
原则三:建立原型
原则四:舍弃高效率而取可移植性
原则五:使用纯文本来存储数据
原则六:利用软件的杠杆效应
原则七:使用shell脚本来提高杠杆效应和可移植性
原则八:避免强制性的用户界面
原则九:让每个程序都成为过滤器
原则十:并行思考
原则十一:各部分之和大于整体
原则十二:寻找90%的解决方案
原则十三:层次化思考
`
Linux C编程一站式学习
https://www.kancloud.cn/wizardforcel/linux-c-book/134922
http://akaedu.github.io/book/
Standard C 语言标准函数库速查 (Cheat Sheet)
http://ganquan.info/standard-c/
Linux C 函数 使用手册
http://www.iteedu.com/os/linux/linuxprgm/linuxcfunctions/
Section 3: library functions – Linux man pages
https://linux.die.net/man/3/
C语言标准库
http://c.biancheng.net/cpp/u/biaozhunku/
C语言常见问题集锦【你必须知道的495个C语言问题】
http://c.biancheng.net/cpp/u/wenti/
http://www.cprogramming.com/tutorial/c-tutorial.html
50个C语言经典小程序和C语言编写的小游戏,带注释和解析
http://c.biancheng.net/cpp/u/yuanma/