使用数组我得到了菜单的输出(类似的东西),如下所示;
1 file_1101
2 file_1102
3 file_1103
4 file_1104
5 file_1105
代码
#!/bin/bash
declare -a logs
order="acc_log.+csv$"
mennum=1
for file in ./*; do
if [[ $file =~ $order ]]; then
logs+=($(basename $file))
fi
done
count=${#logs[*]}
echo -e "The logs array contains $count files.\n"
for file in "${logs[@]}"; do
echo -e "$mennum $file"
((mennum++))
done
现在我想从每个文件中提取列。如何使用 cat 命令提取文件,或者如果您建议任何其他命令也会有所帮助
答案1
您无需创建自己的菜单,也可以使用select
select file in "${logs[@]}"; do
printf 'You selected "%s"\n' "$file"
break
done
cat "$file" # or do whatever with this file
或者不保存中间数组,我认为在这种情况下没有必要:
select file in acc_log*.csv; do
printf 'You selected "%s"\n' "$file"
break
done
cat "$file" # or do whatever with this file