如何为每行分配和显示索引号,并在PS3中选择第一个字段值到索引号,然后将其提供给下一个命令



我有这个bash脚本:

#!/usr/bin/env bash
job=`cat example.txt`
lines=`cat example.txt | cut -d " " -f1`
for i in ${!lines[@]}; do echo "$it" ${job[$i]}; done
PS3="Select desired job-id to check current status: ENTER HERE!!! => "
select id in "${lines[@]}"; do echo "you have selected ${id}" ; echo "looking into ${id}" ; break
done

使用这个example.txt文件:

53763958  4.01005  my_job  me_userid  r    2023-01-13T07:39:10.821  1
53763959  0.00000  your_job  you_userid  hqw  2023-01-13T07:37:29.525  1
53763961  0.00000  his_job  he_userid  hqw  2023-01-13T07:37:29.923  1
53763929  0.00000  her_job  her_userid  qw   2023-01-13T07:28:56.918  1

结果:

1) 53763958
53763959
53763961
53763929
Select desired job-id to check current status: ENTER HERE!!! => 1
you have selected 53763958
53763959
53763961
53763929
looking into 53763958
53763959
53763961
53763929

我期望的结果是:

1) 53763958  4.01005  my_job  me_userid  r    2023-01-13T07:39:10.821  1
2) 53763959  0.00000  your_job  you_userid  hqw  2023-01-13T07:37:29.525  1
3) 53763961  0.00000  his_job  he_userid  hqw  2023-01-13T07:37:29.923  1
4) 53763929  0.00000  her_job  her_userid  qw   2023-01-13T07:28:56.918  1
you have selected: 53763958-my_job

我将选择x)索引和第一个字段值(在本例中为53763958)必须被选为下一个命令的变量(echo "查找${id}")

首先,将整个文件的内容放入一个数组中。

readarray -t lines < example.txt
并使用该数组来构建菜单。在之后的做出选择之前,不会提取第一个字段。
PS3="Select desired job-id to check current status: ENTER HERE!!! => "
select job in "${lines[@]}"; do 
id=$(echo "$job" | cut -d " " -f1)
echo "you have selected ${id}"
echo "looking into ${id}"
break
done

相关内容