bashのシェルスクリプト実行方法

Linuxの実行可能ファイル、実行権限、bashスクリプトの作成、実行方法などについてまとめました。

※目次をクリックすると目次の下部にコンテンツが表示されます。

実行可能ファイル
●実行可能ファイル
 
・実行可能ファイルには、バイナリ形式のプログラムやコマンドとbashなどのシェルを使ったテキスト形式のスクリプトがある。
 
・fileコマンドで、そのファイルがバイナリであるかテキスト形式であるか確認することができる。
 
●バイナリ形式
 
・Linuxの場合は多くの場合、ELF(Executable and Linking Format)。
・ELFとは、コンパイラが生成するオブジェクト、および、ライブラリとリンクされた実行ファイルのファイルフォーマット。
 
例)fileコマンドの出力例

$ file /bin/ls
/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped

 
●テキスト形式
 
・Bashなどのシェルのスクリプト。
・実行権限が付与されている。
・1行目に、#!/bin/sh のようにファイルを実行するのにプログラムが指定されている。
 
例)fileコマンドの出力例

$ file /etc/rc.d/init.d/mysqld
/etc/rc.d/init.d/mysqld: POSIX shell script text executable

bashスクリプトの実行方法
ここでは、下記サンプルスクリプトを例とする。
(hello.sh)
#!/bin/bash
echo hello!
 

(1)ドットコマンドまたはsourceコマンドを使用


 
・カレントシェルからファイルを読み込みファイル内のコマンドを実行する。
・スクリプトの中のコマンドは呼び出したシェルと同じシェルでコマンドを実行するので、そこで環境変数を変更して利用することができる。
 
実行例)
$ . hello.sh
hello!
$ source hello.sh
hello!
 

(2)sh、bashを使って実行


 
・スクリプトファイルの名前を指定してシェルを呼び出す。
 
実行例)
$ sh hello.sh
hello!
$ bash hello.sh
hello!
 

(3)スクリプト名を入力して実行


 
・通常のコマンドのようにファイル名を入力するのみで実行することもできる。
・この場合は、実行するファイルに実行権限を付与する必要がある。
・$PATH環境変数にカレントディレクトリのPATHがある場合はファイル名のみでも実行できるが、無い場合はフルパスを指定する必要がある。
・この場合は、サブプロセスとしてサブシェルと呼ばれるシェルのコピーが起動され、サブシェルはスクリプトからコマンドを取り出し、実行・終了させたのち、制御を親シェルに戻す。
 
①実行権限がない場合の実効例

$ pwd .
/home/unyouser
$ ./hello.sh
-bash: ./hello.sh: Permission denied

※ドットはカレントディレクトリをあらわすので上記はドットを使ってフルパスを指定している。
 
②実行権限を付与して実行

$ chmod 764 hello.sh

$ ./hello.sh
hello!

$ /home/unyouser/hello.sh
hello!

スクリプトの先頭の#!/bin/bash、#!/bin/shについて
1)使用例
 
システムの起動に関わるシェルスクリプト”/etc/rc.d/rc.sysinit”、”/etc/rc.d/rc.local”を見てみます。
 
①/etc/rc.d/rc.sysinitの場合
#!/bin/bash
 
②/etc/rc.d/rc.localの場合
#!/bin/sh
 
2)#!/bin/bashと#!/bin/shの違い
 
下記のように/bin/shは/bin/bashへのリンクとなっているので全く同じように思えます。
 
$ ls -al /bin/sh
lrwxrwxrwx 1 root root 4 Aug 14 2013 /bin/sh -> bash
 
bashのmanを見るとこの違いについて下記記載があります。
 
If bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well. When invoked as an interac-tive login shell, or a non-interactive shell with the –login option,it first attempts to read and execute commands from /etc/profile and ~/.profile, in that order. The –noprofile option may be used to inhibit this behavior. When invoked as an interactive shell with the name sh, bash looks for the variable ENV, expands its value if it is defined, and uses the expanded value as the name of a file to read and execute. Since a shell invoked as sh does not attempt to read and exe-cute commands from any other startup files, the –rcfile option has no effect. A non-interactive shell invoked with the name sh does not attempt to read any other startup files. When invoked as sh, bash enters posix mode after the startup files are read.
 
bashでは、下記のようにposixのオプションがoffになっていますが、shで実行するとonになります。
 
①bashで実行する場合
 
(スクリプトの中身)
#!/bin/bash
set -o | grep posix
(上記スクリプト実行)
$ ./hello.sh
posix off
 
②shで実行する場合
 
(スクリプトの中身)
#!/bin/sh
set -o | grep posix
(上記スクリプト実行)
$ ./hello.sh
posix on
 
(スクリプトの中身)
#!/bin/bash
set -o | grep posix
(上記スクリプトをshで実行)
$ sh hello.sh
posix on

関連記事の目次

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください