date 日期和 long 的转化
date +%s000 --date="2024-12-20 00:00:00"
根据 ID 查看程序的安装目录
# 以 nginx 为例
ll /proc/{
pid}/exe
# exe 其实是一个连接,这个连接指向的就是 nginx 的绝对路径
如何再 AWK 中打印单引号
使用 \47
,例如:
seq 0 10 | awk '{print "partition by (pfield = \47"$1"\47"}'
批量执行命令
while read line ;
do
IP=$(取到 ip)
## 简单命令直接放到这里
ssh -n ${
IP} ${
cmd}
## 复杂的命令放到文件中
ssh -n ${
IP} < ${
cmd_file}
done < $file_path
-n 的作用是 ssh 不从标准输入读数据,而是从 /dev/null 中读数据。
while do done < $file 这种方式,会将所有的内容给 while 语句,如果 ssh 不加 -n , read 只能读到第一行的内容,剩余的内容会被 shh 读到,导致只能读一行的问题。
并行执行命令
cmd_file=$(mktemp -q /path/cmd_file.XXXXX)
if [ $? -ne 0 ]; then
echo "$0: Can not create"
exit 1
fi
while ..
do
echo "cmd string" >> ${cmd_file}
done
eval $(cat ${
cmd_file})
1和2 append 到文件中
commad >> log.file 2>$1
单行处理管道中的数据
conmmad | \
while read line;
do
echo $line
done
read args1 args2 args3 ;
echo $args1
编辑文末 n 行
sed -e ":a " -e "$action ; N ; 2,n行数字ba" -e "P;D" file_path
找出不包含某个字符的文件
grep -L '字符串' ./文件名字
分割文件
# 每 1G 就分割一个文件
split -c 1G file pre_fix
cp 的升级版
rsync -av --exclude 排除某些文件
命令超时
timeout -- run a command with a time limit
SYNOPSIS
timeout [OPTION] DURATION COMMAND [ARG]..
DESCRIPTION
Start COMMAND, and kill it if still running after DURATION.
Mandatory arguments to long options are mandatory for short options too.
-k, --kill-after=DURATION
also send a KILL signal if COMMAND is still running
this long after the initial signal was sent
-s, --signal=SIGNAL
specify the signal to be sent on timeout;
SIGNAL may be a name like 'HUP' or a number; see 'kill -l' for a list of signals
拦截 hive 命令每次执行的命令
ps -ef | grep 'hive.beeline' | grep '\-f' | awk '{print $2}'
export data dictionary from hive
#!/bin/bash -x
function desc_dict(){
db=$1
tables=$(hive -e --showHeader=true " use $db ; show tables; " | sed "s/|//g ; s/[-+]//g ; s/tab_name//g" | xargs )
for table in $tables
do
echo "$db.${table//|/}" >> out.dict
hive -e --showHeader=true -e " use $db ; DESCRIBE ${table//|/} " >> out.dict
done
}
结合 python 在把 txt 文档转为 insert 语句或者其他的格式。https://gitee.com/bluedream_pp/pythonFirstInHead/blob/master/gen_data_dictionary_insert_sql.py
计算任务耗时
grep 'duration' ${filepath} | grep -v 'echo' | sed 's/-//g' | \
awk '{ {print $1 , $7}}' | sort -k2 -r -n | \
awk 'BEGIN{printf "%-28s,%5s\n","任务","耗时" } { printf "%-30s,%5s 秒\n",$1,$2}' >> ${filepath}
sed 批量执行命令
sed "s/\(.*\)/grep -o '\1' log.log /e" k.txt
文件路径
$> cat s.sh
#!/bin/bash -x
declare -r curr_dir=$(cd `dirname $0` ; pwd)
$> sh s.sh # 1
$> cd ..
$>sh folder/s.sh # 2
三个知识点:
basename 路径或者文件路径
:取出路径或者文件路径中除最后一个路径之前的相对路径。dirname 路径或者文件目录
:取出路径或者文件路径中最后一个路径。$0
是 shell 中的预定义的变量,意思是文件的相对路径。
例如,#1 中式当前目录是 .
,所以 basename $0
结果是s.sh
。dirname $0
的结果是 .
#2 中的往前推了一级目录,所以 basename $0
结果是s.sh
取 k-v 值
$>cat k-v.txt
1