Linux with Operating System Concepts



Download 5,65 Mb.
Pdf ko'rish
bet20/254
Sana22.07.2022
Hajmi5,65 Mb.
#840170
1   ...   16   17   18   19   20   21   22   23   ...   254
Bog'liq
Linux-with-Operating-System-Concepts-Fox-Richard-CRC-Press-2014

Keystroke
Meaning
c
+
a
Move cursor to beginning of line
c
+
e
Move cursor to end of line
c
+
n (also up arrow)
Move to next instruction in history list
c
+
p (also down arrow)
Move to previous instruction in history list
c
+
f (also right arrow)
Move cursor one character to the right
c
+
b (also left arrow)
Move cursor one character to the left
c
+
d (also delete key)
Delete character at cursor
c
+
k
Delete all characters from cursor to end of line
c
+
u
Delete everything from the command line
c
+
w
Delete all characters from front of word to cursor
c
+
y
Yank, return all deleted characters (aside from c
+
d) 
to cursor position
c
+
_
Undo last keystroke
m
+
f
Move cursor to space after current word
m
+
b
Move cursor to beginning of current word
m
+
d
Delete the remainder of the word


54

Linux with Operating System Concepts
Now you wish to see the next user’s home directory, marst. Here are the keystrokes you 
could enter to accomplish this.
• 
c
+
p
—Move back one instruction in the history list, now the command line has the 
previous command, placing 
ls /home/dukeg
on the command line.
• 
m
+
b
—Move back to the beginning of the current word, the cursor is now at the ‘d’ in 
dukeg in 
ls /home/
dukeg.
• 
c
+
k
—Kills the rest of the line, that is, it deletes all of dukeg leaving 
ls /home/ 
on 
the command line.
Now type 
marst
<
enter
>
which causes 
ls /home/marst
to execute. You are shown 
marst’s directory. Next, you repeat the same three keystrokes and enter 
underwoodi 
<
enter
>
. Now you are shown underwoodi’s directory. Next, you can enter the following:
• 
c
+
p
—Move back one instruction in the history list.

<
backspace
>
.
And then type 
r
<
enter
>
. This deletes the ‘i’ in underwoodi and replaces it with ‘r.’ 
Finally, you repeat the three keystrokes above (c
+
p, m
+
b, c
+
k) and type 
zappaf 
<
enter
>
.
We will visit another example in Chapter 3 which will hopefully further convince you 
of the value of command line editing.
2.4.5 Redirection
In Linux, there are three predefined locations for interaction with the Linux commands 
(programs). These are standard input (abbreviated as STDIN), standard output (STDOUT), 
and standard error (STDERR). Typically, a Linux command will receive its input from 
STDIN and send its output to STDOUT. By default, STDIN is the keyboard and STDOUT 
is the terminal window in which the instruction was entered.
These three locations are actually file descriptors. A file descriptor is a means for rep-
resenting a file to be used by a process. In Linux, file descriptors are implemented as inte-
ger numbers with 0, 1, and 2 reserved for STDIN, STDOUT, and STDERR, respectively. 
A redirection is a command that alters the defaults of STDIN, STDOUT, and STDERR.
There are two forms of output redirection, two forms of input redirection, and a fifth 
form of redirection called a 
pipe
. Output redirection sends output to a file rather than to 
the terminal window from which the program was run. One form writes to the file while 
the other appends to the file. The difference is that if the file previously existed, the first 
form overwrites the file. One form of input redirection redirects input from a text file to 
the program while the other redirects input from keyboard to the program. The Linux 
pipe is used to send the output of one program to serve as the input to another. This 
allows the user to chain commands together without having to use temporary files as an 
intermediate.


The Bash Shell

55
The forms of redirection are as follows:

>
-Send output to the file whose name is specified after the 
>
; if the file already exists, 
delete its contents before writing.

>>
-Append output to the file whose name is specified after the 
>>
; if the file does not 
currently exist, create it.

<
-Use the file whose name is specified after the 
<
as input to the command.

<<
-Use STDIN (keyboard) as input. As the 
<
enter
>
key can be part of any input 
entered, we need to denote the end of input. There are two mechanisms for this. The 
user can either use control
+
d to indicate the end of input or place a string after the 
<<
characters to be used as an “end of input” string.
• |-Redirect the output of one command to serve as the input to the next command.
Let us examine the pipe. You want to count the number of items in a directory. The ls 
command only lists the items found in a directory. The wc command is a word counter. It 
counts the number of items found in an input and outputs three pieces of information: the 
number of lines in the input, the number of words in the input, and the number of char-
acters in the input. If we use 
wc –l
(a lower case “L”), it only outputs the number of lines. 
The 
ls -1
(the number 1) outputs the listing of items in a directory all in one column.
We can combine these two instructions with a pipe. A pipe is denoted with the char-
acter 
|
(vertical bar) between the two instructions. In order to pipe from the long listing 
of ls to word count, we issue the command 
ls -1 | wc –l
. This instruction consists 
of two separate commands. The first outputs the items in the current directory, all in one 
column. This output is not sent to the terminal window but instead used as input for the 
wc command. The wc command expects input to come from a file, but here it comes from 
ls -1
. The wc command counts the number of lines, words and characters, and outputs 
the number of lines.
The ‘
<
’ redirection is probably the least useful because most Linux commands expect 
input to come from file. We will see, later in the text, that the 
<
form of redirection is used 
in some instances when a user wants to use a file as input to a user-defined script. We will 
also find 
<<
to be of limited use. Here, we examine all four, starting with 
>
and 
>>
.
If you wish to store the results of a Linux program to a file, in most cases, you will redi-
rect the output. If you want to collect data from several different commands (or the same 
command entered on different parameters or at different times), you would probably want 
to append the output to a single file.
Consider that you want to create a file that stores all of the items in various users’ direc-
tories. You want to put them all into a single file called user_entries.txt. Let us use the same 
five users from the example in Section 2.4.4. The five commands would be
ls/home/dukeg 
>
user_entries.txt
ls/home/marst 
>>
user_entries.txt


56

Linux with Operating System Concepts
ls /home/underwoodi 
>>
user_entries.txt
ls /home/underwoodr 
>>
user_entries.txt
ls /home/zappaf 
>>
user_entries.txt
Notice that the first redirection symbol is ‘
>
’ to create the file. If the file already existed, 
this overwrites the file. Had we used ‘
>>
’ instead and the file already existed, we would 
be appending not only the latter four outputs but all five of them to the file. NOTE: using 
command line editing features would simplify the five instructions. Can you figure out 
how to do that?
The cat command performs a concatenate operation. This means that it joins files 
together. We can use cat in several different ways. For instance,
cat filename
will output the result of filename to the terminal window. This lets you quickly view the 
contents of a text file. Another use of cat is to take multiple files and join them together. 
The command
cat file1.txt file2.txt file3.txt
will output the three files to the terminal window, one after another. Instead of dumping 
these files to the window though, we might be interested in placing them into a new file. 
We would use redirection for that as in
cat file1.txt file2.txt file3.txt 
>
joined_files.txt
Yet another use of cat is to create a file from input entered by the keyboard. This 
requires two forms of redirection. First, we alter cat’s behavior to receive input from the 
keyboard rather than from one or more files. The 
<
redirection redirects input to come 
from a file rather than a keyboard. We want to use 
<<
instead. To establish the end of 
the input, we will use a string rather than control
+
d. This means that as the user enters 
strings and presses 
<
enter
>
, each string is sent to cat. However, if the string matches our 
end-of-input string, then cat will terminate. We will use the string “done.” The com-
mand is so far
cat 
<<
done
This tells the interpreter to receive anything that the user enters via the keyboard, 
including 
<
enter
>
keystrokes, terminating only when the user enters “done
<
enter
>
.”
Because cat defaults to outputting its results to the terminal window, whatever you 
entered via 
cat
<<
done
is output back to the window (except for “done” itself). Consider 
for instance the following interaction. Notice that typical $ is the user prompt while 
>
is the 
prompt used while the user is entering text in the cat program.


The Bash Shell

57
$ cat 
<<
done
>
pear
>
banana
>
cherry
>
strawberry
>
done
pear
banana
cherry
strawberry
$
As can be seen, the four lines that the user entered are echoed to the window. The string 
“done” is not. This seems like a useless pursuit; why bother to repeat what was entered? 
What we would like is for cat to output to a file. So we have to add another redirection clause 
that says “send the output to the specified file.” The full command then might look like this.
cat 
<<
done 
>
fruit
Thus, the list of entered items is saved to the file name fruit.
All five forms of redirection can be useful, but the pipe is probably the one you will use 
the most. We will hold off on further details of the pipe until we reach Chapter 3.
2.4.6 Other Useful Bash Features
There are numerous other useful features in Bash. We have already alluded to two of them: 
tilde expansion and wildcards. In tilde expansion, the ~ is used to denote a user’s home 
directory. If provided by itself, it means the current user’s home directory. For instance, if 
you are foxr, then ~ is foxr’s home directory. If used immediately before a username, then 
the tilde indicates that particular user’s home directory. For instance, 
~zappaf
would 
mean zappaf’s home directory. In most cases, user home directories are located under 
/home, so 
~zappaf
would be /
home/zappaf
.
Wildcards are a shortcut approach to specify a number of similarly named files without 
having to enumerate all of them. The easiest wildcard to understand, and the most com-
monly used wildcard, is the 
*
. When the * is used, the Linux interpreter replaces it with all 
entities in the given directory. For instance, 
rm *
would delete everything in the current 
directory. The * can be used as a portion of a name. All of the items that start with the name 
“file” and end with “.txt” but can have any characters in between would appear as file*.txt. 
This would, for instance, match file1.txt, file21.txt, file_a.txt, and also file.txt. There are 
other wildcards available. The wildcards are covered in more detail in Chapter 3.
Another highly useful feature is called tab completion. When entering a filename (or 
directory name), you do not have to type the full name. Instead, once you have typed a 
unique portion of the name, pressing 
<
tab
>
will cause Bash to complete it for you. If there 
are multiple matches, the shell beeps at you. Pressing 
<
tab
>
<
tab
>
will cause Bash to 
respond with all possible matches.


58

Linux with Operating System Concepts
For instance, imagine that your current directory stores the following three files that 
start with the letter ‘f.’
foolish.txt forgotten.txt funny.txt
Typing the command 
ls for
<
tab
>
causes Bash to complete this for you as 
ls for-
gotten.txt
. Typing 
ls fo
<
tab
>
causes Bash to beep at you because there are two 
possible matches. Typing 
ls fo
<
tab
><
tab
>
will cause Bash to list both 
foolish.txt
and 
forgotten.txt
and repeat the command line prompt with 
ls fo
on it, waiting 
for you to continue typing. Tab completion is extremely useful in that it can reduce the 
amount of your typing but it can also help you identify a filename or directory name if you 
cannot remember it.
Finally, and perhaps most confusingly is brace expansion. The user can specify a list of 
directories inside of braces (curly brackets, or {}). Each item is separated by a comma. This 
is perhaps not as useful as other types of shortcuts because most Linux commands can 
accept a list of files or directories to begin with. For instance, 
ls /home/foxr /home/
zappaf /home/marst /home/dukeg
would list all four of these users’ directories. 
With the braces though, the user has less to type as this can become
ls /home/{foxr,zappaf,marst,dukeg}
There are more complex uses of the braces, and like the wildcards, this will be covered 
in more detail in Chapter 3.
Recall that a shell contains an environment. This environment consists of all of the 
entities defined during this session. What happens if you want some definitions to persist 
across sessions? There are two mechanisms to support this. First, you can define scripts 
that run when a new shell starts. The scripts contain the definitions that you want to per-
sist. To support this, Linux has pre-set script files available. As a user, all you have to do is 
edit the files and add your own definitions. Second, you can export variables that already 
have values using the export instruction, as in
export 

Download 5,65 Mb.

Do'stlaringiz bilan baham:
1   ...   16   17   18   19   20   21   22   23   ...   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