Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

Bashでいろいろループする

More than 3 years have passed since last update.

配列をハードコーディングしてループ

items=(
    "altair"
    "betelgeuse"
    "canopus"
)

for item in "${items[@]}" ; do
    echo "[ ${item} ]"
done

ハードコーディング パターン2

items[0]="altair"
items[1]="betelgeuse"
items[2]="canopus"

for item in "${items[@]}" ; do
    echo "[ ${item} ]"
done

連番を生成してループ

for i in {1..10} ; do
    echo ${i}
done
for i in `seq 1 10`; do
    echo ${i}
done

ファイル一覧をループ

for file_name in * ;do
    echo ${file_name}
done
files=(`ls -1 somedir/`)
for file_name in "${files[@]}"; do
    echo ${file_name}
done

引数をすべてループ

for arg; do
  echo ${arg}
done
for arg in "$@"; do
  echo ${arg}
done
$ ./hoge.sh altair betelgeuse canopus

Thanks to: @akinomyoga, @mpyw

ファイルを読み込んで1行づつループ

while read line ; do
    echo ${line}

done < ${DATAFILE}

二次元表

#!/usr/bin/env bash

# iOS 公開用に PNG 画像のアイコンサイズを変更するスクリプト

sizes=(
"29 29"
"40 40"
"50 50"
"57 57"
"58 29@2x"
"60 60"
"72 72"
"76 76"
"80 40@2x"
"87 29@3x"
"100 50@2x"
"114 57@2x"
"120 60@2x"
"144 72@2x"
"152 76@2x"
"167 83.5@2x"
"180 60@3x"
)

if [ ! "${1}" ]; then
    echo "usage: ${0} <image-file>"
    exit 1
fi

bn=`basename ${1}`
bn=${bn%.*}
DN=`dirname ${1}`
for size in "${sizes[@]}"; do
    s=(${size})
    out_file=${bn}_${s[1]}.png
    echo ${s[0]} ${out_file}
    # sips --resampleWidth ${s[0]} --out ${out_file} ${1}
    convert -resize ${s[0]}x ${1} ${out_file}
done
ytyng
torico
マンガに関わるWEBサービスを軸に、面白いと思えることを企画・開発している会社です。オタク文化が好きな人は是非一緒に働きましょう! Python3 / Vue.js(TypeScript) / Flutterで開発中!
https://www.torico-corp.com
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away