37
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

シェルスクリプト for文 書き方(6つの例)

Posted at

for file in *

カレントディレクトリ内のすべてのファイル名を表示

for-example1
for file in *
do
  echo "${file}"
done

for text in `cat textlist`

コマンド置換により、textlist 内のテキストを1行ずつ読み込み表示

for-example2
for text in `cat textlist`
do
  echo "${text}"
done

for arg in "$@"

シェルスクリプトに与えられた引数を1つずつ表示

for-example3
for arg in "$@"
do
  echo "${arg}"
done

for文内に case

カレントディレクトリ内のファイルに対してバックアップ(.bak)を作成
ただし、すでにバックアップが存在している場合はスキップ

for-example4
for file in *
do
  case "${file}" in
    *.bak)
      continue
      ;;
  esac
  cp -p "${file}" "${file}".bak
done

N回数ループ

10回ループ

for-example5
for i in {1..10}
do
  echo "${i}"
done

特殊文字でループ

; や \ にはシングルクォートで囲んであげる
: や / に対しては囲まなくても動く

for-example6
for i in ';' '\'
do
  echo "${i}"
done
37
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
37
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?