LoginSignup
0
1

More than 3 years have passed since last update.

シェルスクリプトの基礎#1

Posted at

シェルスクリプトの基礎#1です。

クオーティング

シェルの中身が以下の内容である時
' '(シングルコーテーション)
" "(ダブルコーテーション)で変数の展開の仕方が異なる。

work10.sh
#!/bin/bash

echo 'Hello'
name='Tadashi'
echo 'My name is $name'
echo "My name is $name"

シェルを実行した結果、
' '(シングルコーテーション)の場合は変数展開が無効、
" "(ダブルコーテーション)の場合は変数展開され、変数nameの中身が
展開されている。

[wataru@localhost work]$ ./work10.sh
Hello
My name is $name
My name is Tadashi

コマンド置換

コマンドの結果をシェルスクリプト中で利用したい時があります。
コマンド置換をを利用すれば、コマンドの結果を文字列として取得することが出来る。
$( )の形式でカッコ内に実行したいコマンドを記述する。

例として、dateコマンドで日付を出力し、変数filenameに格納。
その結果をtouchコマンドの引数として、利用することで、
現在日付のファイル名を作成できる。

[wataru@localhost work]$ cat work10.sh 
#!/bin/bash

echo 'Hello'
filename=$(date '+%Y-%m-%d')
touch "$filename"

[wataru@localhost work]$ ls -l
total 48
-rw-rw-r--. 1 wataru wataru     0 Aug 22 23:06 2020-08-22

シェルに引数を与えて、それをシェル内で使用する

[wataru@localhost work]$ cat -n work10.sh 
     1  #!/bin/bash
     2  
     3  echo "My name is $1"
     4  echo "came from $2"

#引数として、Tanaka とJapanを与えている
[wataru@localhost work]$ ./work10.sh Tanaka Japan
My name is Tanaka
came from Japan

if

条件分岐

構文

if <コマンド1>; then
<コマンド1>が真の場合
elif <コマンド2>; then
  <コマンド2>が真の場合
else 上記のコマンドがすべて偽の場合
fi

[wataru@localhost work]$ cat work11.sh 
#!/bin/bash

if [ "$1" = "hello" ]; then
    echo "OK"
else
    echo "NO"
fi

[wataru@localhost work]$ ./work11.sh hello
OK
[wataru@localhost work]$ ./work11.sh hel
NO
[wataru@localhost work]$ cat work11.sh 
#!/bin/bash

if [ "$1" = "$2" ]; then
    echo "true"
#-n str1 :str1が空文字でない
elif [ -n "$2" ]; then
    echo "notBlank"
#-z str1 :str1が空文字である
elif [ -z "$2" ]; then
    echo "blank"
fi

[wataru@localhost work]$ ./work11.sh 20 20
true
[wataru@localhost work]$ ./work11.sh 20 
blank
[wataru@localhost work]$ ./work11.sh 20 500
notBlank
[wataru@localhost work]$ cat work11.sh 
#!/bin/bash

num1=$1
num2=$2

#int1 -eq int2: int1とint2が等しい
if [ "$num1" -eq "$num2" ]; then
    echo "match"
#int1 -lt int2: int1がint2よりも小さい 
elif [ "$num1" -lt "$num2" ]; then
    echo large="$num2"
#int1 -eq int2: int1がint2よりも大きい
elif [ "$num1" -gt "$num2" ]; then
    echo large="$num1"
fi


[wataru@localhost work]$ ./work11.sh 100 100
match
[wataru@localhost work]$ ./work11.sh 100 200
large=200
[wataru@localhost work]$ ./work11.sh 500 499
large=500

#これらの演算子は整数しか扱えない
[wataru@localhost work]$ ./work11.sh 2.2 2.2
./work11.sh: line 6: [: 2.2: integer expression expected
./work11.sh: line 8: [: 2.2: integer expression expected
./work11.sh: line 10: [: 2.2: integer expression expected
[wataru@localhost work]$ cat work99.sh 
#!/bin/bash

dir=$1

#-d file: fileが存在し、ディレクトリである
if [ -d "$dir" ]; then
    echo "dir Existence"
#-f file: fileが存在し、通常のファイルである
elif [ -f "$dir" ]; then
    echo "file Existence"
fi

[wataru@localhost work]$ ./work99.sh /home/wataru/tmp/work/work.02.txt 
file Existence
[wataru@localhost work]$ ./work99.sh /home/wataru/tmp/work
dir Existence

for

繰り返し処理

[wataru@localhost work]$ cat fortest.sh 
#!/bin/bash

#
for number in $(seq 1 5) 
do
    touch "for${number}.txt"
done

[wataru@localhost work]$ ./fortest.sh 
[wataru@localhost work]$ ls -l for*
#for1~5.txtが作成されていることを確認。
-rw-rw-r--. 1 wataru wataru  0 Sep 11 05:53 for1.txt
-rw-rw-r--. 1 wataru wataru  0 Sep 11 05:53 for2.txt
-rw-rw-r--. 1 wataru wataru  0 Sep 11 05:53 for3.txt
-rw-rw-r--. 1 wataru wataru  0 Sep 11 05:53 for4.txt
-rw-rw-r--. 1 wataru wataru  0 Sep 11 05:53 for5.txt
-rwxrwxrwx. 1 wataru wataru 73 Sep 11 05:51 fortest.sh
[wataru@localhost work]$ cat 20200911.sh 
#!/bin/bash

# $@を指定して、すべてのコマンドライン引数を表示
for file in "$@"
do
    echo $file
done

[wataru@localhost work]$ ./20200911.sh abc efg file2020.txt
abc
efg
file2020.txt

case

指定された文字列がパターンにマッチするかどうか判断し、
マッチしたパターンに対応する処理を実行する。

[wataru@localhost work]$ cat work.09.sh
#!/bin/bash

#引数がtxtファイルなら、catコマンドを実行する。
#引数がshファイルなら、viコマンドを実行する。
#それ以外なら、メッセージ出力する。
case "$1" in
    *.txt)
    cat "$1"
    ;;
        *.sh)
    vi "$"
    ;;
        *)
       echo Not txt,sh 
       ;;
esac

[wataru@localhost work]$ ./work.09.sh work.02.txt
2020/07/17
test?cat
123445
TEST?CAT
test
testcat
tfst
test01
test02
test03
test04

[wataru@localhost work]$ ./work.09.sh work
Not txt,sh

while

指定した条件が真である限り、繰り返し処理を実行する。

[wataru@localhost work]$ cat while_test.sh
#!/bin/bash

#シェル変数が10以下である限り、iの値を表示して2を足す処理をする
i=1
while [ "$i" -le 10 ]
do
    echo "$i"
    i=$((i+2))
done

[wataru@localhost work]$ ./while_test.sh
1
3
5
7
9
0
1
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
0
1