Difference between revisions of "Linux Bible"
Jump to navigation
Jump to search
(→sed) |
|||
| (13 intermediate revisions by the same user not shown) | |||
| Line 9: | Line 9: | ||
r=""a b+"" | r=""a b+"" | ||
[[ ""a bbb"" =~ $r ]] true" | [[ ""a bbb"" =~ $r ]] true" | ||
| + | |||
| + | ==== Conditon ==== | ||
| + | -d file file存在并且是一个目录 | ||
| + | -e file file存在 | ||
| + | -f file file存在并且是普通文件 | ||
| + | -r file file有读权限 | ||
| + | -s file file存在且不为空 | ||
| + | -w file file写权限 | ||
| + | -x file file有执行权限 | ||
| + | -a FILE 如果 FILE 存在则为真。 | ||
| + | [ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真。 | ||
| + | [ -c FILE ] 如果 FILE 存在且是一个字特殊文件则为真。 | ||
| + | [ -d FILE ] 如果 FILE 存在且是一个目录则为真。 | ||
| + | [ -e FILE ] 如果 FILE 存在则为真。 | ||
| + | [ -f FILE ] 如果 FILE 存在且是一个普通文件则为真。 | ||
| + | [ -g FILE ] 如果 FILE 存在且已经设置了SGID则为真。 | ||
| + | [ -h FILE ] 如果 FILE 存在且是一个符号连接则为真。 | ||
| + | [ -k FILE ] 如果 FILE 存在且已经设置了粘制位则为真。 | ||
| + | [ -p FILE ] 如果 FILE 存在且是一个名字管道(F如果O)则为真。 | ||
| + | [ -r FILE ] 如果 FILE 存在且是可读的则为真。 | ||
| + | [ -s FILE ] 如果 FILE 存在且大小不为0则为真。 | ||
| + | [ -t FD ] 如果文件描述符 FD 打开且指向一个终端则为真。 | ||
| + | [ -u FILE ] 如果 FILE 存在且设置了SUID (set user ID)则为真。 | ||
| + | [ -w FILE ] 如果 FILE 如果 FILE 存在且是可写的则为真。 | ||
| + | [ -x FILE ] 如果 FILE 存在且是可执行的则为真。 | ||
| + | [ -O FILE ] 如果 FILE 存在且属有效用户ID则为真。 | ||
| + | [ -G FILE ] 如果 FILE 存在且属有效用户组则为真。 | ||
| + | [ -L FILE ] 如果 FILE 存在且是一个符号连接则为真。 | ||
| + | [ -N FILE ] 如果 FILE 存在 and has been mod如果ied since it was last read则为真。 | ||
| + | [ -S FILE ] 如果 FILE 存在且是一个套接字则为真。 | ||
==== Var OP ==== | ==== Var OP ==== | ||
| − | + | ||
| − | |||
${#var_name} $var_name 的字符串长度 | ${#var_name} $var_name 的字符串长度 | ||
| Line 30: | Line 59: | ||
${string/%substr/rplacemnt} 后缀匹配 $substring, 用 $rplacemnt 来代替匹配 | ${string/%substr/rplacemnt} 后缀匹配 $substring, 用 $rplacemnt 来代替匹配 | ||
| + | ${test##*/} 获取文件名 | ||
| + | ${test%/*} 获取目录名 | ||
| + | |||
| + | t_dev=$(cat $i |grep '^dev'|awk '{print $2}') | ||
| + | t_dev=${t_dev//[$'\t\r\n']} | ||
\[\e[F;Bm\]........\[\e[0m\] | \[\e[F;Bm\]........\[\e[0m\] | ||
| Line 94: | Line 128: | ||
sed = a.txt | sed 'N; s/^/ /; s/ *\(.\{4,\}\)\n/\1 /' | sed = a.txt | sed 'N; s/^/ /; s/ *\(.\{4,\}\)\n/\1 /' | ||
sed = a.txt | sed 'N;s/\n/\t/' | sed = a.txt | sed 'N;s/\n/\t/' | ||
| + | sed -n '1,/everyone/p' a.txt | ||
| + | sed -n '/learn/,+2p' a.txt | ||
| + | sed 's/life/leaves/' a.txt | ||
| + | sed 's/to/two/2' a.txt | ||
| + | sed 's/life/learn/g' a.txt | ||
| + | sed 's/to/TWO/2g' a.txt | ||
| + | If you wish to print only the replaced lines, then use “-n” option along with “/p” print flag to display only the replaced lines | ||
| + | sed -n 's/to/TWO/p' a.txt | ||
| + | Replace a pattern with other except in the nth line | ||
| + | sed -i '5!s/life/love/' a.txt | ||
| + | sed -n 's/\(love\)able/\1rs/p' | ||
| + | |||
| + | sed -i '/^status / s/^\(.*\)$/;\1/g' *.conf | ||
| + | cat abc.txt | sed '$G' #文件尾加空行 | ||
| + | |||
| + | echo True | sed 's/[a-z]/\u&/g' - # 转小写 | ||
| + | echo True | sed 's/[A-Z]/\l&/g' - # 转大写 | ||
| + | echo "abcdefg" | awk '{print toupper($0)}' | ||
| + | echo "ABCDEF" | awk '{print tolower($0)}' | ||
| + | UPPERCASE=(echo(echo VARIABLE | tr '[a-z]' '[A-Z]') | ||
| + | LOWERCASE=(echo(echoVARIABLE | tr '[A-Z]' '[a-z]') | ||
| + | |||
| + | t_dev=$(cat $i |grep '^dev'|awk '{print $2}') | ||
| + | t_dev=${t_dev//[$'\t\r\n']} | ||
=== '''awk''' === | === '''awk''' === | ||
| Line 119: | Line 177: | ||
else c4+=$4; } | else c4+=$4; } | ||
END {printf "c1=[%d];c2=[%d];c3=[%d];c4=[%d]\n",c1,c2,c3,c4}"' | END {printf "c1=[%d];c2=[%d];c3=[%d];c4=[%d]\n",c1,c2,c3,c4}"' | ||
| + | |||
| + | ls | xargs du -s -m | awk '$1>50000{print $1,$2}' | ||
=== '''tar''' === | === '''tar''' === | ||
| − | === ''' | + | === '''Tmux Usage''' === |
| − | + | ^B + c New Window | |
| − | + | ^B + num Switch to the nubered window | |
| − | / | + | ^B + n Switch to next window |
| − | + | ^B + d Detach the tmux to background | |
| − | / | + | ^B + % Split the window with left/right |
| − | / | + | ^B + " Split the window with up/down |
| − | + | ^B + arrow Switch to window on left/right/up/down | |
| − | + | ^B + x Kill current window | |
| − | + | ^B + z Enlarge/Recover current window | |
| + | ^B + t Show clock/time on current window | ||
| + | ^B + , Rename current window | ||
| + | ^B + [ Enter copy-mode, Arrows to move cursor | ||
| + | ^B + PagUD Roll the current screen. | ||
| − | + | vim ~/.tmux.conf | |
| − | + | setw -g mode-keys vi | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
Latest revision as of 14:07, 14 May 2023
Contents
Bash
||, &&, <, >, =, ==, =~, -n, -z, -lt, -eq, -ne
<, >, = (字符串比较,双括号不需要转移) [ "${name}" \> "a" -o "${name}" \< "m" ] [[ "${name}" > "a" && "${name}" < "m" ]]
"t=""abc123""
""$t"" == abc* true (globabing 比较)
""$t"" == ""abc*"" false (字面比较)
[[ ""$t"" =~ [abc]+[123]+ ]] true (正则表达式比较)
""$t"" =~ ""abc*"" false (字面比较)
r=""a b+""
""a bbb"" =~ $r true"
Conditon
-d file file存在并且是一个目录 -e file file存在 -f file file存在并且是普通文件 -r file file有读权限 -s file file存在且不为空 -w file file写权限 -x file file有执行权限 -a FILE 如果 FILE 存在则为真。 [ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真。 [ -c FILE ] 如果 FILE 存在且是一个字特殊文件则为真。 [ -d FILE ] 如果 FILE 存在且是一个目录则为真。 [ -e FILE ] 如果 FILE 存在则为真。 [ -f FILE ] 如果 FILE 存在且是一个普通文件则为真。 [ -g FILE ] 如果 FILE 存在且已经设置了SGID则为真。 [ -h FILE ] 如果 FILE 存在且是一个符号连接则为真。 [ -k FILE ] 如果 FILE 存在且已经设置了粘制位则为真。 [ -p FILE ] 如果 FILE 存在且是一个名字管道(F如果O)则为真。 [ -r FILE ] 如果 FILE 存在且是可读的则为真。 [ -s FILE ] 如果 FILE 存在且大小不为0则为真。 [ -t FD ] 如果文件描述符 FD 打开且指向一个终端则为真。 [ -u FILE ] 如果 FILE 存在且设置了SUID (set user ID)则为真。 [ -w FILE ] 如果 FILE 如果 FILE 存在且是可写的则为真。 [ -x FILE ] 如果 FILE 存在且是可执行的则为真。 [ -O FILE ] 如果 FILE 存在且属有效用户ID则为真。 [ -G FILE ] 如果 FILE 存在且属有效用户组则为真。 [ -L FILE ] 如果 FILE 存在且是一个符号连接则为真。 [ -N FILE ] 如果 FILE 存在 and has been mod如果ied since it was last read则为真。 [ -S FILE ] 如果 FILE 存在且是一个套接字则为真。
Var OP
${#var_name} $var_name 的字符串长度
${var:n1} 截取变量var从n1开始的字符
${var:n1:n2} 截取变量var从n1开始的n2个字符
${var#substring} 从变量$string的开头, 删除最短匹配$substring的子串
${var##substring} 从变量$string的开头, 删除最长匹配$substring的子串
${var#*string} 从左向右截取第一个string后的字符串
${var##*string} 从左向右截取最后一个string后的字符串
${var%substring} 从变量$string的结尾, 删除最短匹配$substring的子串
${var%%substring} 从变量$string的结尾, 删除最长匹配$substring的子串
${var%string*} 从右向左截取第一个string后的字符串
${var%%string*} 从右向左截取最后一个string后的字符串
${string/substr/rplacemnt} 使用 $rplacemnt, 来代替第一个匹配的 $substr
${string//substr/rplacemnt} 使用 $rplacemnt, 代替所有匹配的 $substr
${string/#substr/rplacemnt} 前缀匹配 $substring, 用 $rplacemnt 来代替匹配
${string/%substr/rplacemnt} 后缀匹配 $substring, 用 $rplacemnt 来代替匹配
${test##*/} 获取文件名
${test%/*} 获取目录名
t_dev=$(cat $i |grep '^dev'|awk '{print $2}')
t_dev=${t_dev//[$'\t\r\n']}
\[\e[F;Bm\]........\[\e[0m\] /etc/DIR_COLORS => ~/.dir_colors
F B 30 40 黑色 31 41 红色 32 42 绿色 33 43 黄色 34 44 蓝色 35 45 紫红色 36 46 青蓝色 37 47 白色
diff <(wget -o - url1) <(wget -o - url2)
Array
定义: declare -A array1 declare -a 显示所有数组
赋值: array1=(value1 value2 value3) 或 array1[0]=value1 array1[1]=value2
变量 取值
echo ${array1[0]} 数组第一个元素
echo ${array1[@]} 或 echo ${array1[*]} 数组所有元素
echo ${#array1[@]} 数组元素个数
echo ${!array1[@]} 数组所有索引下标
echo ${array1[@]:1} 数组索引1以后的元素
echo ${array1[@]:1:2} 数组索引1以后2个元素
declare -a 查看所有普通数组
遍历:
遍历元素
for t in ${allThreads[@]}; do
done
遍历索引
for i in ${!allThreads[@]}; do
done
demo:
统计系统中不同shell的使用次数
declare -A shs
while read line
do
sh_type=`echo $line | awk -F":" '{print $NF}'`
let shs[$sh_type]++
done
Loop Array
ls
sed
sed -i "/^<${OP_KEY}>/r ${OP_KFILE}" $conf_file
sed -n '/^-----BEGIN CERTIFICATE-----/,/^-----END CERTIFICATE-----$/p' server.crt
sed -i "/^172.*/s/^172.*$/$netip/" "$xfile"
sed -i "s/dpAclBlacklist/dpAclBlocklist/g" dpMgr/src/dpAclMgr.cpp
sed = a.txt | sed 'N; s/^/ /; s/ *\(.\{4,\}\)\n/\1 /'
sed = a.txt | sed 'N;s/\n/\t/'
sed -n '1,/everyone/p' a.txt
sed -n '/learn/,+2p' a.txt
sed 's/life/leaves/' a.txt
sed 's/to/two/2' a.txt
sed 's/life/learn/g' a.txt
sed 's/to/TWO/2g' a.txt
If you wish to print only the replaced lines, then use “-n” option along with “/p” print flag to display only the replaced lines
sed -n 's/to/TWO/p' a.txt
Replace a pattern with other except in the nth line
sed -i '5!s/life/love/' a.txt
sed -n 's/\(love\)able/\1rs/p'
sed -i '/^status / s/^\(.*\)$/;\1/g' *.conf cat abc.txt | sed '$G' #文件尾加空行
echo True | sed 's/[a-z]/\u&/g' - # 转小写
echo True | sed 's/[A-Z]/\l&/g' - # 转大写
echo "abcdefg" | awk '{print toupper($0)}'
echo "ABCDEF" | awk '{print tolower($0)}'
UPPERCASE=(echo(echo VARIABLE | tr '[a-z]' '[A-Z]')
LOWERCASE=(echo(echoVARIABLE | tr '[A-Z]' '[a-z]')
t_dev=$(cat $i |grep '^dev'|awk '{print $2}')
t_dev=${t_dev//[$'\t\r\n']}
awk
ARGC 命令行变元个数 ARGV 命令行变元数组 FILENAME 当前输入文件名 FNR 当前文件中的记录号 FS 输入域分隔符,默认为一个空格 RS 输入记录分隔符 NF 当前记录里域个数 NR 到目前为止记录数 OFS 输出域分隔符 ORS 输出记录分隔符
计算文件第3列的和
awk '{ x += $3 } END { print x }' myfile.txt
awk -F, 'NF>1 {OFS="%";i=1; while(i<NF) {print $1,$i; i++}}' < input.txt
awk -F, 'BEGIN {print ARGC} NF>1 {OFS="%";i=1; while(i<NF) {print $1,$i; i++}}'
awk '/101/,/105/' file
awk '$1 * $2 >100 ' file
awk '{gsub(/\$/,"");gsub(/,/,"");
if ($4>1000&&$4<2000) c1+=$4;
else if ($4>2000&&$4<3000) c2+=$4;
else if ($4>3000&&$4<4000) c3+=$4;
else c4+=$4; }
END {printf "c1=[%d];c2=[%d];c3=[%d];c4=[%d]\n",c1,c2,c3,c4}"'
ls | xargs du -s -m | awk '$1>50000{print $1,$2}'
tar
Tmux Usage
^B + c New Window ^B + num Switch to the nubered window ^B + n Switch to next window ^B + d Detach the tmux to background ^B + % Split the window with left/right ^B + " Split the window with up/down ^B + arrow Switch to window on left/right/up/down ^B + x Kill current window ^B + z Enlarge/Recover current window ^B + t Show clock/time on current window ^B + , Rename current window ^B + [ Enter copy-mode, Arrows to move cursor ^B + PagUD Roll the current screen.
vim ~/.tmux.conf setw -g mode-keys vi