シェルスクリプトマガジン

シェルスクリプトの書き方入門(Vol.68記載)

著者:大津 真

本連載ではシェルスクリプトの書き方をやさしく紹介します。対象とするシェルは、多くのLinuxディストリビューションが標準シェルとして採用する「Bash」です。第4回は、繰り返し処理を実現する制御構造を中心に解説します。

シェルスクリプトマガジン Vol.68は以下のリンク先でご購入できます。

図1 リストにある果物名を一つずつ表示するシェルスクリプトの例

for name in メロン バナナ イチゴ ミカン
do
    echo $name
done

図2 シェルスクリプト「showArgs1.sh」の内容

#!/bin/bash
for name in $@
do
    echo $name
done

図3 シェルスクリプト「showArgs2.sh」の内容

#!/bin/bash
for name in "$@"
do
    echo $name
done

図4 シェルスクリプト「showArgs3.sh」の内容

#!/bin/bash
for name
do
    echo $name
done

図5 シェルスクリプト「colon_to_comma2.sh」の内容

#!/bin/bash
if [[ $# -eq 0 ]]; then
  echo "引数でファイルを指定してください"
  exit 1
fi
if [[ ! -f $1 ]]; then
  echo "$1が見つかりません"
  exit 1
fi
fname=$1
cp "$fname" "${fname}~"
tr ":" "," < "${fname}~" > "$fname"

図6 シェルスクリプト「colon_to_comma3.sh」の内容

#!/bin/bash
if [[ $# -eq 0 ]]; then
    echo "引数でファイルを指定してください"
    exit 1
fi
for file in $@
do
    if [[ ! -f $file ]]; then
        echo "$fileが見つかりません"
        exit 1
    fi
    fname=$file
    echo "変換中: $file"
    cp "$fname" "${fname}~"
    tr ":" "," < "${fname}~" > "$fname"
done

図7 シェルスクリプト「hello10.sh」の内容

#!/bin/bash
for i in $(seq 10)
do
    echo "$i: こんにちは"
done

図8 シェルスクリプト「case1.sh」の内容

#!/bin/bash
case $1 in
    [a-z]*)
        echo "アルファベット小文字で始まります"
        ;;
    [A-Z]*)
        echo "アルファベット大文字で始まります"
        ;;
    [0-9]*)
        echo "数字で始まります"
        ;;
    *)
        echo "その他"
esac

図9 シェルスクリプト「case2.sh」の内容

#!/bin/bash
for file
do
    case $file in
        *.txt)
            echo "テキスト: $file"
            ;;
        *.htm | *.html)
            echo "HTML: $file"
            ;;
        *)
            echo "その他: $file"
    esac
done

図10 シェルスクリプト「pat1.sh」の内容

#!/bin/bash
path="/home/o2/music/sample.test.mp3"
echo '${path#/*/} = ' ${path#/*/}
echo '${path##/*/} = ' ${path##/*/}
echo '${path%.*} = ' ${path%.*}
echo '${path%%.*} = ' ${path%%.*}

図11 シェルスクリプト「cgExt1.sh」の内容

#!/bin/bash
if [[ $# -eq 0 ]]; then
    echo "引数でファイルを指定してください"
    exit 1
fi

for file
do
    case $file in
        *.htm)
            newfile=${file%.*}.html
            echo "$file to $newfile"
            mv $file $newfile
            ;;
        *.jpeg)
            newfile=${file%.*}.jpg
            echo "$file to $newfile"
            mv $file $newfile
            ;;
    esac
done

図12 シェルスクリプト「while1.sh」の内容

#!/bin/bash
read -p "文字列? " str
while [[ -n $str ]]
do
    echo $str | tr "a-z" "A-Z"
    read -p "文字列? " str
done

図13 シェルスクリプト「while2.sh」の内容

#!/bin/bash
while true
do
    read -p "文字列? " str
    if [[ -z $str ]]; then
        break
    fi
    echo $str | tr "a-z" "A-Z"
done

図14 シェルスクリプト「cgExt2.sh」の内容

#!/bin/bash
if [[ $# -eq 0 ]]; then
    echo "引数でファイルを指定してください"
    exit 1
fi

for file
do
    if [[ ! -f $file ]]; then
        echo "${file}が見つかりません"
        continue
    fi
    case $file in
        *.htm)
            newfile=${file%.*}.html
            echo "$file to $newfile"
            mv $file $newfile
            ;;
        *.jpeg)
            newfile=${file%.*}.jpg
            echo "$file to $newfile"
            mv $file $newfile
            ;;
    esac
done