published on 17.11.2019

Bu yazıda, Bash ile script yazarken döngüleri nasıl kullanabileceğimizi ele alacağız. For, while ve until döngülerini karşılaştıracağız.

  • döngüler, ‘done’ komutu ile bitiyor.
  • if statement’da if-then kullandığımız gibi döngülerde de for-do, while-do, until-do terimlerini kullanıyoruz.

while

#!/bin/bash
i=0

while [ $i -le 10 ]; do
    echo "i = $i"
    ((i++))
done

until

#!/bin/bash
i=0

until [ $i -gt 10 ]; do
    echo "i = $i"
    ((i++))
done

for

for döngüsünün syntaxı biraz daha farklı. while ve until döngülerinde bir counter ilerlerken, for döngüsünde bir array içerisindeki elemanlar için döndürüyoruz.

#!/bin/bash
for i in $(ls); do
    echo "item: $i"
done

Yukarıdaki örneğin aslında pek de bir mantığı yok sadece for loop’u örneklemek için verilmiş. ls komutu çalışıyor ve her bir elementi tek tek yazdırıyor. İçeride bir if statement kullanarak daha mantıklı hale getirilebilir.

#!/bin/bash
for i in 1 2 3; do
    echo "(($i**2))"
done
> 1
> 4
> 9

Array’i 1 2 3 diye belirtmek yerine aşağıdaki gibi brace expansion’dan faydalanabiliriz. 1’den 8’e kadar 2’şer artarak gidecektir.

#!/bin/bash
for i in {1..8..2}; do
    echo "(($i**2))"
done
> 1
> 9
> 25
> 49

Veya bir array içerisinde bu örnekteki gibi de döngü yazabiliriz.

#!/bin/bash
array=("vader" "master yoda" "hello there" "the senate")
for i in ${array[@]}; do
    if [[ $i == "the senate" ]]
    echo "I'm the senate"
done

C’ye benzer dillerden alışkın olduğumuz loop yapısına gelelim.

#!/bin/bash
for (( i=0; i<=10; i++)); do
    echo "$i"
done
You are reading the 7th of 10 episodes in Bash.
Published on 17.11.2019 by Mert Bakır with commit db8d56a.
bash
#bash #linux
Next episode:
Bash: Case Statement
published on 16.11.2019

Eğer test etmek istediğimiz bir durum varsa if statement kullanırız. Birden fazla durum varsa nested if statement kullanabiliriz ama sayı arttıkça if statement karmaşıklaşacaktır. Burada devreye case giriyor. Syntax’ı basit.

published on 19.11.2019

Her dilde kullandığımız, tekrara düşmemek adına kullanılan en temel yapı. Fonksiyonlar, bash scriptinde aşağıdaki örnekteki gibi tanımlanabilir. function anahtar sözcüğü ardından fonksiyon adı verilir ve küme parantezi içerisinde fonksiyon tanımlanır. […] #!/bin/bash function selamVer { echo …

published on 20.11.2019

Fonksiyonlar, bölümünde argümanlardan kısaca bahsetmiştik. Peki, ya argümanı direkt scriptin dışından userdan ya da farklı bir programdan almamız gerekiyorsa ne yapacağız? Yine fonksiyona argüman gönderir gibi scripte argüman gönderebiliriz. […] ./foobar.sh "foo" "bar" # …

published on 10.07.2022

Previously, I’ve published a blog post about deploying static content on heroku with basic authentication. The main purpose was to get basic auth for a freely hosted static website. In that post, we hosted the source code on GitLab and configured a CI/CD pipeline to render the static content …

published on 28.05.2022

Each git commit has a field called Author which consists ‘user.name’ and ‘user.email’. We usually set these variables once, after installing git, with git config --global so that each repo gets the variables from the global definition. We can also set them locally for a …

published on 25.05.2022

In this post, I’ll first walk through hosting static content with basic authentication. Then, we’ll look into deploying to Heroku using GitLab Pipelines, more specifically deploying a certain sub-directory within the project instead of pushing the whole project. Also, I’ll share …

published on 17.04.2022
edited on 15.07.2022

Önceki bölümde, markdown formatını LaTeX formatına dönüştürmek için kullanılan Pandoc yazılımından bahsetmiştik. Şimdi konuyu bir adım daha ileri taşıyıp ve bookdown’a geçiyoruz. Bookdown; Rmarkdown kullanarak teknik dökümanlar, kitaplar yazabilmemizi sağlayan, Yihui Xie tarafından yazılmış …

published on 10.04.2022

I’ve been using WSL-2 on Windows for over a year. It’s very useful because some Python packages are just a headache to install on Windows. Also, docker. It’s just better on Linux. Yet, WSL-2 can also be problematic. I remember trying a dual-boot setup when things just went way too …

published on 03.03.2022

In this post, I’ll share how to install geopandas and some other gis related packages on Windows. If you are on Mac or Linux you can probably just pip install those without any issue. I usually had to do a google search every time I wanted to install these packages on Windows environment. Of …