bash代码片段snippets

bash代码片段


批量改文件extention

# Rename all *.txt to *.text
for file in *.txt; do
    mv -- "$file" "${file%.txt}.text"
done

read读取多列数据

while read x z y; do
  of="xag.1/$x.png" 
  if="xag/$y.png"
  cp $if $of	  
done < db.log 

去除tree输出的特殊字符

tree -L 2 | sed 's/\xc2\xa0/ /g' > ~/Downloads/1.txt
du -sh -t 100M *

移除重复行

#整行比较
awk '!seen[$0]++' file
#第一个field比较
awk '!seen[$1]++' file

按指定field排序

#按第二个field排序
sort -k2,2 file

按起始10个字符去重复

uniq -w 10 file 

od输出二进制文件数据

od -x -v -A n --width=100 ./x.bin >od.txt

文件分割

cat ../xtotal.pad | split -a 4 -d -l 52

一份文件复制两次

n=0;
for x in x*; do 
  y=`printf "y%04d" $n` && ((n=n+1)) && cp $x $y
  y=`printf "y%04d" $n` && ((n=n+1)) && cp $x $y
done


计算文本文件每行的md5

cat od.txt | perl -MDigest::MD5=md5_hex -nlE'say md5_hex($_)' >db_x.txt

生成所有文件md5sum

md5sum x*

ffmpeg批量提取多个视频中图像

 n=1; for x in round.0/*;do echo $x;  fmt=frame${n}.%04d.png; ffmpeg -i $x -vf "fps=2" $fmt; ((n=n+1)); done

printout.sh

#!/bin/bash

hexpattern="0-9a-f"
pattern='$LZSKGHVRMTBCDEF'
input=$1

cat $input | sed 's/\s//g' | tr $hexpattern $pattern  | awk  'BEGIN{print} {print " ", $0}'

padding.pl

#!/usr/bin/perl

$pad='9999';
$nword_per_line=50;
$nline_per_page=52;

$num_of_lines=0;
while(<>)
{
    @array=split;

    $nword=@array;

    while($nword < $nword_per_line)
    {
        push(@array,$pad);
        $nword+=1;
    }
    $str=join(" ",@array);
    print $str,"\n";

    $num_of_lines+=1;
}

while($num_of_lines%$nline_per_page)
{
    print $str, "\n";
    $num_of_lines+=1;
}

dispatch.sh

#!/bin/bash

#set -x

for((n=0;n<200;n+=20)); do
        ((s=n))
        ((e=n+19))
        d=`printf '%03d-%03d\n' $s $e`
        [ -e $d ] && rm -rf $d && echo "delete $d"
        mkdir -p $d
        for ((i=s;i<=e;i++));do
                x=`printf 'x%03d\n' $i`
                [ -e $x ] || exit
                mv $x $d/.
        done
done

find

find path prune

find . -path './backup/*' -prune -o -path './.git/*' -prune -o type f -o -iname '*.f90' | xargs wc -l | sort -n 

数字列表

for f in x00{01..11};do 
  echo $f 
done 

for f in x00{1..9}{a..b};do 
  echo $f
done 


glob通配符匹配

命令行通配符
单个字符匹配(glob-expansion)

#!/bin/bash

echo "Matching Directories:"

for dir in [p-xP-X]*; do
    if [ -d "$dir" ]; then
        ls -d "$dir"
    fi
done

#合并所有分区的cells文件(剔除注释行和数量行)
for f in part.*/cells; do 
  echo $f
  sed '/^\*\*.*/d' $f | tail -n +2 >>cells
done 


数学计算

 awk "BEGIN{print 0.015/0.022}"

sort

Aligning columns using awk

#第三列排序(数字逆序)
sort -nr -k3,3 timers.txt >timers.sort.txt
#列格式化
column -t timers.sort.txt

watch

watch tail -n 15 mylogfile.txt

tee

#stdout&stderr重定向到tee 
xxxx |& tee run.loog 


cygpath


cygpath "D:\xxxx"

#只有C系统盘才有short name?
cygpath -ms "C:\Program Files (x86)\Windows Photo Viewer"

cygpath -help 


convt.sh 剔除OpenFOAM头部注释

sed -n '/^\/\/.*\/\//,$p' faceZones | tail -n +2

wget下载所有pdf文件

wget -r -A .pdf [url]

设置用户sudo权限

sudo usermod -a -G sudo username

获取脚本所在绝对路径

MYDIR=$(dirname $(readlink -f "$0"))

OCR Capture

for x in xa[a-d].dir;do echo $x; cd $x; /tmp/db_line_compare db_yyy.tx /tmp/$x/db_xxxx.txt 2>1.log; cd ..;done 
for x in xa[a-d].dir;do echo $x; cd $x; /tmp/get_lines 1.log xtotal.pad >xround; cd ..;done 
for x in x*.dir;do echo $x; cd $x;  cat xxx |  perl -MDigest::MD5=md5_hex -nlE'say md5_hex($_)' >db_xxxx.txt; cd ..;done

Associative Arrays

Associative Arrays
shell Bash的数组与关联数组的实现


命令行参数

#!/usr/bin/bash

arg1=$1
arg2=$2
arg3=$3

#把第4个起所有的命令行内容都给options
shift 3 
options="$@"

echo "options=" "'$options'" 
echo '"$options"'
echo "$options"

getopts

bash: getopts


unload所有已加载模块

How do I split a string on a delimiter in Bash?

if [ $LOADEDMODULES ];then
  IFS=':' read -ra MMM <<< $LOADEDMODULES 
  for m in "${MMM[@]}";do module unload $m; done   
fi 

BoomerAMG参数


declare -A amg_opts

amg_opts['strong_threshold']=0.6 
amg_opts['max_level']=25         
amg_opts['coarsen_type']="PMIS"  
amg_opts['agg_nl']=1
amg_opts['agg_num_paths']=1
amg_opts['truncfactor']=0.3
amg_opts['interp_type']="ext+i"
amg_opts['print_statistics']=3
amg_opts['print_debug']=1

AMG_OPTIONS=""
for k in ${!amg_opts[@]};do
    v="${amg_opts[$k]}"
    #echo "$k", "$v"
    AMG_OPTIONS="$AMG_OPTIONS -eqn_PP_pc_hypre_boomeramg_$k $v"
done

#=================================================================

#export PETSC_OPTIONS="-verbose 3 -timing -timing_prof -eqn_PP_ksp_view -eqn_PP_ksp_type gmres $AMG_OPTIONS "
export PETSC_OPTIONS="-verbose 3 -timing -timing_prof -eqn_PP_ksp_view -eqn_PP_ksp_type gmres  "

比较两个文件夹内同名数据文件

for x in mesh.dir/part.0/*;do y=`basename $x`;diff $x mesh.dir_new_omp/part.0/$y;done

top监控进程资源使用情况

mpirun -np 64 $UNS_SOLVER  $CASE_DIR  BCGS_2222 &
MPIRUN_PID=$!
sleep 10 
top -p  `pgrep -d ' ' 'bcgs' | awk '{print $1}'` -d 60 -n 10000 -b > top.log 
wait $MPIRUN_PID

字符串操作

在这里插入图片描述


How-to: Redirection and Process Substitution

How-to: Redirection and Process Substitution
在这里插入图片描述
What’s the difference between <<, <<< and < < in bash?


网络资源

Awesome Bash
Bash-Snippets
linux-shell-command-exit-codes
how-do-i-write-standard-error-to-a-file-while-using-tee-with-a-pipe
Bash内置变量

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值