sed tool basic

本文深入讲解了SED命令的基础用法及高级技巧,包括字符替换、文本插入、行删除等操作,并展示了如何结合脚本语言实现复杂任务。
语法
sed [options] '{command}' [filename]

替换命令
's/{old value}/{new value}/'
例子
$echo The tiger cubs will meet on Tuesday afer school | sed 's/tiger/wolf/'
The wolf cubs will meet on Tuesday afer school

$echo The tiger cubs will meet on Tuesday after school | sed -e 's/tiger/wolf/' -e 's/after/befor/'
The wolf cubs will meet on Tuesday befor school

$echo The tiger cubs will meet on Tuesday after scholl | sed 's/tiger/wolf/;s/after/before/'
The wolf cubs will meet on Tuesday before scholl

$echo The tiger cubs will meet on Tuesday after school | sed '
> s/tiger/wolf/
> s/after/before/
> '
The wolf cubs will meet on Tuesday before school

$echo The tiger cubs will meet this Tuesday at the same time as the meeting last Tuesday |sed 's/Tuesday/Thursday/'
The tiger cubs will meet this Thursday at the same time as the meeting last Tuesday

$echo The tiger cubs will meet this Tuesday at the same time as meeting last Tuesday | sed 's/Tuesday/Thursday/g'
The tiger cubs will meet this Thursday at the same time as meeting last Thursday

sed 可以用来将任意的可打印字符修改为任意其它
的可打印字符。如果您想将不可打印字符修改为可打印字符—例如,铃铛修改
为单词"bell"—sed 不是适于完成这项工作的工具(但tr 是)。


$cat sample_one
one 1
two 1
three 1
one 1
two 1
two 1
three 1

模式匹配
$sed '/two/ s/1/2/' sample_one
one 1
two 2
three 1
one 1
two 2
two 2
three 1

$sed '
> /two/ s/1/2/
> /three/ s/1/3/' sample_one
one 1
two 2
three 3
one 1
two 2
two 2
three 3
$sed '
> /two/ s/1/2/
> /three/ s/1/3/' sample_one > sample_two

$cat sample_two
one 1
two 2
three 3
one 1
two 2
two 2
three 3
$

使用sed脚本文件
$cat sedlist
/two/ s/1/2/
/three/ s/1/3/
$sed -f sedlist sample_one
one 1
two 2
three 3
one 1
two 2
two 2
three 3

行匹配
$sed '5,6 s/1/2/' sample_one
one 1
two 1
three 1
one 1
two 2
two 2
three 1

禁止显示 -n
$sed -n -f sedlist sample_one
$

显示选项 -p
$cat sedlist
/two/ s/1/2/p
/three/ s/1/3/p
$sed -n -f sedlist sample_one
two 2
three 3
two 2
two 2
three 3
$

$sed -n -f sedlist sample_one > sample_two
$cat sample_two
two 2
three 3
two 2
two 2
three 3

只显示第三行到第五行
$sed -n '3,5p' sample_one
three 1
one 1
two 1
$


删除行
删除匹配行
$sed '/two/ d' sample_one
one 1
three 1
one 1
three 1
删除指定行(第一到第三行)
$sed '1,3 d' sample_one
one 1
two 1
two 1
three 1
对于流编辑器,一般当它们涉及
到全局表达式时,特别是应用于删除操作时,有几点要记住:
上三角号(^) 表示一行的开始,因此,如果"two" 是该行的头三个字符,则
sed '/^two/ d' sample_one
将只删除该行。

美元符号($) 代表文件的结尾,或一行的结尾,因此,如果"two" 是该行的最
后三个字符,则
sed '/two$/ d' sample_one
将只删除该行。

sed '/^$/ d' {filename}
删除文件中的所有空白行。

以下命令将"1" 替换为"2",以及将"1" 替换为"3",并删除文件中所有尾随的空行:
$ sed '/two/ s/1/2/; /three/ s/1/3/; /^$/ d' sample_one

以下命令将删除文件中所有的行,从第一行直到第一个空行:
sed '1,/^$/ d' {filename}


添加和插入文本

可以结合使用sed 和"a" 选项将文本添加到一个文件的末尾。实现方法如
下:
$ sed '$a\
> This is where we stop\
> the test' sample_one
one 1
two 1
three 1
one 1
two 1
two 1
three 1
This is where we stop
the test
$

在该命令中,美元符号($) 表示文本将被添加到文件的末尾。反斜线(\) 是必
需的,它表示将插入一个回车符。如果它们被遗漏了,则将导致一个错误,显
示该命令是错乱的;在任何要输入回车的地方您必须使用反斜线。


要将这些行添加到第4 和第5 个位置而不是末尾,则命令变为:
$sed '3a\
> This is where we stop\
> the rest' sample_one
one 1
two 1
three 1
This is where we stop
the rest
one 1
two 1
two 1
three 1

和几乎所有的编辑器一样,您可以选择插入而不
是添加(如果您希望这样的话)。这两者的区别是添加跟在指定的行之后,而
插入从指定的行开始。当用插入来代替添加时,只需用"i" 来代替"a",如下
所示:
$sed '3i\
> this is where we stop\
> the rest' sample_one
one 1
two 1
this is where we stop
the rest
three 1
one 1
two 1
two 1
three 1

读写文件

执行替换, 并将1-3 行写到名称为sample_three 的文件中:
$sed '
> /two/ s/1/2/
> /three/ s/1/3/
> 1,3 w sample_three' sample_one
one 1
two 2
three 3
one 1
two 2
two 2
three 3
$cat sample_three
one 1
two 2
three 3

修改命令

除了替换项目之外,还可以将行从一个值修改为另一个值。要记住的是,替换
是对字符逐个进行,而修改功能与删除类似,它影响整行:

$sed '/two/ c\
> we are no longer using two' sample_one
one 1
we are no longer using two
three 1
one 1
we are no longer using two
we are no longer using two
three 1
修改命令与替换的工作方式很相似,但在范围上要更大些—将一个项目完全替
换为另一个项目,而无论字符内容或上下文。夸张一点讲,当使用替换时,只
有字符"1" 被字符"2" 替换,而当使用修改时,原来的整行将被修改。在两种
情况下,要寻找的匹配条件都仅为"two"。


修改全部但……
对于大多数sed 命令,详细说明各种功能要进行何种修改。利用感叹号,可
以在除指定位置之外的任何地方执行修改—与默认的操作完全相反。
例如,要删除包含单词"two" 的所有行,操作为:
$sed '/two/ d' sample_one
one 1
three 1
one 1
three 1
$sed '/two/ !d' sample_one
two 1
two 1
two 1

如果您有一个文件包含一系列项目,并且想对文件中的每个项目执行一个操
作,那么首先对那些项目进行一次智能扫描并考虑将要做什么是很重要的。为
了使事情变得更简单,您可以将sed 与任意迭代例程(for、while、until)结
合来实现这一目的。
比如说,假定您有一个名为"animals" 的文件,其中包含以下项目:
pig
horse
elephant
cow
dog
cat
您希望运行以下例程:
#mcd.ksh
for I in $*
do
echo Old McDonald had a $I
echo E-I, E-I-O
done
结果将为,每一行都显示在"Old McDonald has a" 的末尾。虽然对于这些项
目的大部分这是正确的,但对于"elephant" 项目,它有语法错误,因为结果应
当为"an elephant" 而不是"a elephant"。利用sed,您可以在来自shell 文
件的输出中检查这种语法错误,并通过首先创建一个命令文件来即时地更正它
们:
#sublist
/ a a/ s/ a / an /
/ a e/ s/ a / an /
/a i/ s / a / an /
/a o/ s/ a / an /
/a u/ s/ a / an /
然后执行以下过程:
$ sh mcd.ksh 'cat animals' | sed -f sublist
现在,在运行了mcd 脚本之后,sed 将在输出中搜索单个字母a (空格,
"a",空格)之后紧跟了一个元音的任意位置。如果这种位置存在,它将把该序
列修改为空格,"an",空格。这样就使问题更正后才显示在屏幕上,并确保各
处的编辑人员在晚上可以更容易地入睡。结果是:
Old McDonald had a pig
E-I, E-I-O
Old McDonald had a horse
E-I, E-I-O
Old McDonald had an elephant
E-I, E-I-O
Old McDonald had a cow
E-I, E-I-O
Old McDonald had a dog
E-I, E-I-O
Old McDonald had a cat
E-I, E-I-O


提前退出

sed 默认读取整个文件,并只在到达末尾时才停止。不过,您可以使用退出命
令提前停止处理。只能指定一条退出命令,而处理将一直持续直到满足调用退
出命令的条件。
例如,仅在文件的前五行上执行替换,然后退出:
$ sed '
> /two/ s/1/2/
> /three/ s/1/3/
> 5q' sample_one
one 1
two 2
three 3
one 1
two 2
$
在退出命令之前的项目可以是一个行号(如上所示),或者一条查找/匹配命
令:
$ sed '
> /two/ s/1/2/
> /three/ s/1/3/
> /three/q' sample_one
one 1
two 2
three 3
$
您还可以使用退出命令来查看超过一定标准数目的行,并增加比head 中的功
能更强的功能。例如,head 命令允许您指定您想要查看一个文件的前多少行
—默认数为10,但可以使用从1 到99 的任意一个数字。如果您想查看一个
文件的前110 行,您用head 不能实现这一目的,但用sed 可以:
sed 110q filename //和head进行比较


当使用sed 时,要记住的重要事项是它的工作方式。它的工作方式是:读入
一行,在该行上执行它已知要执行的所有任务,然后继续处理下一行。每一行
都受给定的每一个编辑命令的影响

如果您的操作顺序没有十分彻底地考虑清楚,那么这可能会很麻烦。例如,假
定您需要将所有的"two" 项目修改为"three",然后将所有的"three" 修改为
"four":
$ sed '
> /two/ s/two/three/
> /three/ s/three/four/' sample_one
one 1
four 1
four 1
one 1
four 1
four 1
four 1
$
最初读取的"two" 被修改为"three"。然后它满足为下一次编辑建立的准则,
从而变为"four"。最终的结果不是想要的结果—现在除了"four" 没有别的项目
了,而本来应该有"three" 和"four"。
当执行这种操作时,您必须非常用心地注意指定操作的方式,并按某种顺序来
安排它们,使得操作之间不会互相影响。例如:
$ sed '
> /three/ s/three/four/
> /two/ s/two/three/' sample_one
one 1
three 1
four 1
one 1
three 1
three 1
four 1
$
这非常有效,因为"three" 值在"two" 变成"three" 之前得到修改。
#!/bin/bash all_options= check_disk_info=0 disk_command=0 ac_cycle=0 dc_cycle=0 reboot_cycle=0 quit_option=0 io_test=0 tmp_log_path_tmp=$(pwd) tmp_log_path="${tmp_log_path_tmp}/$(echo $0 | awk -F '/' '{print $NF}' | awk -F .sh '{print $1}')" server_ip="" server_passwd="" pdu_ip_port="" PROGRAM_NAME=$(basename $0) date_time=`date +%Y.%m.%d.%H.%M.%S` sleep_time=40 ac_cycle_flag=0 dc_cycle_flag=0 reboot_cycle_flag=0 quit_option=0 io_test=0 all_times=0 function red_message() { echo -e "\033[31m${1}\033[0m" } function green_message() { echo -e "\033[32m${1}\033[0m" } function yellow_message() { echo -e "\033[33m${1}\033[0m" } function error() { echo "$PROGRAM_NAME run with error at $date_time : $1" echo "$PROGRAM_NAME run with error at $date_time : $1" >> ${reboot_flag}_all_log exit 1 } function usage_and_exit() { echo -e "*-------------------------------------------------------------------* Filename : $0 Version : 1.0 Description : run cycle test like: nvme reset without IO Generate 10g file and Save MD5 before cycle test check MD5 when cycle test complete $(yellow_message "!!! Note: If your disk has partitions, please format it before testing") $(yellow_message "!!! Note: Please confirm the tool of DERA nvme tool under ${tmp_log_path_tmp}/") *-------------------------------------------------------------------* $PROGRAM_NAME support option is : [-c|-n number|-l number|--help] -c <Check disk info (speed,width,smart,state)> -n [num] <Custom hotplug times,a space is needed after the follow number!> -l [num] <Nvme disk count,excluding the os disk!> --help <Display this content> For Example: If you want to test hotplug 10 cycle,run ./$PROGRAM_NAME -n 100 If you want to check disk info,run ./$PROGRAM_NAME -c -n [num] If you need add nvme disk count,run ./$PROGRAM_NAME -c -n [num] -l [num] Suggestions are as follows: $(yellow_message "When testing a cycle, it is best to add parameters -c,-n [num],l [num]") run as: ./$PROGRAM_NAME -n [cycle_num] -c -l [disk_count] " exit 0 } function deal_command_log_files_when_error() { if [ -e ${tmp_log_path}/genfile_basic ] ;then while read files do if [ $2 = "cp" ];then cp ${tmp_log_path}/$files $1 2> /dev/null else mv ${tmp_log_path}/$files $1 2> /dev/null fi done < ${tmp_log_path}/genfile_basic fi if [ -e ${tmp_log_path}/genfile_after ];then while read files do mv ${tmp_log_path}/$files $1 2> /dev/null done < ${tmp_log_path}/genfile_after fi } function collect_log_delect_startup_before_exit() { name_flag=$1 dir_name=${name_flag}_${date_time} mkdir -p "${tmp_log_path}/$dir_name/before_after_info" deal_command_log_files_when_error "${tmp_log_path}/$dir_name" "mv" cd ${tmp_log_path} mv ${reboot_flag}_all_times ${reboot_flag}_all_log failed_times succeed_times "$dir_name" 2> /dev/null mv *after *basic "$dir_name/before_after_info" 2> /dev/null mv ${tmp_log_path}/$dir_name/*after ${tmp_log_path}/$dir_name/*basic "$dir_name/before_after_info" 2> /dev/null mv nohup.out smart_info/ pci_info/ fio_log/ dmesg_log/ state_info/ md5_check/ nor_log/ telemetry_log/ vendor_smart/ -t "$dir_name" 2> /dev/null cd - > /dev/null pkill -9 fio } function generate_file() { file_result=`cat "${tmp_log_path}/genfile_$1" 2> /dev/null` #preserve dmesg log mkdir -p ${tmp_log_path}/dmesg_log #检测硬盘的信息 if [ $check_disk_info -eq 1 ] ;then fdisk -l 2> /dev/null | sed -n 's/^\(磁盘\|Disk\) \([^::]*\)\(:\|:\)\([^,]*\),.*$/\2 \4/p' | awk '{if (( $2>=32 && ($3=="GB" || $3=="GiB"))||($3=="TB"||$3=="TiB")) print }'| sort > "${tmp_log_path}/disk_$1" #test -z "$file_result" && echo "disk_$1" >> "${tmp_log_path}/genfile_$1" mkdir -p ${tmp_log_path}/smart_info mkdir -p ${tmp_log_path}/pci_info #HDD/SSD get smartctl info for sdy in $(lsblk |grep -vw ${osdisk}|grep disk |awk '{print $1}' |grep sd) do smartctl -a /dev/${sdy} > ${tmp_log_path}/smart_info/${sdy}_smart_${all_times} done ###nvme SSD get info log for nvmey in $(lsblk |grep -vw ${osdisk}|grep disk |awk '{print $1}' |grep nvme) do sn=$(nvme list | awk '/'${nvmey}'/ {print $3}') result_cycle=$((${all_times} % 5)) #[ "$result_cycle" -eq 0 ] && ${tmp_log_path}/nvme dera nor-log /dev/$nvmey -f ${tmp_log_path}/nor_log/${sn}_${all_times}_nor.log nvme smart-log /dev/${nvmey} > ${tmp_log_path}/smart_info/"${sn}"_smart_${all_times} nvme intel smart-log-add /dev/${nvmey} > ${tmp_log_path}/smart_info/"${sn}"_smart_add_${all_times} if `echo "$sn" | grep -q '^D8'` ;then #if_d8_ssd="yes" mkdir -p ${tmp_log_path}/vendor_smart ${tmp_log_path}/nvme drd8 get-vendor-smart /dev/$nvmey > ${tmp_log_path}/vendor_smart/"${sn}"_vendor_smart_${all_times} #[ "$result_cycle" -eq 0 ] && ${tmp_log_path}/nvme telemetry-log /dev/$nvmey --output-file ${tmp_log_path}/telemetry_log/${sn}_${all_times}_telemetry.log -d 4 [ "$result_cycle" -eq 0 ] && mkdir -p ${tmp_log_path}/telemetry_log/${sn}_${all_times}_log && ${tmp_log_path}/nvme drd8 get-tlog /dev/${nvmey} -s ${tmp_log_path}/telemetry_log/${sn}_${all_times}_log -e -c else #if_d8_ssd="no" mkdir -p ${tmp_log_path}/state_info #mkdir -p ${tmp_log_path}/nor_log [ "$result_cycle" -eq 0 ] && ${tmp_log_path}/nvme dera nor-log /dev/$nvmey -f ${tmp_log_path}/nor_log/${sn}_${all_times}_nor.log ${tmp_log_path}/nvme dera state /dev/${nvmey} > ${tmp_log_path}/state_info/"${sn}"_state_${all_times} cat ${tmp_log_path}/state_info/"${sn}"_state_${all_times} | grep -E "device_status" > ${tmp_log_path}/state_${sn}_$1 test -z "$file_result" && echo "state_${sn}_$1" >> "${tmp_log_path}/genfile_$1" fi cat ${tmp_log_path}/smart_info/"${sn}"_smart_${all_times} | grep -E "critical_warning|media_errors|num_err_log_entries" > ${tmp_log_path}/smart_${sn}_$1 cat ${tmp_log_path}/smart_info/"${sn}"_smart_add_${all_times} | grep -E "crc_error_count" > ${tmp_log_path}/smart_add_${sn}_$1 test -z "$file_result" && echo "smart_${sn}_$1" >> "${tmp_log_path}/genfile_$1" test -z "$file_result" && echo "smart_add_${sn}_$1" >> "${tmp_log_path}/genfile_$1" done lspci -vvd 1d78: > ${tmp_log_path}/pci_info/"pci_vvd_${all_times}" lspci -vvd 1d78: | grep -E "Non-V|LnkSta:" > ${tmp_log_path}/pci_vvd_$1 #nvme list|awk 'NR >2' |awk '{print $1," ",$2," ",$3," ",$4," ",$5," ",$9,$10," ",$11," "$12,$13," ",$16}' > ${tmp_log_path}/nvmelist_$1 nvme list|awk 'NR >2' > ${tmp_log_path}/nvmelist_$1 #test -z "$file_result" && echo "nvmelist_$1" >> "${tmp_log_path}/genfile_$1" test -z "$file_result" && echo "pci_vvd_$1" >> "${tmp_log_path}/genfile_$1" fi if [ "${io_test}" -eq 1 ] && [ "$all_times" -lt "$custom_times" ];then mkdir -p ${tmp_log_path}/fio_log #nvme FIO with 4K QD=128 for nvmex in $(lsblk -l | grep -v ${osdisk} |awk '{print $1}' |grep nvme | grep "p2$") do sn=$(nvme list | awk '/'${nvmex//p2/}'/ {print $3}') nohup fio --filename=/dev/${nvmex} --ioengine=libaio --randrepeat=0 --norandommap --thread --direct=1 --group_reporting --name=mytest --runtime=1m --time_based --numjobs=1 --iodepth=128 --rw=randrw --rwmixread=50 --bs=4k >> ${tmp_log_path}/fio_log/${sn}_randrw_50_50_${all_times}.log & done wait && wait fi if [ "$all_times" -eq "$custom_times" ];then echo "wait md5 check ..." check_md5_after ###nvme SSD get info log for nvmey in $(lsblk |grep -vw ${osdisk}|grep disk |awk '{print $1}' |grep nvme) do sn=$(nvme list | awk '/'${nvmey}'/ {print $2}') #result_cycle=$((${all_times} % 20)) ${tmp_log_path}/nvme dera nor-log /dev/$nvmey -f ${tmp_log_path}/nor_log/${sn}_${all_times}_nor.log done fi dmesg -T > ${tmp_log_path}/dmesg_log/"dmesg_$all_times" } function show_basic_file_to_user() { if [ -e ${tmp_log_path}/genfile_basic ] ;then while read basic do echo -e "$basic shows that:\n" cat ${tmp_log_path}/$basic echo -e "\n\n" done < ${tmp_log_path}/genfile_basic echo "is all basic is right (Y/N):" while read input do if [ $input != 'y' -a $input != 'Y' ] && [ $input != 'n' -a $input != 'N' ]; then echo "input error Y/y for right, N/n for error,Input again:" continue else if [ $input = 'y' -o $input = 'Y' ] ;then echo "user confirmed!" break else collect_log_delect_startup_before_exit "invalid" error "user ensure basic is error" fi fi done fi } function compare_file_result() { tmp_result= if [ -e ${tmp_log_path}/genfile_after ];then while read files do filename=`echo $files|sed 's/^\(.*\)_after/\1/'` tmp=`diff ${tmp_log_path}/${filename}_basic ${tmp_log_path}/${filename}_after | wc -l` test $tmp -ne 0 && tmp_result="$tmp_result $filename" done < ${tmp_log_path}/genfile_after fi echo $tmp_result } function md5_file_gen() { [ ! -d ${tmp_log_path}/md5_check ] && mkdir -p ${tmp_log_path}/md5_check if [ "$io_test" -eq 1 ] ;then for nvmey in $(lsblk |grep -i disk |grep -vw $osdisk |awk {'print $1'} |grep nvme) do { sn=$(nvme list | awk '/'${nvmey}'/ {print $3}') nvme format -s 1 /dev/${nvmey} --force [ ! -d /mnt/${sn} ] && mkdir -p /mnt/${sn} echo yes | parted /dev/${nvmey} mklabel gpt echo I | parted /dev/${nvmey} mkpart primary 0 50% parted /dev/${nvmey} mkpart primary 50% 100% ; sleep 10 echo yes | mkfs.ext4 /dev/${nvmey}p1 && sleep 10 mount /dev/${nvmey}p1 /mnt/${sn} && sleep 1 fio --name=write_latency --filename=/mnt/${sn}/${sn}_100Gfile --ioengine=libaio --direct=1 --numjobs=1 --iodepth=128 --rw=write --group_reporting --norandommap --randrepeat=0 --size=100G --bs=1M md5sum /mnt/${sn}/${sn}_100Gfile > ${tmp_log_path}/md5_check/${sn}_md5_basic sync && sync umount /mnt/${sn} } & done else for nvmey in $(lsblk |grep -i disk |grep -vw $osdisk |awk {'print $1'} |grep nvme) do { sn=$(nvme list | awk '/'${nvmey}'/ {print $3}') nvme format -s 1 /dev/${nvmey} --force [ ! -d /mnt/${sn} ] && mkdir -p /mnt/${sn} echo yes| mkfs.ext4 /dev/${nvmey} && sleep 10 mount /dev/${nvmey} /mnt/${sn} && sleep 1 fio --name=write_latency --filename=/mnt/${sn}/${sn}_100Gfile --ioengine=libaio --direct=1 --numjobs=1 --iodepth=128 --rw=write --group_reporting --norandommap --randrepeat=0 --size=100G --bs=1M md5sum /mnt/${sn}/${sn}_100Gfile > ${tmp_log_path}/md5_check/${sn}_md5_basic sync && sync umount /mnt/${sn} } & done fi wait && wait lsblk -f echo "check parted is right (Y/N):" while read input do if [ $input != 'y' -a $input != 'Y' ] && [ $input != 'n' -a $input != 'N' ]; then echo "input Y/y for right, N/n for error,Input again:" continue else if [ $input = 'y' -o $input = 'Y' ] ;then echo "user confirmed!" break else exit fi fi done } function check_md5_after() { [ ! -d ${tmp_log_path}/md5_check ] && mkdir -p ${tmp_log_path}/md5_check if [ "$io_test" -eq 1 ] ;then for nvmey in $(lsblk |grep -i disk |grep -vw $osdisk |awk {'print $1'} |grep nvme) do { sn=$(nvme list | awk '/'${nvmey}'/ {print $3}') [ ! -d /mnt/${sn} ] && mkdir -p /mnt/${sn} mount /dev/${nvmey}p1 /mnt/${sn} && sleep 1 md5sum -c ${tmp_log_path}/md5_check/${sn}_md5_basic | tee ${tmp_log_path}/md5_check/${sn}_md5_after sync && sync umount /mnt/${sn} } & done else for nvmey in $(lsblk |grep -i disk |grep -vw $osdisk |awk {'print $1'} |grep nvme) do { sn=$(nvme list | awk '/'${nvmey}'/ {print $3}') [ ! -d /mnt/${sn} ] && mkdir -p /mnt/${sn} mount /dev/${nvmey} /mnt/${sn} && sleep 1 md5sum -c ${tmp_log_path}/md5_check/${sn}_md5_basic | tee ${tmp_log_path}/md5_check/${sn}_md5_after sync && sync umount /mnt/${sn} } & done fi wait && wait } while getopts :cin:l: opt do case $opt in c) check_disk_info=1; disk_command=1; all_options="$all_options -c" ;; n) custom_times=$OPTARG; all_options="$all_options -n $custom_times" ;; i) io_test=1 ; all_options="$all_options -i" ;; l) disk_list=1 ; disk_count=$OPTARG;all_options="$all_options -l $disk_count" ;; ?) usage_and_exit ;; esac done [ $# -lt 1 ] && usage_and_exit && exit reboot_flag="hotplug" [ ! -d ${tmp_log_path} ] && mkdir -p ${tmp_log_path} if [ -f ${tmp_log_path_tmp}/nvme ] ;then nvme_version=`${tmp_log_path_tmp}/nvme --version | awk '/^nvme version/ {print $3}' | tr -d '.'` [ "${nvme_version}" -lt "398" ] && echo "Please download new nvme tool,version need newer than 3.9.8" && exit chmod +x ${tmp_log_path_tmp}/nvme cp ${tmp_log_path_tmp}/nvme ${tmp_log_path} else yellow_message "Please download DERA nvme tool to ${tmp_log_path_tmp}/" exit fi #osdisk bootdisk=`df -h | awk '{print $1}' | grep -iE "/dev/sd" | sed 's/[0-9]//g' |sort -u` if test -z "$bootdisk" ;then #OS disk = nvmeXn1 bootdisk=`df -h | awk '{print $1}' | grep -iE "/dev/nvme" | sed 's/p[0-9]//g' |sort -u` osdisk=`echo ${bootdisk#/dev/}` else osdisk=`echo ${bootdisk#/dev/}` fi nvme_count_cur=$(nvme list | grep -vw $osdisk | grep -c nvme) if [ "${nvme_count_cur}" != "${disk_count}" ] ;then yellow_message "!!! Note: Check disk count failed,continue?(yes/no)" while read input do if [ $input != 'y' -a $input != 'Y' ] && [ $input != 'n' -a $input != 'N' ]; then echo "input Y/y for continue, N/n for stop,Input again:" continue else if [ $input = 'y' -o $input = 'Y' ] ;then echo "user confirmed, continue!" break else exit fi fi done fi parted_if=`lsblk | grep -v ${osdisk} | grep -E "n1p1|n1p2"` [ ! -z "$parted_if" ] && yellow_message "!!! Note: Please format it before testing" && exit generate_file "basic" show_basic_file_to_user echo "Generate 100g file...." md5_file_gen echo "the 1 times $reboot_flag at $date_time_before with option $all_options to generate basic file" | tee -a ${tmp_log_path}/${reboot_flag}_all_log for ((all_times=1;all_times<="$custom_times";all_times++));do date_time_before=`date +%Y.%m.%d.%H.%M.%S` echo "the $all_times times $reboot_flag at $date_time_before start" | tee -a ${tmp_log_path}/${reboot_flag}_all_log echo "Please output disk,If complete input enter..." read sleep 30 echo "Please input disk,If complete input enter..." read sleep 20 while((1));do ssd_list=`nvme list | grep -v "$osdisk" | awk '/nvme/ {print $1}'` nvme_count=`nvme list | grep -v "$osdisk"| grep -c nvme` if [ "$nvme_count" -ne "$disk_count" ] ;then echo "check nvme count fail,cur:$nvme_count;exp:$disk_count" echo "wait 10s......" sleep 10 else echo "check nvme count ok!" break fi done generate_file "after" result=$(compare_file_result) sync date_time_after=`date +%Y.%m.%d.%H.%M.%S` if [ -z "$result" ];then #没有错误的时候的处理方式,succeed次数加1 touch ${tmp_log_path}/succeed_times succeed=`cat ${tmp_log_path}/succeed_times` succeed=$(($succeed+1)) echo $succeed > ${tmp_log_path}/succeed_times echo "the $all_times times $reboot_flag at $date_time_after with no error" | tee -a ${tmp_log_path}/${reboot_flag}_all_log else #出错的时候的处理方式,failed次数加1 touch ${tmp_log_path}/failed_times failed=`cat ${tmp_log_path}/failed_times` failed=$(($failed+1)) echo $failed > ${tmp_log_path}/failed_times #处理出错的日志 echo "the $all_times times $reboot_flag at $date_time_after , an error occurred:check $result failed " | tee -a ${tmp_log_path}/${reboot_flag}_all_log dir_name=${tmp_log_path}/err_env_$date_time_after mkdir -p "$dir_name" deal_command_log_files_when_error "$dir_name" "cp" cp ${tmp_log_path}/${reboot_flag}_all_log "$dir_name" 2> /dev/null if [ $disk_command -eq 1 ] ;then mv ${tmp_log_path}/fdisk_l_after "$dir_name" 2> /dev/null cp ${tmp_log_path}/fdisk_l_basic "$dir_name" 2> /dev/null fi fi sleep 10 done echo "we has run $custom_times times $reboot_flag automaticly" >> ${tmp_log_path}/${reboot_flag}_all_log collect_log_delect_startup_before_exit "times_end" 在执行测试时,提示错误:root@bogon:/home/meitest/fuc_006# ./NVMe_FUNC_00006.sh -n 1 -l 1 !!! Note: Check disk count failed,continue?(yes/no) yes input Y/y for continue, N/n for stop,Input again: y user confirmed, continue! Generate 100g file.... Success formatting namespace:1 Success formatting namespace:1 Success formatting namespace:1 Success formatting namespace:1 mkdir: cannot create directory ‘/mnt/D87527K8015331’: No space left on device mkdir: cannot create directory ‘/mnt/D87527K8019231’: No space left on device mkdir: cannot create directory ‘/mnt/D87528K8001A31’: No space left on device mkdir: cannot create directory ‘/mnt/D87528K8001531’: No space left on device mke2fs 1.47.2 (1-Jan-2025) mke2fs 1.47.2 (1-Jan-2025) mke2fs 1.47.2 (1-Jan-2025) mke2fs 1.47.2 (1-Jan-2025) Discarding device blocks: Discarding device blocks: Discarding device blocks: Discarding device blocks: Success formatting namespace:1 Success formatting namespace:1 mkdir: cannot create directory ‘/mnt/D87527K801EU31’: No space left on device mkdir: cannot create directory ‘/mnt/D87528K8001P31’: No space left on device
最新发布
09-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值