我是shell腳本編程的新手......任何人都可以告訴我如何在第一個'if'語句中檢查數組的第一個元素嗎?它似乎讓我成爲一個整數以外的東西。檢查bash shell腳本中的數組索引
#! /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
for循環將循環遍歷元素,即在第一次迭代中'i = $ {namelist [0]}'而不是'i = 0'如果您想遍歷索引,請使用'for i in' $ {!namelist [@]}「' –