Follow Excellent, Success will Chase you

0%

shell常用代码

shell常用代码:

递归便利整个目录及子目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function getdir(){
echo $1
for file in $1/*
do
if test -f $file
then
echo $file
arr=(${arr[*]} $file)
else
getdir $file
fi
done
}
getdir ./src
#echo ${arr[@]}

打开多个窗口并登录ssh执行命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash

run_cmd_shell=$(tempfile)
cat > $run_cmd_shell << EOF
#!/bin/bash

ssh root@192.168.101.55 'pwd; ls; sleep 10'
EOF
echo "Run cmd shell: $run_cmd_shell"

for win in {1..5}
do
gnome-terminal -t "win-$win" --window -e \
"bash ${run_cmd_shell}"
done

判断是否使用sudo

1
2
3
4
5
6
7
8
# root or not
if [[ $EUID -ne 0 ]]; then
SUDO='sudo -H'
else
SUDO=''
fi

$SUD0 apt update -y

EUID: 在shell启动时被初始化的当前用户的有效ID,如果是root用户EUID=0

shell命令id -u作用相同

${:-}变量的默认值

1
NUM_THREADS=${NUM_THREADS:-4}

如果NUM_THREADS变量没有被定义,NUM_THREADS值将是:-后得默认值;如果NUM_THREADS变量被定义,NUM_THREADS值将定义值

1
2
3
4
5
6
7
8
9
10
11
12
=====>$bash -v aa.sh
#!/bin/bash
NUM_THREADS=${NUM_THREADS:-4}
echo "num-thread=$NUM_THREADS"
num-thread=4

=====>$bash -v aa.sh
#!/bin/bash
NUM_THREADS=8
NUM_THREADS=${NUM_THREADS:-4}
echo "num-thread=$NUM_THREADS"
num-thread=8

将逗号分隔的字符串转成换行

1
2
3
4
5
6
7
8
OLD_IFS="$IFS"
IFS=","
arr=(`cat 1.txt`)
IFS="$OLD_IFS"
for s in ${arr[@]}
do
echo $s | tr -d '"'
done

多线程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/sh

cpu_tmp=0
cpu=""
pmon=""
cycles=0
data_dir="test_rpt.$$"

while read LINE
do
if echo $LINE|grep 'cpu_tmp'
then
cpu_tmp=${LINE#*:}
fi


if echo $LINE|grep 'cycles'
then
cycles=${LINE#*:}
fi

done < run.cfg

#多线程:
# init fifo file
THREAD1DIR=3 && FIFONR=4 && FIFONAME="$$.ff" && mkfifo $FIFONAME && str="exec $FIFONR<> $FIFONAME" && eval $str && rm $FIFONAME -f
i=0
while [ $i -lt $THREAD1DIR ]; do
i=$((i+1))
echo
done >& $FIFONR
#for (( i=0; i<$THREAD1DIR; i++ )); do

# start test 1st level dir
all=`find . -maxdepth 1 -name "?????\.*"`
for i in $all
do
if [ -d $i ]
then
read
( echo $i" 1runing" && cd $i/ && ./$i'.run'.sh $cycles && cd - && echo >& $FIFONR ) &
fi
done <& $FIFONR

# rm fifo file
wait && str="exec $FIFONR>&-" && eval $str

#get log
[ -d $data_dir ] && rm -rf $data_dir || mkdir $data_dir && cp run.cfg $data_dir -f

for i in $all
do
if [ -d $i ]
then
des_dir=$data_dir'/'$i'/'
mkdir $des_dir
src_dir='./'$i'/'
file_path=`find $src_dir/ -name *_*.log`
echo $file_path
mv $file_path $des_dir
fi
done
chmod 777 './'$data_dir -R
rm *.ff -f

修改文件名后缀

1
2
3
4
5
6
for file in `find . -name "*.f90"`
do
newfile=${file%.*}.f77
#echo "$newfile"
mv $file $newfile
done

Linux批量更改文件后缀名

检查网段IP占用情况

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash

up=0
down=0

for siteip in $(seq 1 255)
do
#site="192.168.2.${siteip}"
site="172.16.189.${siteip}"
ping -c1 -W1 ${site} &> /dev/null
if [ "$?" == "0" ]; then
up=$[$up+1]
echo "$site is UP, cnt=$up"
else
down=$[$down+1]
echo "$site is DOWN, cnt=$down"
fi
done
# 除法
alive=`awk 'BEGIN{printf "%.2f\n",('$up'/'$(($up + $down))')}'`

echo "up:$up, down:$down, alive:$alive"

提取本地IP

1
ip=`ifconfig | grep "inet " | grep -v "127.0.0.1"| awk '{print $2}'`

-v: 排除

参考

  1. [shell中各种括号的作用()、(())、[]、[]]、{}
  2. shell变量详解
-------------本文结束感谢您的阅读-------------
  • 本文作者: Winddoing
  • 本文链接: https://winddoing.github.io/post/3776.html
  • 作者声明: 本博文为个人笔记, 由于个人能力有限,难免出现错误,欢迎大家批评指正。
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!