LoginSignup
5

More than 5 years have passed since last update.

ドットインストールのシェルスクリプト入門視聴メモ(#10~#18)

Posted at

はじめに

ドットインストールのシェルスクリプト入門を見たので、その視聴メモをまとめておきたいと思います。
この記事は、後半(#10~#18)のまとめです。
前半(#01~#09)のまとめは、こちら

シェルスクリプト入門 (全18回) - ドットインストール
http://dotinstall.com/lessons/basic_shellscript

#10 if文で条件分岐をしてみよう (1)

if文

  • if文の書き方
 if test 条件式; then
      処理
 fi
  • testコマンドは、 [](大括弧)で書き換えることができる
hoge.sh
X=70
# if test $x -gt 60; then  と同じ
if [ $x -gt 60 ]; then
  echo "OK!"
fi

実行結果

$ ./hoge.sh
OK!

参考

[](大括弧) は、testを代替する命令。

$ which [
[: shell built-in command

#11 if文で条件分岐をしてみよう (2)

else文とelsif文

  • elif は、 else if文。
hoge.sh
X=20
if [ $x -gt 60 ]; then
  echo "OK!"
elif [ $x -gt 40]; then
  echo "Soso..."
else
  echo "NG..."
fi

実行結果

$ ./hoge.sh
NG...

#12 case文で条件分岐をしてみよう

case文

  • case文の書き方
    casein
    パターン1) 処理;;
    パターン2) 処理;;
    ...
    パターンn) 処理;;
    *)  処理;; (どれにも当てはまらないパターン)
    esac
hoge.sh
# case文

signal="red"
case $signal in
  "red")
    echo "Stop!"
    ;;
  "yellow")
    echo "Caution!"
    ;;
  "green")
    echo "Go!"
    ;;
  # どれにも当てはまらないパターンは *) を使う
  *)
    echo "..."
    ;;
esac

実行結果

$ ./hoge.sh
Stop!

#13 while文でループ処理をしてみよう

while文

  • while文の書き方
  while 条件式
  do
      処理
  done
hoge.sh
# while文

i=0
while [ $i -lt 10 ]
do
  i=`expr $i + 1`
  echo $i
done

実行結果

$ ./hoge.sh
1
2
3
4
(中略)
9
10

while文で無限ループ

  • while文で無限ループさせる場合は、 ヌルコマンド(:(コロン))を使う
  • break文でループから抜ける
foo.sh
# while文

i=0
while :
do
  i=`expr $i + 1`

  if [ $i -eq 3 ]; then
    continue
  fi

  if [$i -gt 10]; then
    break
  fi

  echo $i
done

実行結果(3をスキップした結果)

./ foo.sh
1
2
4
5
6
(中略)
9
10

#14 for文でループ処理をしてみよう

for文

  • for文の書き方
  for 変数 in 値リスト
  do
      処理
   done
  • seq コマンドは連番を出力するコマンド `seq 1 5` は、1から5までの数字、つまり「1 2 3 4 5」と同じ意味になる。
hoge.sh
# for文

a=(1 2 3 4 5)

# 全て同じ結果になる
# for i in 1 2 3 4 5
# for i in ${a[@]}
for i in `seq 1 5`
do
  echo $i
done

実行結果

$ ./hoge.sh
1
2
3
4
5

#15 コマンド引数を使ってみよう

コマンドライン引数

  • コマンドライン引数の処理に使用する変数のいろいろ
hoge.sh
# コマンドライン引数

# $0はプログラム自身
echo $0
# $1は1番目の引数
echo $1
# $2は2番目の引数
echo $2

# 10番目以降は{}を付けないといけない
# echo ${10}

# $@は引数全部
echo $@
# $#は引数の数
echo $#

実行結果

$ ./hoge.sh a b
./hoge.sh
a
b
a b
2

#16 ユーザーからの入力を受け付けよう

ユーザからの入力の受付

  • read文 でユーザーからの入力を受け付ける。
hoge.sh
# read

while :
do
  # ユーザからの入力を key に格納する
  read key
  echo "You pressed $key"
  if [ $key = "end ]; then
    break
  fi
done

実行結果

$ ./hoge.sh
hoge
You pressed hoge
foo
You pressed foo
end
You pressed end
(プログラムの処理終了)

ユーザからの入力を選択肢で受付

  • select文 でユーザからの入力を選択肢で受け付ける。
foo.sh
# select

# ユーザから入力を CODE DIE の中で選ばせる
select option in CODE DIE
do
 echo "You pressed $option"
 break;
done

実行結果

$ ./foo.sh
1) CODE
2) DIE
#? 1  (1を入力する)
You pressed CODE

#17 ファイルから入力してみよう

ファイルから入力

  • while文にリダイレクションするには done の直後に < を書く
date.txt
hoge
foo
bar
piyo
hoge.sh
i=1
# 1行ずつ読み込みlineに格納する
while read line
do
  echo "$i: $liine"
  i=`expr $i + 1`
# 引数の1つ目に指定したファイルから入力を受け付ける  
done <$1

実行結果

$ ./hoge.sh date.txt
1: hoge
2: foo
3: bar
4: piyo

#18 関数を使ってみよう

関数

  • 関数の宣言は function を使う(省略可)
  fuction 関数名() {
      処理
      ruturn 値
  }
  • 関数の呼び出しは () を省略可
  • 関数内で宣言した変数は関数外でも使える
hoge.sh
# 関数
# 関数の宣言 hello()だけでも可
function hello() {
  # $1と$2は関数の1番目の引数と2番目の引数
  echo "Hello $1 and $2"
  i=5
  echo $i
}

hello Tom Mike

# 関数内で宣言した変数も関数外でも使える
echo $i

実行結果

$ ./hoge.sh
Hello Tom and Mike
5
5

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
5