简体   繁体   English

在bash shell脚本中检查数组索引

[英]Checking array indices in bash shell scripting

I'm new to shell scripting...can anyone tell me how I can check whether I am on the first element of the array in my very first 'if' statement? 我是Shell脚本的新手...任何人都可以告诉我如何检查我是否在第一个'if'语句中位于数组的第一个元素上吗? It seems to be making i into something other than an integer. 似乎使我变成了整数以外的东西。

#! /bin/bash

namelist=($(lsblk -d -n -o NAME))
sizelist=($(lsblk -d -n -o SIZE))
seriallist=($(lsblk -d -n -o SERIAL))

IFS=$'\n'
modellist=($(lsblk -d -n -o MODEL))

echo "*********** HARD DRIVE DATA SCRIPT ***********"
echo "This script lets you choose the hard drives"
echo " "
echo "********************************************"
echo "* Step one: Choose your hard drive         *"
echo "********************************************"

for i in "${namelist[@]}"
do
    if [ "$i" -eq "0" ]
    then
        echo "First hard drive:"
    else
        echo "Next hard drive:"
    fi
    echo "* Size: ${sizelist[$i]}B"
    echo "* Model: ${modellist[$i]}"
    echo "* Serial number: ${seriallist[$i]}"
    echo "* Linux device name: ${namelist[$i]}"
    echo "* Partition List:"
    echo "Number  Name  Formatted as"
    lsblk -n -o NAME,LABEL,FSTYPE /dev/${namelist[$i]} | tail -n +2
    ((i++))
done
i=0
for item in "${namelist[@]}"
do
    echo $item # so you can see what your assumption actually is
    if [ "$i" -eq "0" ]
    then
        echo "First hard drive:"
    else
        echo "Next hard drive:"
    fi
    echo "* Size: ${sizelist[$i]}B"
    echo "* Model: ${modellist[$i]}"
    echo "* Serial number: ${seriallist[$i]}"
    echo "* Linux device name: ${namelist[$i]}"
    echo "* Partition List:"
    echo "Number  Name  Formatted as"
    lsblk -n -o NAME,LABEL,FSTYPE /dev/${namelist[$i]} | tail -n +2
    ((i++))
done

You're mixing up for syntaxes. 您正在混淆语法。 item refers here to each element of the array, not i the counter. 项目在这里是指数组的每个元素,而不是计数器。 Note that it won't work unless you get serials for each device that you end up with, which you probably won't. 请注意,除非获得最终使用的每个设备的序列号,否则它将无法正常工作。 In other words, you are assuming the counts of each array you generated is the same, that's not necessarily the case here. 换句话说,您假设生成的每个数组的计数都是相同的,在这里不一定是这种情况。 I get 0 results for serial in my system, for example, whether as root or as normal user. 例如,以root用户或普通用户身份获得的串行系统结果为0。 The only way I know to get serials is far more complex than you have here, so I won't attempt describe it, and it also won't always return values for your serials for each device. 我知道获取序列号的唯一方法比这里要复杂得多,因此我不会尝试对其进行描述,并且它也不会总是为每个设备返回序列号的值。

Also note that you will get results for fdx and srx with this logic, that is, floppy drives and optical drives, which you probably don't want. 还要注意,使用这种逻辑,即可能不需要的软盘驱动器和光盘驱动器,您将获得fdx和srx的结果。

I also tested, and get nothing for LABEL FSTYPE either, by the way. 我也进行了测试,顺便说一句,LABEL FSTYPE也一无所获。 So this method wouldn't work on a Debian type system, I can't speak for others. 因此,这种方法在Debian类型系统上不起作用,我不能为别人说。

If you were to use: 如果要使用:

for i in "${!namelist[@]}"

then you would not use ((i++)), which is only required if nothing else is changing i. 那么您就不会使用((i ++)),只有在没有其他改变i的情况下才需要。

This would work better using awk, which is designed to create such reports, but that's outside the scope of your question. 使用awk可以更好地工作,awk旨在创建此类报告,但这不在您的问题范围内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM