CS350 Lecture9 PROGRAMMING THE BASH SHELL PART IV by İlker Korkmaz and Kaya Oğuz.

Slides:



Advertisements
Benzer bir sunumlar
Alakalı müşterileri hedefleyin. Google ile Yeniden Pazarlama Remarketing with Google. Target customers who are already showing interest in your business.
Advertisements

Google Display Network Targeting options.
If you delete this after reading... you'll spend a year of ill luck! But... if you send it to (at least) 2 friends... you'll have 3 years of good luck!!!
I ASİMO I ASİMO PREPARED: CENGİZ MURAT TEKİNBÜĞRÜ English Course Presentation TURKEY Mechatronics Engineering at SAKARYA UNIVERSITY PREPARED: CENGİZ.
Atama ve eşleme (eşleştirme) problemleri (Matching and Assignment problems)
If you delete this after reading... you'll spend a year of ill luck! But... if you send it to (at least) 2 friends... you'll have 3 years of good luck!!!
BTP205 – Görsel Programlama I
Bir Problemin Programa Dönüştürülme Süreci
Hareket halindeki insanlara ulaşın.Mobil Arama Ağı Reklamları Reach customers with Mobile Search Network.
.NET Framework 2.0 Öğr. Gör. Dr. Mustafa Cem Kasapbaşı Hafta 1 Ref Beginning ASP.NET 2.0 in C# 2005 From Novice to Professional İnternet Programlama Dilleri.
NOUN CLAUSES (İSİM CÜMLECİKLERİ).
Kabuk Programlama Shell Scripting(bash)
Bilgisayarlar kodu nasıl işler?
Kampanyanızı optimize edin. Görüntülü Reklam Kampanyası Optimize Edici'yi Kullanma Display Ads Campaign Optimizer. Let Google technology manage your diplay.
SÜLEYMAN DEM İ REL PRIMARY AND SECONDARY SCHOOL. GENERAL INTRODUCTION SCHOOL DIRECTORY, TEACHERS AND OTHER STAFF CLASSROOMS PARTS OF THE SCHOOL GALLERY.
INTRODUCTION TO JAVASCRIPT. JAVASCRIPT ► JavaScript is used in millions of Web pages to improve the design, validate forms, detect browsers, create cookies,
COSTUMES KILIKLAR (KOSTÜMLER)
ISE Senior Project Fall 2015.
BM-305 Mikrodenetleyiciler Güz 2015 (6. Sunu) (Yrd. Doç. Dr. Deniz Dal)
Yapısal Programlama Yüksek Düzeyli Dillerin Gelişim Süreci – , Fortran (by IBM), for creating scientific and engineering applications, first commercial.
Database for APED Büşra Bilgili | Emirhan Aydoğan | Meryem Şentürk | M. Arda Aydın COMPE 341.
S ÜLEYMAN Ş AH ÜN İ VERS İ TES İ DERS KAYIT İŞ LEMLER İ / COURSE REGISTRATION PROCESS.
21/02/2016 A Place In My Heart Nana Mouskouri « Istanbul « (A Different Adaptation)
Environmental pollution Traffic Infrastructural problems Unconscious employee Urbanization and industrialization Lack of financial sources.
Practice your writing skills
First Conditional Sentences. LOOK AT THE EXAMPLES If the weather is fine, we’ll play tenis If I have enough money, I’ll buy the car If it rains, we’ll.
CHAPTER Section 1.1 What Is Entrepreneurship? Section 1.2 Characteristics of an Entrepreneur Introduction to Entrepreneurship.
DISCUSSION
Students social life and join the social clubs. BARIŞ KILIÇ - EGE DÖVENCİ IŞIK ÜNİVERSİTESİ
INTRODUCTION TO JAVASCRIPT
Self-Registration on the Coats Supplier Portal
İSTATİSTİK II Hipotez Testleri 1.
PROGRAMMING THE BASH SHELL PART IV by İlker Korkmaz and Kaya Oğuz
INTRODUCTION TO JAVASCRIPT
Superstitions Grade 7 Unit Bilginbakterim.com
CHAPTER 1 uzm. Psk. Özlem ataoğlu
BİLİMSEL ÇALIŞMA BASAMAKLARI SCIENTIFIC WORKING STEPS MHD BASHAR ALREFAEI Y
Ac POWER ANALYSIS Part III..
Bilgisayarlar kodu nasıl işler?
Bir Problemin Programa Dönüştürülme Süreci
Web Programming– UFCFB Lecture 13
The Need for Program Design
Chapter 1 (Bölüm 1) The accounting equation(muhasebe denklemi) and the balance sheet(bilanço)
INTRODUCTION TO JAVASCRIPT
BMS-301 Kabuk Programlama Güz 2017 (7. Sunu) (Yrd. Doç. Dr. Deniz Dal)
INTRODUCTION TO JAVASCRIPT
If you delete this after reading... you'll spend a year of ill luck!
Future: I will/shall & I am going to. Structure: Subject+will/shall+verb(base form)+object.
tomorrow soon next week / year in five minutes/ in two hours later today I'll go to the market tomorrow. Don’t worry. He will be here soon. There.
NİŞANTAŞI ÜNİVERSİTESİ
MAKİNA TEORİSİ II GİRİŞ Prof.Dr. Fatih M. Botsalı.
The Need for Program Design
PRODUCTION. CONTENT  WHAT IS PRODUCTION  BENEFITS OF PRODUCTION  ELEMENTS THAT CHARACTERIZE THE PRODUCTION PROCESS  CLASSIFICATION OF PRODUCTION SYSTEMS.
“Differentiation for making a positive Difference!!!!”
İSTATİSTİK II Hipotez Testleri 3.
NİŞANTAŞI ÜNİVERSİTESİ
Before the Battle of Çanakkale. Why a Front in Çanakkale was Opened? In the summer of 1914, the war continued in Europe with all its intensity, and by.
Feminism, unlike the idea of ​​ mankind, is a trend that is prioritized to bring gender inequality to the agenda. The notion of feminism, which is not.
Yapısal Programlama Yüksek Düzeyli Dillerin Gelişim Süreci
Güzel bir bahar gününün sabahında İETT şöförü garaja gider, otobüsünü çalıştırır ve yola çıkar. Sorunsuz bir şekilde bir duraktan diğerine ilerler, yolcularını.
Chapter 5 – Balancing of accounts
Imagine that you are a teacher and you are taking your 20 students to England for the summer school.
PREPARED BY: 9-B STUDENTS. Sumerians, who laid the foundations of great civilizations and the world cultural heritage, emerged to the st The Sumerians.
DÜZLEMSEL MEKANİZMALARIN
Bilgisayarlar kodu nasıl işler?
SUBJECT NAME Prepeared by Write the names of group members here
People with an entrepreneurial mindset are always brave.
UNIT 1: FRIENDSHIP.
TWINSPACE Anthony RIOU Coşku AKER TR Teacher Multilateral Contact Seminar, İ zmir, April 2019.
Sunum transkripti:

CS350 Lecture9 PROGRAMMING THE BASH SHELL PART IV by İlker Korkmaz and Kaya Oğuz

Programming the bash ● As pointed in syllabus document, there are 5 lecture weeks to cover the programming with bash scripts. ● This lecture is the last part on programming the bash shell.

References ● The contents of this lecture are prepared with the help of and based on: – the textbook, UNIX Shells by Example (Chapter15) – the tutorial, Advanced Bash-Scripting Guide at

Contents of Lecture9 ● Debugging shell scripts

Why to cover the debugging subject ? ● Chapter15 of the textbook, UNIX Shells by Example, aims to provide the debugging concept for finding, fixing, and understanding some types of errors that cause shell scripts to misbehave. ● Although they have a successful script, the system administrators will usually want to make it better to maintain. In this sense, tracing the the script may be helpful. ● Therefore, a good script is not only a successful one, but owns the good style issues as well.

Lecture design ● At first, the style issues are introduced. ● Secondly, the types of errors are categorized. ● Finally, tracing options for debugging the runnings codes are presented. ● At the end of the lecture, your common mistakes on styling issues are listed according to the observation on your HW and LAB works submitted.

Style Issues ● To be written in a good style is an important issue for any program in deed. ● A good style in script design can be helpful to quickly find possible bugs in the scripts. ● Even the script does not include any bug, which is an unrealistic property according to the philosophy of program- bug relation, it could be easily readable and also maintainable if it is written in a good style. ● So, what are the style issues for a script?

Commenting ● As the first style issue, put helpful comments in your scripts to maintain them later in the future without spending any extra time to understand the objectives of them.

Variable naming ● Define variables with meaningful names and put them at the beginning point of their scope, which is generally the top of the script. ● Pay attention to case sensitivity. ● Assure that your script does not include any variable with an identifier name among the reserved words.

Indentation ● Use indentation in the code. ● Whenever you use a conditional or looping command, indent the block of statements that follows, at least one tab stop.

Echoing ● Use the echo command in areas where you suspect of any syntax error to trace the program execution.

Logic correctness ● The programs may contain some logic errors, even a program runs without any syntax error. ● The operators are the possible logic error sources. Some operators have different use in different shells.

Robustness ● To make your program robust test it carefully. ● Check your script for any possible human error, such as bad input, insufficient arguments, nonexistent files, and so on.

Simplicity ● Try to implement the script in a simple way. ● Also when testing your script, keep simplicity. As an illustration, a test on the syntax of the function may be handled via trying it within a short script to check whether the result is similar to the expected one or not.

Command know-how ● Know the commands of the OS. ● Know the commands of the shell. ● If you are new to a command, try it on the command line before using in a script. ● By the way, try to understand the behavior of the command, such as what it returns, what the exit code of its is. ● Also try to understand the OS behavior while issuing the commands, such as how the variables are interpreted in your OS, how to redirect output and errors in your OS.

System administrator responsibility ● If you are an administrator of a system, test your any script carefully before taking it to the system level. An unexpected error could bring a whole system to its knees.

Types of errors ● Runtime errors ● Logical errors

Runtime errors ● Syntactical errors ● Mismatched quotes ● Misspelled errors ● Bad script name, ● Permission issues, ● Path problems, ●...

Naming conventions ● If you have a script with a name of ls, when you try ls in the command line which ls will be executed? ● It depends on which ls is found in the path first. ● which command tells you the path where the named program is found. ● So, avoid using the following command names as to be filenames. ● test ● script ● ls ●... ● You can also try the script with a leading./ to run the script in the current directory.

Insufficient permissions ● Check the permissions of the files. ● The scripts needs an execute permission to be run. ● $ ls -l myScript ● chmod...

Path problems ● If you have a superuser account, it is recommended, for security reasons, that you do not include a dot in the search PATH variable. ● There are two alternatives: ● Give the explicit path: ● $./myScript ● Precede the name of the script with the name of the shell ● $ bash myScript ● # The shell automatically checks the current directory for myScript

The shbang line ● Shbang ( #! ) line as the first line of a script. ● The path following the shbang notation is the location of the shell that will be invoked to interpret the script. ● If the shbang is put at any top line except the first line of the script, the line will be ignored to be interpreted.

Sneaky alias ● Remember any alias you defined previously. ● You may delete an alias if you do not need it anymore. ● unalias...

Two reminders: ● 1. Check the page 985 of the textbook for what you need to know about quotes. ● 2. Check the page 999 for common bash error messages.

Tracing the script execution ● By using the -n option to the bash command, you can check the syntax of your scripts without really executing any command. ● If there is a syntax error in the script, error will be reported, nothing will be displayed. ● $ bash -n myScript ● # interprets but does not execute commands ● The two usual debugging methods of any script are as follows: ● $ bash -x myScript ● # displays each line of myScript after variable substitutions and before execution ● $ set -x ● # turns on echo

Demos for debugging: ● #!/bin/bash ● toplam=0 ● sayac=0 ● for i in $(cat liste) ● do ● let toplam=$toplam+$i ● let sayac=$sayac+1 ● echo $toplam ● done ● echo "sayac: $sayac" ● declare -i ortalama ● ortalama=$toplam/$sayac ● echo "ortalama: " $ortalama ● # myScript.sh file contains the code above ● Try followings: ● $ bash -n myScript ● $ bash -x myScript ● $ bash myScript

Suggestions according to your HW-LAB-QUIZ answers ● (in Turkish): ● 1- Renklendiren bir metin düzenleyici kullanmak: buradaki amaç “keyword” ile denk gelen değişken isimlerini (“file” gibi) engellemek, yapılan hataları daha rahat görebilmek ● 2- “Indentation” - bu sayede döngü ve kontrol bloklarını daha rahat görebiliyoruz ● 3- Kodu tam olarak test etmek. Özellikle o uzun ödevde /etc/passwd dosyasını okurken grep yanlış kullanılmış. Örneğin, kullanıcı adı koguz ise, oguz veya uz yazılsa bile grep o ifadeyi bulduğu için "böyle bir kullanıcı var" diye sonuç geliyor. Halbuki \ kullanarak kelimeyi tam olarak aramak mantıksal olarak daha uygun olabilir. Bir diğeri de echo `ps` gibi olaylar. Burada ps zaten ekrana bir çıktı veriyor, onu bir daha echo ile yazdırmak ekran biçimlendirmesini bozuyor. ● 4- Calistirilan dosya disindan gelen veriler icin illa baska bir dosyadan okuma ihtiyacı yok. Halbuki dosyadan okumak zahmetli sayılır - ve yapacağımız ● birçok iş aslında diğer komutların çıktılarını işlemek oluyor (ls gibi). Bash'in güzel bir yanı, bir komutun çıktısı üzerinde işlem yapmayı kolaylaştıran yapısı (ister pipe ● ile ister for i in $(ls) şeklinde...) ● 5- Quiz sorusunda C gibi yazanlar olmuş :) Her ne kadar kontrol ve döngü yapıları her dilde benzese de dillerin kullanımı farklı düşünülmüş demek ki yazılmalarını gerektiren durumlar oluşmuş. Yani kabuk programlarken, C programlar gibi düşünmemek daha uygun olabilir :P