那些年擦肩而过的 Linux 命令

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

三个知识点:

  1. basename 路径或者文件路径:取出路径或者文件路径中除最后一个路径之前的相对路径。
  2. dirname 路径或者文件目录:取出路径或者文件路径中最后一个路径。
  3. $0 是 shell 中的预定义的变量,意思是文件的相对路径。

例如,#1 中式当前目录是 .,所以 basename $0 结果是s.shdirname $0的结果是 .
#2 中的往前推了一级目录,所以 basename $0结果是s.sh

取 k-v 值

$>cat k-v.txt
1
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值