{"id":1978,"date":"2022-10-22T07:47:28","date_gmt":"2022-10-22T07:47:28","guid":{"rendered":"https:\/\/sunucucozumleri.com\/?p=1978"},"modified":"2022-10-19T21:34:10","modified_gmt":"2022-10-19T21:34:10","slug":"bash-komut-dosyasinda-kosullu-ifadeler-nasil-kullanilir","status":"publish","type":"post","link":"https:\/\/sunucucozumleri.com\/blog\/bash-komut-dosyasinda-kosullu-ifadeler-nasil-kullanilir\/","title":{"rendered":"Bash Komut Dosyas\u0131nda Ko\u015fullu \u0130fadeler Nas\u0131l Kullan\u0131l\u0131r"},"content":{"rendered":"<p><span>Bu k\u0131lavuzda, bash beti\u011finde if, if-else, If-elif-else, netsted if ve case gibi ko\u015fullu ifadelerin nas\u0131l kullan\u0131laca\u011f\u0131n\u0131 \u00f6\u011frenece\u011fiz.\u00a0Ko\u015fullu ifadeler, ko\u015fula g\u00f6re karar vermeye yard\u0131mc\u0131 oldu\u011fu i\u00e7in bash beti\u011finde \u00f6nemli bir rol oynar.<\/span><\/p>\n<p><span>Bir bash beti\u011finde,\u00a0<\/span><span>if ifadesi<\/span><span>\u00a0bir ko\u015fulun do\u011fru olup olmad\u0131\u011f\u0131n\u0131 kontrol eder.\u00a0E\u011fer \u00f6yleyse, kabuk if ifadesiyle ili\u015fkili kod blo\u011funu y\u00fcr\u00fct\u00fcr.\u00a0\u0130fade true de\u011filse, kabuk if ifade blo\u011funun sonunun \u00f6tesine atlar ve Devam eder.<\/span><\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Makale \u0130\u00e7eri\u011fi<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"\u0130\u00e7indekiler Tablosunu A\u00e7\/Kapat\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/sunucucozumleri.com\/blog\/bash-komut-dosyasinda-kosullu-ifadeler-nasil-kullanilir\/#if_Bildirimi\" >if Bildirimi\u00a0<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/sunucucozumleri.com\/blog\/bash-komut-dosyasinda-kosullu-ifadeler-nasil-kullanilir\/#if-else_Ifadesi\" >if-else \u0130fadesi<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/sunucucozumleri.com\/blog\/bash-komut-dosyasinda-kosullu-ifadeler-nasil-kullanilir\/#If-elif-else_Ifadesi\" >If-elif-else \u0130fadesi<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/sunucucozumleri.com\/blog\/bash-komut-dosyasinda-kosullu-ifadeler-nasil-kullanilir\/#Ic_ice_if_Ifadesi\" >\u0130\u00e7 i\u00e7e if \u0130fadesi<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/sunucucozumleri.com\/blog\/bash-komut-dosyasinda-kosullu-ifadeler-nasil-kullanilir\/#Vaka_Aciklamasi\" >Vaka A\u00e7\u0131klamas\u0131<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"if_Bildirimi\"><\/span><span>if Bildirimi\u00a0<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span>S\u00f6zdizimi:\u00a0<\/span><span id=\"ezoic-pub-ad-placeholder-166\" class=\"ezoic-adpicker-ad\"><\/span><\/p>\n<div class=\"iwOYPunV\"><\/div>\n<p>if [ condition_command ]<br \/>\nthen<br \/>\ncommand1<br \/>\ncommand2<br \/>\n\u2026\u2026..<br \/>\nlast_command<br \/>\nfi<\/p>\n<p><span>A\u015fa\u011f\u0131daki bash beti\u011fi \u00f6rne\u011finde, if ko\u015fulu deyimini kullanarak iki say\u0131y\u0131 kar\u015f\u0131la\u015ft\u0131r\u0131yoruz.<\/span><\/p>\n<pre>#!\/bin\/bash\r\necho \"Enter the Number: \"\r\nread n\r\nif [ $n -lt 150 ]\r\nthen\r\necho \"Number is $n\"\r\nfi<\/pre>\n<p><span>Bu beti\u011fi \u00e7al\u0131\u015ft\u0131rd\u0131\u011f\u0131m\u0131zda e\u011fer 150&#8217;den k\u00fc\u00e7\u00fckse say\u0131y\u0131 yazd\u0131racakt\u0131r.<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre><img decoding=\"async\" class=\"alignnone size-full wp-image-1979\" src=\"https:\/\/sunucucozumleri.com\/wp-content\/uploads\/2022\/10\/Bash-Komut-Dosyasinda-Kosullu-Ifadeler-Nasil-Kullanilir.webp\" alt=\"\" width=\"768\" height=\"258\" title=\"\" srcset=\"\/\/sunucucozumleri.com\/blog\/wp-content\/uploads\/2022\/10\/Bash-Komut-Dosyasinda-Kosullu-Ifadeler-Nasil-Kullanilir.webp 768w, \/\/sunucucozumleri.com\/blog\/wp-content\/uploads\/2022\/10\/Bash-Komut-Dosyasinda-Kosullu-Ifadeler-Nasil-Kullanilir-300x101.webp 300w\" sizes=\"(max-width: 768px) 100vw, 768px\" \/>\r\n\r\n\r\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"if-else_Ifadesi\"><\/span><span>if-else \u0130fadesi<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span>Normal if deyimine ek olarak, if deyimini bir else blo\u011fu ile geni\u015fletebiliriz.\u00a0Temel fikir, e\u011fer ifade true ise if blo\u011funu y\u00fcr\u00fctmektir.\u00a0\u0130fade yanl\u0131\u015fsa, else blo\u011funu y\u00fcr\u00fct\u00fcn.<\/span><\/p>\n<p>Syntax :<\/p>\n<p>if [ condition_command ]<br \/>\nthen<br \/>\ncommand1<br \/>\ncommand2<br \/>\n\u2026\u2026..<br \/>\nlast_command<br \/>\nelse<br \/>\ncommand1<br \/>\ncommand2<br \/>\n\u2026\u2026..<br \/>\nlast_command<br \/>\nfi<\/p>\n<p>If we modify the above script and insert else block then it would look like below,<\/p>\n<pre>#!\/bin\/bash\r\necho \"Enter the Number: \"\r\nread n\r\nif [ $n -gt 150 ]\r\nthen\r\n    echo \"Number $n is greater than 150\"\r\nelse\r\n    echo \"Number $n is smaller than 150\"\r\nfi\r\n\r\n<\/pre>\n<p><span>Komut dosyas\u0131n\u0131 \u00e7al\u0131\u015ft\u0131r\u0131p 129 say\u0131s\u0131n\u0131 girersek, a\u015fa\u011f\u0131da g\u00f6sterildi\u011fi gibi else blo\u011funu \u00e7al\u0131\u015ft\u0131racakt\u0131r,<\/span><\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-1980\" src=\"https:\/\/sunucucozumleri.com\/wp-content\/uploads\/2022\/10\/Bash-Komut-Dosyasinda-Kosullu-Ifadeler-Nasil-Kullanilir2.webp\" alt=\"\" width=\"768\" height=\"322\" title=\"\" srcset=\"\/\/sunucucozumleri.com\/blog\/wp-content\/uploads\/2022\/10\/Bash-Komut-Dosyasinda-Kosullu-Ifadeler-Nasil-Kullanilir2.webp 768w, \/\/sunucucozumleri.com\/blog\/wp-content\/uploads\/2022\/10\/Bash-Komut-Dosyasinda-Kosullu-Ifadeler-Nasil-Kullanilir2-300x126.webp 300w\" sizes=\"(max-width: 768px) 100vw, 768px\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"If-elif-else_Ifadesi\"><\/span><span>If-elif-else \u0130fadesi<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span>Bash beti\u011finde, if deyimini kullanarak birden \u00e7ok ko\u015ful uygulamak istiyorsan\u0131z, &#8216;if elif else&#8217; kullan\u0131n.\u00a0Bu t\u00fcr ko\u015fullu deyimlerde, ilk ko\u015ful sa\u011flan\u0131rsa a\u015fa\u011f\u0131daki kod \u00e7al\u0131\u015ft\u0131r\u0131l\u0131r, aksi takdirde sonraki ko\u015ful kontrol edilir ve e\u015fle\u015fmezse a\u015fa\u011f\u0131daki komutlar y\u00fcr\u00fct\u00fcl\u00fcr, aksi takdirde deyim y\u00fcr\u00fct\u00fcl\u00fcr.\u00a0S\u00f6zdizimi ve \u00f6rne\u011fi a\u015fa\u011f\u0131da g\u00f6sterilmi\u015ftir.<\/span><\/p>\n<p><span>s\u00f6zdizimi:<\/span><\/p>\n<p>if [ condition_command ]<br \/>\nthen<br \/>\ncommand1<br \/>\ncommand2<br \/>\n\u2026\u2026..<br \/>\nlast_command<br \/>\nelif [ condition_command2 ]<br \/>\nthen<br \/>\ncommand1<br \/>\ncommand2<br \/>\n\u2026\u2026..<br \/>\nlast_command<br \/>\nelse<br \/>\ncommand1<br \/>\ncommand2<br \/>\n\u2026\u2026..<br \/>\nlast_command<br \/>\nfi<\/p>\n<p>&nbsp;<\/p>\n<pre>#!\/bin\/bash\r\necho \"Enter the Number: \"\r\nread n\r\nif [ $n -gt 150 ]\r\nthen\r\n    echo \"Number $n is greater than 150\"\r\nelif [ $n -lt 150 ]\r\nthen\r\n    echo \"Number $n is smaller than 150\"\r\nelse\r\n    echo \"Number $n is equal to 150\"\r\nfi\r\n\r\n<\/pre>\n<p><span>Komut Dosyas\u0131 Y\u00fcr\u00fctme \u00e7\u0131kt\u0131s\u0131,<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-1981\" src=\"https:\/\/sunucucozumleri.com\/wp-content\/uploads\/2022\/10\/Bash-Komut-Dosyasinda-Kosullu-Ifadeler-Nasil-Kullanilir3.webp\" alt=\"\" width=\"768\" height=\"571\" title=\"\" srcset=\"\/\/sunucucozumleri.com\/blog\/wp-content\/uploads\/2022\/10\/Bash-Komut-Dosyasinda-Kosullu-Ifadeler-Nasil-Kullanilir3.webp 768w, \/\/sunucucozumleri.com\/blog\/wp-content\/uploads\/2022\/10\/Bash-Komut-Dosyasinda-Kosullu-Ifadeler-Nasil-Kullanilir3-300x223.webp 300w\" sizes=\"(max-width: 768px) 100vw, 768px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Ic_ice_if_Ifadesi\"><\/span><span>\u0130\u00e7 i\u00e7e if \u0130fadesi<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span>If deyimi ve else deyimi bir bash beti\u011finde yuvalanabilir.\u00a0&#8216;fi&#8217; anahtar s\u00f6zc\u00fc\u011f\u00fc, i\u00e7 if ifadesinin sonunu g\u00f6sterir ve t\u00fcm if ifadesinin &#8216;fi&#8217; anahtar kelimesiyle bitmesi gerekir.<\/span><\/p>\n<p><span>Yuvalanm\u0131\u015f if&#8217;nin temel s\u00f6zdizimi a\u015fa\u011f\u0131da g\u00f6sterilmi\u015ftir:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>if [ condition_command ]<br \/>\nthen<br \/>\ncommand1<br \/>\ncommand2<br \/>\n\u2026\u2026..<br \/>\nlast_command<br \/>\nelse<br \/>\nif [ condition_command2 ]<br \/>\nthen<br \/>\ncommand1<br \/>\ncommand2<br \/>\n\u2026\u2026..<br \/>\nlast_command<br \/>\nelse<br \/>\ncommand1<br \/>\ncommand2<br \/>\n\u2026\u2026..<br \/>\nlast_command<br \/>\nfi<br \/>\nfi<\/p>\n<p><span>Yukar\u0131daki komut dosyas\u0131n\u0131 i\u00e7 i\u00e7e if ifadesini kullanacak \u015fekilde de\u011fi\u015ftirdi\u011fimizde, kodu a\u015fa\u011f\u0131daki gibi g\u00f6r\u00fcn\u00fcr,<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre>#!\/bin\/bash\r\necho \"Enter the Number: \"\r\nread n\r\nif [ $n -gt 150 ]\r\nthen\r\n   echo \"Number $n is greater than 150\"\r\nelse\r\nif [ $n -lt 150 ]\r\nthen\r\n   echo \"Number $n is smaller than 150\"\r\nelse\r\n  echo \"Number $n is equal to 150\"\r\n  fi\r\nfi\r\n\r\n<img decoding=\"async\" class=\"alignnone size-full wp-image-1982\" src=\"https:\/\/sunucucozumleri.com\/wp-content\/uploads\/2022\/10\/Bash-Komut-Dosyasinda-Kosullu-Ifadeler-Nasil-Kullanilir34webp.webp\" alt=\"\" width=\"768\" height=\"591\" title=\"\" srcset=\"\/\/sunucucozumleri.com\/blog\/wp-content\/uploads\/2022\/10\/Bash-Komut-Dosyasinda-Kosullu-Ifadeler-Nasil-Kullanilir34webp.webp 768w, \/\/sunucucozumleri.com\/blog\/wp-content\/uploads\/2022\/10\/Bash-Komut-Dosyasinda-Kosullu-Ifadeler-Nasil-Kullanilir34webp-300x231.webp 300w\" sizes=\"(max-width: 768px) 100vw, 768px\" \/>\r\n\r\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Vaka_Aciklamasi\"><\/span><span>Vaka A\u00e7\u0131klamas\u0131<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span>Case deyimi, birden fazla elif i\u00e7eren if deyimine benzer.\u00a0bash beti\u011findeki case ifadesi ifadeyi geni\u015fletir ve ard\u0131ndan t\u00fcm kal\u0131plarla e\u015fle\u015fmeyi bulmaya \u00e7al\u0131\u015f\u0131r.\u00a0Bir e\u015fle\u015fme bulundu\u011funda, t\u00fcm ifadeler \u00e7ift noktal\u0131 virg\u00fcle (;;) kadar y\u00fcr\u00fct\u00fcl\u00fcr.\u00a0E\u015fle\u015fme olmamas\u0131 durumunda *) kal\u0131b\u0131nda belirtilen ifadeler \u00e7al\u0131\u015ft\u0131r\u0131lacakt\u0131r.<\/span><\/p>\n<pre>case expression in \r\n    pattern1) \r\n       statements \r\n       ;; \r\n    pattern2) \r\n       statements \r\n       ;; \r\n    pattern3) \r\n       statements \r\n       ;; \r\n    pattern-n) \r\n       statements \r\n       ;;\r\n    *) \r\n       statements \r\n       ;; \r\nesac\r\n\r\n<\/pre>\n<pre>#!\/bin\/bash\r\n\r\necho \"Which is Your Favorite Linux Distro?\"\r\necho \"Debian, Ubuntu, SUSE, RHEL, Arch\"\r\nread -p \"Type Your Linux Distro:\" OS\r\n\r\ncase $OS in\r\n    Debian)\r\n        echo \"Most Stable Linux OS, good choice \"\r\n        ;;\r\n    Ubuntu)\r\n        echo \"Best Linux OS for Desktop and Servers\"\r\n        ;;\r\n    SUSE)\r\n        echo \"Top Linux OS for SAP Application\"\r\n        ;;\r\n    Arch)\r\n        echo \"Flexible OS for experienced Linux users\"\r\n        ;;\r\n    *)\r\n        echo \"Please Enter Correct OS from the list\"\r\n        ;;\r\nesac\r\n\r\n<img decoding=\"async\" class=\"alignnone size-full wp-image-1983\" src=\"https:\/\/sunucucozumleri.com\/wp-content\/uploads\/2022\/10\/Bash-Komut-Dosyasinda-Kosullu-Ifadeler-Nasil-Kullanilir5webp.png\" alt=\"\" width=\"848\" height=\"823\" title=\"\" srcset=\"\/\/sunucucozumleri.com\/blog\/wp-content\/uploads\/2022\/10\/Bash-Komut-Dosyasinda-Kosullu-Ifadeler-Nasil-Kullanilir5webp.png 848w, \/\/sunucucozumleri.com\/blog\/wp-content\/uploads\/2022\/10\/Bash-Komut-Dosyasinda-Kosullu-Ifadeler-Nasil-Kullanilir5webp-300x291.png 300w, \/\/sunucucozumleri.com\/blog\/wp-content\/uploads\/2022\/10\/Bash-Komut-Dosyasinda-Kosullu-Ifadeler-Nasil-Kullanilir5webp-768x745.png 768w\" sizes=\"(max-width: 848px) 100vw, 848px\" \/>\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Bu k\u0131lavuzda, bash beti\u011finde if, if-else, If-elif-else, netsted if ve case gibi ko\u015fullu ifadelerin nas\u0131l kullan\u0131laca\u011f\u0131n\u0131 \u00f6\u011frenece\u011fiz.\u00a0Ko\u015fullu ifadeler, ko\u015fula g\u00f6re karar vermeye yard\u0131mc\u0131 oldu\u011fu i\u00e7in bash beti\u011finde \u00f6nemli bir rol oynar. Bir bash beti\u011finde,\u00a0if ifadesi\u00a0bir ko\u015fulun do\u011fru olup olmad\u0131\u011f\u0131n\u0131 kontrol eder.\u00a0E\u011fer \u00f6yleyse, kabuk if ifadesiyle ili\u015fkili kod blo\u011funu y\u00fcr\u00fct\u00fcr.\u00a0\u0130fade true de\u011filse, kabuk if ifade blo\u011funun &hellip;<\/p>\n","protected":false},"author":1,"featured_media":1983,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1978","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"acf":[],"_links":{"self":[{"href":"https:\/\/sunucucozumleri.com\/blog\/wp-json\/wp\/v2\/posts\/1978","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sunucucozumleri.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sunucucozumleri.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sunucucozumleri.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sunucucozumleri.com\/blog\/wp-json\/wp\/v2\/comments?post=1978"}],"version-history":[{"count":0,"href":"https:\/\/sunucucozumleri.com\/blog\/wp-json\/wp\/v2\/posts\/1978\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sunucucozumleri.com\/blog\/wp-json\/wp\/v2\/media\/1983"}],"wp:attachment":[{"href":"https:\/\/sunucucozumleri.com\/blog\/wp-json\/wp\/v2\/media?parent=1978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sunucucozumleri.com\/blog\/wp-json\/wp\/v2\/categories?post=1978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sunucucozumleri.com\/blog\/wp-json\/wp\/v2\/tags?post=1978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}