0
0

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 3 years have passed since last update.

シェルスクリプトの作成と実行方法

0
Last updated at Posted at 2022-03-14

シェルスクリプトの基本的な書き方と実行方法をメモしておく。

シェルスクリプトとは

シェルで解釈・実行される一連の処理(コマンド)を記述したスクリプトのこと。
シェルスクリプトで複雑な処理や繰り返し処理などをスクリプトとして書いておくことで、効率的にLinuxやUnixの操作を効率的に行うことができる。一般的に拡張子は、.shを指定する。
今回はbashでのシェルスクリプトの作成と実行に関して基本的な部分をメモしておく。

基本的な書き方

bashのシェルスクリプトで簡単なサンプルとして、「Hello!」を出力させるものを書くと以下となる。

hello.sh
#!/usr/bin/bash
# bashを使用したシェルスクリプトであることを宣言

echo "Hello!"

ファイルを新規作成する際はviコマンドを使ってviエディタを開くことができる。

$ vi hello.sh

今回はほんとに基本的な部分だけたが、シェルスクリプトでは変数やfor文、if文なども記述することができる。

実行方法

以下のコマンドで実行することができる。

$ bash hello.sh
$ ./hello.sh
実行結果
Hello!

もし、上記で実行した場合にファイルの実行権限がないというエラーが出た場合は、以下のコマンドで実行権限を与えることができる。

$ chmod 755 hello.sh
0
0
1

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?