Kimama-IT

ITに関する覚書き

【Ubuntu】【ShellScript】オリジナルコマンドを作ってみる。

Ubuntu】ホームディレクトリにcommandディレクトリを作成し、その中にオリジナルのhhコマンドを作成します。

今回の環境を記載

~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.2 LTS
Release: 22.04
Codename: jammy


ホームディレクトリに移動

~$ cd ~


commandディレクトリが無いことを確認

~$ ls -ld command
ls: cannot access 'command': No such file or directory

→No such file or directoryなどと表示されればディレクトリは無いです。


オリジナルコマンドを入れるディレクトリを作成

~$ mkdir command


commandディレクトリが作成されたことを確認

~$ ls -ld command
drwxrwxr-x 2 kimama kimama 4096  6月 25 16:14 command


commandディレクトリに移動

~$ cd command


hhファイル(オリジナルコマンド用のファイル)がないことを確認

~/command$ ls -l hh
ls: cannot access 'hh': No such file or directory


hhファイルを作成

~/command$ touch hh


hhファイルが作成されたことを確認

~/command$ ls -l hh
-rw-rw-r-- 1 kimama kimama 0  6月 25 16:23 hh


hhファイルにユーザの実行権を付与

~/command$ chmod u+x hh


hhファイルにユーザの実行権が付与されたことを確認

~/command$ ls -l hh
-rwxrw-r-- 1 kimama kimama 0  6月 25 16:23 hh


hhファイルの内容を編集してshellコマンドを記載(vi hh でもよい)

~/command$ nano hh

#!/usr/bin/bash
echo "hello $1"

を記載する


hhファイルの内容を表示

~/command$ cat hh
#!/usr/bin/bash
echo "hello $1"


hhファイルを実行してみる

~/command$ ./hh
hello


hhファイルを引数付きで実行してみる

~/command$ ./hh world
hello world


hhというコマンドを実行してみる

~/command$ hh
hh: command not found

→command not foundなどと表示され実行できない


~/commandディレクトリにPATHを通してhhコマンドを実行できるようにする

ホームディレクトリに移動

~/command$ cd ~


ホームディレクトリのファイルを表示

~$ ls -a1
.
..
.bash_logout
.bashrc
.cache
.config
.local
.profile
.sudo_as_admin_successful
Desktop
Documents
Downloads
Music
Pictures
Public
Templates
Videos
command
snap

→.bash_profileは無いですね。名前の似ている.profileはなんでしょうね。


.profileの内容を見てみる

~$ cat .profile
# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
 . "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi

→先頭行に「~/.profile: login shellsのコマンド インタプリタによって実行されます。」と表示があります。
 .profileはログインしたときに実行されるようですね。
 「~/.bash_profile または ~/.bash_login が存在する場合、このファイルは bash(1) によって読み取られません。」と書いてありますね
 そして、$HOME/.bashrc、$HOME/bin、$HOME/.local/binが存在すればそれらをPATHに追加するようなコマンドがあります。
 .profileにcommandディレクトリのPATHを追加するコマンドを追記すればPATHを通すことができそうです。


.profileのバックアップファイルが無いことを確認

~$ ls -a .profile.backup
ls: cannot access '.profile.backup': No such file or directory


.profileのバックアップファイルを作成

~$ cp -p .profile .profile.backup


.profileのバックアップファイルが作成されたことを確認

~$ ls -a .profile.backup
.profile.backup


.profileを編集して、commandディレクトリへのPATHを通す(vi .profile でもよい)

~$ nano .profile

最後尾に以下を追記

# set PATH so it includes user's private command if it exists
if [ -d "$HOME/command" ] ; then
    PATH="$HOME/command:$PATH"
fi


.profileの内容を表示して上記が追記されたことを確認

~$ cat .profile
# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
 . "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi

# set PATH so it includes user's private command if it exists
if [ -d "$HOME/command" ] ; then
    PATH="$HOME/command:$PATH"
fi


hhコマンドを実行してみる

~$ hh
hh: command not found

→コマンドが見つからないと表示されてますね。
 .profileがまだ実行されていないことが原因のようです。
 .profileはログイン時に自動で実行されるようなので、ログインしなおしてみます。


いったんログアウトする

~$ exit
logout


再度ログインする。

→ログインの方法はsshtelnet、コンソールなど任意で行ってください。


hhコマンドを実行してみる。

~$ hh
hello

→hhコマンドが実行できました。

 

hhコマンドを引数付きで実行してみる

~$ hh world
hello world

→引数付きでも実行できました。


以上となります。