我拥有的代码查看序列中使用的最后一个活动设备,然后继续执行。如果序列中当前没有使用的缺口,我想填补。如何将其构建到代码中?
脚本按预期的顺序工作,按顺序进行。我不确定从哪里添加功能来填补空白。
输入:
bash script WABEL8499IPM 3
脚本:
SRCFILE="~/Desktop/deviceinfo.csv"
LOGDIR="~/Desktop/"
LOGFILE="$LOGDIR/DeviceNames.csv"
# base name, such as "WABEL8499IPM"
device_name=$1
# quantity, such as "2"
quantityNum=$2
# the largest in sequence, such as "WABEL8499IPM108"
max_sequence_name=$(cat $SRCFILE | grep -o -e "$device_name[0-9]*" | sort --reverse | head -n 1)
# extract the last 3digit number (such as "108") from max_sequence_name
max_sequence_num=$(echo $max_sequence_name | rev | cut -c 1-3 | rev)
# create a sequence of files starting from "WABEL8499IPM101" if there is not any "WABEL8499IPM".
if [ -z "$max_sequence_name" ]; then
max_sequence_name=device_name
max_sequence_num=100
fi
# Fill In Sequence If Any Spots are Available If 101, 102, 104,
# 105, 106, 107 and 108 are used I want to output 103 (to fill in),
# 109 and 110 (to continue sequence).
# create new sequence_name
# such as ["WABEL8499IPM109", "WABEL8499IPM110"]
array_new_sequence_name=()
for i in $(seq 1 $quantityNum); do
cnum=$((max_sequence_num + i))
array_new_sequence_name+=($(echo $device_name$cnum))
done
#CODE FOR CREATING OUTPUT FILE HERE
#for fn in ${array_new_sequence_name[@]}; do touch $fn; done;
# write log
for sqn in ${array_new_sequence_name[@]};
do
echo $sqn >> $LOGFILE
done
书面的实际结果:
#OUTPUT FROM WABEL8499IPM, 3
#IF WABEL8499IPM101,102,104,105 ARE USED THEN OUTPUT IS THIS:
WABEL8499IPM106
WABEL8499IPM107
WABEL8499IPM108
期望/预期结果:
#OUTPUT FROM WABEL8499IPM, 3
#IF WABEL8499IPM101,102,104,105 ARE USED THEN OUTPUT IS THIS:
WABEL8499IPM103
WABEL8499IPM106
WABEL8499IPM107
基本上,在我当前的脚本中,我正在进行API调用,以查看当前已注册到MDM中的内容,然后查看序列中的最高编号并输出序列中的下一个编号。目的是在序列没有完成的任何空白处填写序列。
答案 0 :(得分:0)
这可能对您有用:
# create a test source file
$ cat > src_file <<-EOF
foo WABEL8499IPM102 bar WABEL8499IPM108 foo bar
WABEL8499IPM106
foo WABEL8499IPM104
foo bar
WABEL8499IPM105 WAbel8499IPM110 bar
EOF
# the actual code
$ cat script
#!/usr/bin/env bash
pre=$1
num=$2
f=src_file
s=101
((num==0)) && exit
grep -oP "$pre\K[0-9]+" "$f" | sort -n > tmp
comm -13 tmp <(seq $s $((s+num+$(wc -l < tmp)))) | awk -v n=$num -v p="$pre" '{print p $0}NR>=n{exit}'
# execute the script
$ ./script WABEL8499IPM 5
WABEL8499IPM101
WABEL8499IPM103
WABEL8499IPM107
WABEL8499IPM109
WABEL8499IPM110