特殊変数

[vagrant@localhost shell]$ sed -i ‘s/\r//’ hello
[vagrant@localhost shell]$ ./hello yoshida
hello yoshida

#!/bin/bash

echo "hello $1"

$1, $2などを使う

#!/bin/bash

echo "hello $1"

echo $0
echo $#
echo $@

[vagrant@localhost shell]$ sed -i ‘s/\r//’ hello
[vagrant@localhost shell]$ ./hello a aa aaa
hello a
./hello
3
a aa aaa

ユーザーからの入力を受け取る

#!/bin/bash

read -p "名前: " name
echo "hello ${name}"

[vagrant@localhost shell]$ ./hello
名前: 田中
hello 田中

なんじゃこりゃーー

#!/bin/bash

read -p "Pick 3 colors: " c1 c2 c3
echo $c1
echo $c2
echo $c3

[vagrant@localhost shell]$ ./hello
Pick 3 colors: red geen blue
red
geen
blue

配列

#!/bin/bash

colors=(red blue pink)
colors[1]=silver
colors+=(green orange)
echo ${colors[@]}

[vagrant@localhost shell]$ sed -i ‘s/\r//’ hello
[vagrant@localhost shell]$ ./hello
red silver pink green orange

数値計算

#!/bin/bash

echo $((5 + 2))

n=10
((n=n + 2))
echo $n

if文

read -p "Name? " name
if [ "$name" = "yoshida" ] 
then
	echo "welcome"
else
	echo "you are not allowed"
fi

[vagrant@localhost shell]$ ./hello
Name? yoshida
welcome

#!/bin/bash

read -p "Name? " name
if [ "$name" = "yoshida" ] 
then
	echo "welcome"
elif [ "$name" = "kobayashi" ] 
then
	echo "welcome, too"
else
	echo "you are not allowed"
fi

[vagrant@localhost shell]$ sed -i ‘s/\r//’ hello
[vagrant@localhost shell]$ ./hello
Name? kobayashi
welcome, too

[vagrant@localhost shell]$ ./hello
Name?
empty …

ファイルを比較

#!/bin/bash

if [[ -f $0 ]]; then
	echo "file exists ..."
fi

[vagrant@localhost shell]$ sed -i ‘s/\r//’ hello
[vagrant@localhost shell]$ ./hello
file exists …