VPSで簡単なシェルスクリプトを書く方法(パート3)
序章
条件付きステートメントは、有用なスクリプトおよびフル機能のプログラミング言語に不可欠です。 シェルスクリプトチュートリアルの第3部では、bashのすべての「if」と「else」、およびそれらを有利に使用する方法を学習します。 この記事は、最初のチュートリアルに従ってシェルスクリプトフォルダをすでに構成していることを前提に書かれています。
「if」コマンド
条件ステートメントは、条件がtrueまたはfalseの場合に特定のアクションを実行するために使用されます。 シェルスクリプトでは、これは「if」コマンドによって実行されます。 その後に、テストされる式が続きます。 この式は、他のさまざまなものを超えて、コマンドの実行の終了コード、数式にすることもできます。 終了コードを操作する場合、コマンドは非常に簡単です。
if ls folder then echo "Folder exists" fi
フォルダが存在する場合、成功した場合と同様にlsが終了コード0を返すため、echoコマンドが実行されます。 そうしないと、フォルダが存在しない場合、テキストは表示されません。 すべての「if」ステートメントの後に「then」コマンドを続ける必要があり、「fi」で終了します。 終了コードを使用しておらず、たとえば数式をテストする場合は、「test」コマンドが必要になります。 数値を比較するためのシェルスクリプトには、次の演算子があります。
-eq or Is equal to -ne or Is not equal to -lt or Is less than -le or Is less than or equal to -gt or Is greater than -ge or Is greater than or equal to
テストコマンドは、次の2つの方法で記述できます。
if test 4 -gt 3 or if [ 4 -gt 3]
どちらもまったく同じように動作し、「then」と「fi」も必要です。 例:
if [ 20 -lt 10 ] then echo "What?" fi
“何?” 20は10より大きいため、表示されることはありません。 では、ifステートメントがfalseを返した場合に、ユーザーにメッセージを表示したい場合はどうでしょうか。
「else」コマンド
「Else」は、その名前が示すように、ifコマンドの代替ルートを追加します。 それはかなり基本的です:
if [ 20 -lt 10 ] then echo "What?" else echo "No, 20 is greater than 10." fi
数式に加えて、文字列をif/elseと比較することもできます。 それらは少し異なる構文を必要としますが、それでもテストまたは「[]」コマンドを使用します。 構文は次のとおりです。
string = string or string equals string string != string or string does not equal string string or string is not null or not defined -n string or string is not null and exists -z string or string is null and exists
ファイルのプロパティを知る方法もあります。
-s file tests for a file that is not empty -f file tests if the file exists and is not a folder -d folder tests if it's a folder and not a file -w file tests if the file is writable -r file tests if the file is read-only -x file tests if the file is executable
ネストされた「if」
「if」ステートメント全体を他のステートメント内に配置して、「ネストされたif」と呼ばれるものを作成することもできます。 次の例では、前のチュートリアルで学習したreadによって提供されるユーザー入力の助けを借りてこれを学習します。
#!/bin/bash echo "Input which file you want created" read file if [ -f $file ] then echo "The file already exists" else touch $file if [ -w $file ] then echo "The file was created and is writable" else echo "The file was created but isn't writable" fi fi
スクリプト例
この例では、学習した内容を使用してファイルバックアップスクリプトの改善を試み続けます。 このバージョンには、バックアップフォルダーが存在するかどうか、または存在しないかどうか、作成する権限があるかどうかを確認するためのテストが含まれています。 まず、いつものように、スクリプトを作成します。
touch ~/bin/filebackup3 chmod +x ~/bin/filebackup3 nano ~/bin/filebackup3
そしてそれを編集します:
#!/bin/bash #Backup script 3.0 #Description: makes a copy of any given file at the backup folder #Author: Your Name #Date: 9/29/2013 #Request the backup folder from the user: echo -e "\e[47m\e[1m\e[32mFile Backup Utility\n\e[39m\e[0m\e[47mPlease input your backup folder:" read BACKUPFOLDER #The script will make sure the folder exists if [ -d $BACKUPFOLDER ] then echo "You backup folder exists and will be used." else mkdir $BACKUPFOLDER if [ -d $BACKUPFOLDER ] then echo "Backup folder created successfully." else echo -e "I do not have the rights to create your backup folder.\nThis script will now exit." exit 1 #exit 1 is a command that exits the script with an error code fi fi #Request files to be backed up: echo -e "\e[30mWhich files do you want backed up?\e[39m\e[49m" read FILES if [ -n $FILES ] then cp -a $FILES $BACKUPFOLDER else echo "File does not exist." fi
スクリプトは、入力したバックアップフォルダーが存在しない場合、作成する場合、作成できない場合、およびファイルの要求時にnull文字列を指定した場合に通知します。
結論
学べば学ぶほど、新しい革新的なソリューションを作成するための幅広い可能性とともに、より優れたプログラムを作成できます。 このチュートリアルでは、すでにユーザーフレンドリーなスクリプトがさらに使いやすくなりました。