クリップボードにコピーしました!
文字列のシェルスクリプト

[シェルスクリプト] 実行して理解 未実装のif文で動作

created-2026/01/07 updated-2026/03/15

前書き

このリポジトリは、シェルスクリプトを実行して動作確認をするために処理が未実装のif文を実行できるようにします

インストールする必要のあるコマンド

  1. git

クイックスタート

上記のコマンドをインストール済みの方は、以下のコマンドを実行してリポジトリからダウンロード後、ディレクトリを移動し、シェルスクリプトを実行してエラー無く実行できることを確認してください

ubuntu

git clone https://github.com/trygfmi/shellscript_if-nothing
cd shellscript_if-nothing
cat start_if-nothing.sh
chmod 744 start_if-nothing.sh
./start_if-nothing.sh
sed -i '7i\
:\
' start_if-nothing.sh
./start_if-nothing.sh
出力結果
./start_if-nothing.sh: line 7: syntax error near unexpected token `elif'
./start_if-nothing.sh: line 7: `elif [ $i == 1 ]; then'

no error

macos

MacPorts

source ~/bashrc_folder/macports_alias
git clone https://github.com/trygfmi/shellscript_if-nothing
cd shellscript_if-nothing
cat start_if-nothing.sh
chmod 744 start_if-nothing.sh
./start_if-nothing.sh
sed -i '' '7i\
:\
' start_if-nothing.sh
./start_if-nothing.sh
出力結果
./start_if-nothing.sh: line 7: syntax error near unexpected token `elif'
./start_if-nothing.sh: line 7: `elif [ $i == 1 ]; then'

no error

windows

WSL2

git clone https://github.com/trygfmi/shellscript_if-nothing
cd shellscript_if-nothing
cat start_if-nothing.sh
chmod 744 start_if-nothing.sh
./start_if-nothing.sh
sed -i '7i\
:\
' start_if-nothing.sh
./start_if-nothing.sh
出力結果
./start_if-nothing.sh: line 7: syntax error near unexpected token `elif'
./start_if-nothing.sh: line 7: `elif [ $i == 1 ]; then'

no error

MSYS2 MINGW64

git clone https://github.com/trygfmi/shellscript_if-nothing
cd shellscript_if-nothing
cat start_if-nothing.sh
chmod 744 start_if-nothing.sh
./start_if-nothing.sh
sed -i '7i\
:\
' start_if-nothing.sh
./start_if-nothing.sh
出力結果
./start_if-nothing.sh: line 7: syntax error near unexpected token `elif'
./start_if-nothing.sh: line 7: `elif [ $i == 1 ]; then'

no error

実行手順

ubuntu

クリックして詳細を開く

事前確認

以下のコマンドを端末に打ち込んでcommand not foundが出なければokです

git --version

preinstall

端末にcommand not foundが出たコマンドを以下のコマンドでインストールしてください

sudo apt install git

コマンド

以下のコマンドを端末で実行することで詳細のような文字列が出力されるはずです

git clone https://github.com/trygfmi/shellscript_if-nothing
cd shellscript_if-nothing
cat start_if-nothing.sh
chmod 744 start_if-nothing.sh
./start_if-nothing.sh
sed -i '7i\
:\
' start_if-nothing.sh
./start_if-nothing.sh
詳細
./start_if-nothing.sh: line 7: syntax error near unexpected token `elif'
./start_if-nothing.sh: line 7: `elif [ $i == 1 ]; then'

no error

macos

クリックして詳細を開く

事前確認

以下のコマンドをターミナルに打ち込んでcommand not foundが出なければokです

source ~/bashrc_folder/macports_alias
git --version

※macosはMacPortsパッケージマネージャを使用してコマンドを管理します。もしインストールしていない方は以下のリンクからMacPortsのインストール手順をご覧ください
またコマンドに別名を設定して既存の環境と競合しないでコマンドを呼び出せるようにします。
初めてこのブログを利用する方は、以下の2つの記事を参考に環境構築してください

preinstall

ターミナルでcommand not foundが出たコマンドを以下のコマンドでインストールしてエイリアスを設定してください

sudo port install git
echo 'alias git="/opt/local/bin/git"' >> ~/bashrc_folder/macports_alias

コマンド

以下のコマンドをターミナルで実行することで詳細のような文字列が出力されるはずです

source ~/bashrc_folder/macports_alias
git clone https://github.com/trygfmi/shellscript_if-nothing
cd shellscript_if-nothing
cat start_if-nothing.sh
chmod 744 start_if-nothing.sh
./start_if-nothing.sh
sed -i '' '7i\
:\
' start_if-nothing.sh
./start_if-nothing.sh
詳細
./start_if-nothing.sh: line 7: syntax error near unexpected token `elif'
./start_if-nothing.sh: line 7: `elif [ $i == 1 ]; then'

no error

windows

クリックして詳細を開く

事前確認

以下のコマンドをプロンプトに打ち込んでcommand not foundが出なければokです

WSL2
git --version
MSYS2 MINGW64
git --version

※windowsはWSL2とMSYS2 MINGW64で確認しています。可能な限りWSL2をインストールしていただいて、もし設定できなかった場合はMSYS2をインストールすることで実行できますが、所々WSL2でしか実行できないコマンドが出てくるかもしれません。WSL2とMSYS2のインストール方法は下記の記事を参考にしてください

preinstall

プロンプトでcommand not foundが出たコマンドを以下のコマンドでインストールしてください

WSL2
sudo apt install git
MSYS2 MINGW64
pacman --sync git

コマンド

以下のコマンドをプロンプトで実行することで詳細のような文字列が出力されるはずです

WSL2
git clone https://github.com/trygfmi/shellscript_if-nothing
cd shellscript_if-nothing
cat start_if-nothing.sh
chmod 744 start_if-nothing.sh
./start_if-nothing.sh
sed -i '7i\
:\
' start_if-nothing.sh
./start_if-nothing.sh
詳細
./start_if-nothing.sh: line 7: syntax error near unexpected token `elif'
./start_if-nothing.sh: line 7: `elif [ $i == 1 ]; then'

no error
MSYS2 MINGW64
git clone https://github.com/trygfmi/shellscript_if-nothing
cd shellscript_if-nothing
cat start_if-nothing.sh
chmod 744 start_if-nothing.sh
./start_if-nothing.sh
sed -i '7i\
:\
' start_if-nothing.sh
./start_if-nothing.sh
詳細
./start_if-nothing.sh: line 7: syntax error near unexpected token `elif'
./start_if-nothing.sh: line 7: `elif [ $i == 1 ]; then'

no error

後書き

条件分岐が動作するかどうかテストしたい時に:(コロン)を条件分岐内に記述することでエラーを回避できます
以下のコマンドを使用してコメントアウトを削除したり追加したりして動作確認できます

sed -i '6 s/# //' start_if-nothing.sh #ubuntu, windows
./start_if-nothing.sh
sed -i '6 s/echo/# echo/' start_if-nothing.sh #ubuntu, windows
sed -i '' '6 s/# //' start_if-nothing.sh #macos
./start_if-nothing.sh
sed -i '' '6 s/echo/# echo/' start_if-nothing.sh #macos

コメントを残す

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

©︎ 2025-2026 todo