Shell betiği, bir dizi komutun birleşiminden ibarettir. Diyelim ki bir tekrarlayan görev için her seferinde birden fazla komut girmeniz gerekiyor. Bu görev her tekrarlandığında komutları tek tek girmek zaman alıcı olabilir. Bunun yerine, komutları bir betikte birleştirerek zaman kazanabilirsiniz. Betiği çalıştırdığınızda, tüm komutlar otomatik olarak çalıştırılır. Bu şekilde, aynı komutları her seferinde tekrar yazmak yerine, sadece bir kez betiği çalıştırarak tüm görevleri otomatikleştirebilirsiniz.
Çalışma ortamında kullanılan tüm shell’ler (terminal veya kabuk) betik yazma yeteneğine sahiptir. Betik yazma, görevlerin otomatikleştirilmesine yardımcı olur. Betik yazmayı öğrenmeden önce şunu bilmemiz gerekir: Linux shell’leri betik yazma yeteneğine sahip olsa da, sadece shell kullanarak betik yazmak zorunda değilsiniz. Betikler, çeşitli programlama dillerinde de yazılabilir. Ancak bu açıklamanın kapsamı, shell kullanarak betik yazmaya odaklanmaktadır.
İlk Adım: Terminal Açma ve Shell Seçme
Bash shell, çoğu dağıtımda varsayılan olarak bulunan ve yaygın olarak kullanılan bir shell’dir. Bu nedenle, başlangıçta bash shell kullanacağız.
Shell’de yazacağımız komutlardan farklı olarak, betik için önce bir dosya oluşturmalıyız. Bu dosya, bir metin düzenleyicisiyle oluşturulmalı ve dosya adı .sh uzantısı ile bitmelidir. Bash betikleri için varsayılan uzantıdır.
Betiğin Oluşturulması
Terminalde, şu şekilde bir dosya oluşturabiliriz:
Her betik, shebang adı verilen bir komutla başlar. Shebang, bir dosyanın başlangıcına eklenen ve betiği çalıştırırken kullanılacak yorumlayıcının adı ile başlayan özel bir karakter kombinasyonudur. Bash ile yazdığımız için yorumlayıcı olarak /bin/bash
tanımlanır.
Değişkenler
Değişkenler, içinde bir değer tutar. Örneğin, bir URL veya dosya yolu gibi karmaşık değerleri betiğinizde birden fazla kez kullanmanız gerektiğinde, bunları bir değişkende saklayabilirsiniz. Böylece her seferinde bu değerleri ezbere yazmak yerine, sadece değişken adını kullanabilirsiniz.
Örnek bir betik:
Bu betikte, read
komutu ile kullanıcıdan name
adında bir değişken alınır ve echo
komutuyla kullanıcıya hoş geldin mesajı yazdırılır.
Betiği Çalıştırmak için İzinler Verme
Betiğinizi çalıştırmadan önce, dosyanın yürütme iznine sahip olması gerekir. Aşağıdaki komut ile bu izni verebilirsiniz:
Şimdi betiği çalıştırmak için ./
kullanarak dosyayı şu şekilde çalıştırabilirsiniz:
Döngüler
Döngüler, bir işlemin tekrarıdır. Örneğin, bir arkadaşlar listeniz varsa ve onlara aynı mesajı göndermek istiyorsanız, bu işlemi bir döngü ile otomatikleştirebilirsiniz.
Örnek bir döngü:
Bu betik, 1'den 10'a kadar olan sayıları ekrana yazdırır.
Koşullu İfadeler
Koşullu ifadeler, bir koşul sağlandığında bir kod parçasının çalışmasını sağlar. Aşağıdaki örnek, yalnızca belirli bir kullanıcı adı girildiğinde bir "gizli" mesajı göstermektedir:
Yorumlar
Kod uzun ve karmaşık olduğunda, tekrar baktığınızda veya başkalarına gösterdiğinizde karışıklık yaratabilir. Yorumlar, kodun ne yaptığını açıklamak için kullanılır ve #
ile başlar.
Örnek yorumlu betik:
Yorumlar, betiğin işleyişini değiştirmez, sadece okunabilirliğini artırır.
English Explanation:
A shell script is essentially a series of commands. Suppose you have a repetitive task that requires you to enter several commands in a shell. Instead of entering them one by one each time the task is repeated, which could take more time, you can combine them into a script. By running the script, all the commands will be executed automatically. All the shells mentioned in previous tasks have scripting capabilities. Scripting helps us automate tasks. Before learning how to write a script, we need to understand that while Linux shells have scripting capabilities, this doesn’t mean you can only write scripts using a shell. Scripts can also be written in various programming languages. However, the scope of this explanation focuses on scripting using a shell.
First Step: Open Terminal and Choose a Shell
Bash shell is the default and most widely used shell in many Linux distributions. Therefore, we’ll go with bash in this case.
Unlike commands typed directly in the shell, we first need to create a file using any text editor for our script. This file must end with the .sh
extension, which is the default for bash scripts.
Creating the Script File
In the terminal, you can create a file as follows:
Every script starts with a shebang. A shebang is a special combination of characters added at the beginning of the script, starting with #!
followed by the name of the interpreter (like /bin/bash
) used to run the script.
Variables
A variable stores a value inside it. For example, if you need to use complex values (like a URL or file path) multiple times in your script, instead of remembering and writing them over and over, you can store them in a variable and reference the variable whenever needed.
Example script:
This script uses read
to capture user input in a variable (name
) and echo
to display a welcome message with the user’s name.
Granting Execution Permissions to the Script
Before running the script, it needs execution permissions. Use the following command to grant those permissions:
Now, to execute the script, use ./
before the script name as follows:
Loops
A loop repeats a task multiple times. For example, if you have a list of friends and want to send them the same message, you can automate this using a loop in your script.
Example loop:
This script will print numbers from 1 to 10.
Conditional Statements
Conditional statements allow you to execute a specific block of code only when a condition is true. The following script shows a secret message only for an authorized user.
Comments
Sometimes, code can become long and confusing. Comments are used to clarify what each part of the code does. Comments are added using the #
symbol.
Example of a commented script:
Comments don’t affect the functioning of the script but make it easier to understand and maintain