RHEL9 シェルスクリプト while文で配列を使用するサンプル

環境
Red Hat Enterprise Linux release 9.2 (Plow)

構文
配列名=(要素1 要素2 要素3)
while [ $i -lt ${#配列名[@]} ]

サンプルコード(data.sh)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#!/bin/bash
city=(tokyo oosaka yokohama)
i=0
while [ $i -lt ${#city[@]} ]
do
echo "$n ${city[$i]}"
((i++))
done
#!/bin/bash city=(tokyo oosaka yokohama) i=0 while [ $i -lt ${#city[@]} ] do echo "$n ${city[$i]}" ((i++)) done
#!/bin/bash
city=(tokyo oosaka yokohama)
i=0

while [ $i -lt ${#city[@]} ]
do
      echo "$n ${city[$i]}" 
      ((i++))
done

結果
tokyo oosaka yokohama

IT

Posted by arkgame