Linux with Operating System Concepts



Download 5,65 Mb.
Pdf ko'rish
bet94/254
Sana22.07.2022
Hajmi5,65 Mb.
#840170
1   ...   90   91   92   93   94   95   96   97   ...   254
Bog'liq
Linux-with-Operating-System-Concepts-Fox-Richard-CRC-Press-2014

interpreted
instead of compiled. An interpreter takes each instruction 
in the script one at a time, translates it to an executable statement, and executes it. This is 
far less efficient for the end-user because the end-user must then wait during instruction 
translation. Compiled programs have already been translated so the end-user runs the 
program without waiting for translation.
Many Linux users write and run both types of programs. The developers will write code 
in C or C
++
(or Java). These tend to be larger programs where compilation is a necessity so 
that the executable code can be distributed to end-users to run. However, when it comes 
to small programs, many Linux users and system administrators will write interpreted 


246

Linux with Operating System Concepts
programs instead, called 
scripts
. There are many scripting languages available for Linux 
including Python, Ruby, PHP, and Perl. Each shell type has its own scripting language as 
well. In this chapter, we concentrate on the Bash scripting language although at the end of 
the chapter we briefly compare it to the C-shell scripting language.
The reason that we need to learn how to write scripts is that we need to produce code 
to automate Linux system tasks. Many of these scripts are the glue of the Linux operating 
system, holding together the already written portions of the system. We can get away with 
using an interpreted language here because our scripts will commonly be small programs 
whose execution time is not impacted much by the need to be interpreted during execution.
The advantage of scripting is that you can experiment with individual instructions to 
see what they do first, while putting together your program. Also, the interpreted environ-
ment provides the user with the ability to have a “session” by which he or she can refer back 
to previous definitions and results. Thus, as the session goes on, the user/programmer can 
add more and more to it and recall the items already defined.
As each shell has its own interpreter, you can enter shell instructions from the command 
line. The shell session then creates the environment whereby definitions such as aliases, 
variables, functions, and the history list persist. You can also capture the shell instructions 
in a file to be recalled at later times for convenience. If the tasks that make up this file are 
commonly used, the user can then schedule this file to be executed at regular intervals. 
Thus, the shell script is a mechanism for automation.
The Bash shell scripting language is a programming language that includes instructions 
much like those found in most programming languages such as loops, selection statements, 
input and output statements, and assignment statements. A Bash script can therefore com-
bine such instructions with Linux commands.
Bash also permits the definition of functions and then function calls among the script 
code. Additionally, variables are available and can include environment variables (e.g., 
HOME, PWD, and PATH) along with any variables defined in the script or those defined 
from the command line and then exported. Parameters can also be supplied by the user 
when the script is executed. These parameters can then be utilized in the script.
In this chapter, we introduce shell scripting as a programming tool. We will examine 
most of the constructs available in the Bash shell. At the end of the chapter, we will also 
briefly examine the constructs found in the Csh shell.
Note: It is assumed that the reader already has some knowledge of programming and so 
concepts such as variables, logic, loops, and so forth are not described in any detail.
7.2 SIMPLE SCRIPTING
7.2.1 Scripts of Linux Instructions
Every shell script must start with a comment that specifies the interpreter which should 
run the script. For instance, you would start your script with one of the following lines, 
depending on which shell you are operating on (or which shell interpreter you wish to call 
upon). Here are examples for each of Bash, Csh, and Tcsh.


Shell Scripting

247
#!/bin/bash
#!/bin/csh
#!/bin/tcsh
The script file must also be executable. We will use permission 745, or alternatively 755.
Among the simplest scripts we can write are those that perform a sequence of Linux 
operations. For instance, as a user, you might want to start off each login with a status 
report. This report might include the current time/day, your disk utilization, and a list of 
any empty files found. We can write a script that executes commands such as: 
date

du 
–s ~

find ~ -empty
. Our script might look like this:
#!/bin/bash
date
du –s ~
find ~ -empty
The blank line after 
#!/bin/bash
is not needed and if we wanted, we could separate 
each of the other commands with blank lines. We can also add comments to the script to 
describe the instructions. Comments always follow the # symbol as in
date # output the current date and time for the report
7.2.2 Running Scripts
Assume that we save the previous script under the name 
start
. After changing the per-
missions of the file 
start
to 745 (using chmod), we can execute this script by entering 
.
/start
. The 
start
script outputs information to your terminal window.
Consider what might happen if you have a lot of empty files under your home directory. 
The output may stream through the terminal window too rapidly for you to see the full 
report. To make the output more readable, we might enhance this script in two ways. First, 
we can add echo statements that output literal messages that explain what the output is 
such as “your disk utilization is” and “your empty files are.” We can also add line breaks to 
add blank lines in the output to separate the three types of output being generated. Second, 
we can send some or all of the output to a disk file. This would allow the user to view the 
output using more or less or in a program like vi.
The output statement is echo, as we saw from Chapter 2. To redirect the output of a state-
ment, we use 
>

Download 5,65 Mb.

Do'stlaringiz bilan baham:
1   ...   90   91   92   93   94   95   96   97   ...   254




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish