tigger.itc.mxtigger.itc.mx/conacad/cargas/euuc700411523/fm7/linux...  · web view2019. 8. 6. ·...

90
9.1 Introduction In this chapter, we will discuss how the tools you have learned so far can be turned into reusable scripts.

Upload: others

Post on 02-Aug-2021

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

9.1 IntroductionIn this chapter, we will discuss how the tools you have learned so far can be turned into reusable scripts.

Page 2: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,
Page 3: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

9.2 Shell Scripts in a NutshellA shell script is a file of executable commands that has been stored in a text file. When the file is run, each command is executed. Shell scripts have access to all the commands of the shell, including logic. A script can therefore test for the presence of a file or look for particular output and change its behavior accordingly. You can build scripts to automate repetitive parts of your work, which frees your time and ensures consistency each time you use the script. For instance, if you run the same five commands every day, you can turn them into a shell script that reduces your work to one command.

A script can be as simple as one command:

echo “Hello, World!”

The script, test.sh, consists of just one line that prints the string Hello, World! to the console.

Running a script can be done either by passing it as an argument to your shell or by running it directly:

sysadmin@localhost:~$ sh test.sh

Hello, World!

sysadmin@localhost:~$ ./test.sh

-bash: ./test.sh: Permission denied

sysadmin@localhost:~$ chmod +x ./test.sh

sysadmin@localhost:~$ ./test.sh

Hello, World!

In the example above, first, the script is run as an argument to the shell. Next, the script is run directly from the shell. It is rare to have the current directory in the binary search path $PATH so the name is prefixed with ./ to indicate it should be run out of the current directory.The error Permission denied means that the script has not been marked as executable. A quickchmod later and the script works. chmod is used to change the permissions of a file, which will be explained in detail in a later chapter.

There are various shells with their own language syntax. Therefore, more complicated scripts will indicate a particular shell by specifying the absolute path to the interpreter as the first line, prefixed by#! as shown:

Page 4: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

#!/bin/sh

echo “Hello, World!”

or

#!/bin/bash

echo “Hello, World!”

The two characters, #! are traditionally called the hash and the bang respectively, which leads to the shortened form of “shebang” when they’re used at the beginning of a script.

Incidentally, the shebang (or crunchbang) is used for traditional shell scripts and other text-based languages like Perl, Ruby, and Python. Any text file marked as executable will be run under the interpreter specified in the first line as long as the script is run directly. If the script is invoked directly as an argument to an interpreter, such as sh script or bash script, the given shell will be used no matter what’s in the shebang line.

It helps to become comfortable using a text editor before writing shell scripts, since you will need to create files in plain text. Traditional office tools like LibreOffice that output file formats containing formatting and other information are not appropriate for this task.

9.3 Editing Shell ScriptsUNIX has many text editors, the merits of one over the other are often hotly debated. Two are specifically mentioned in the LPI Essentials syllabus: The GNU nano editor is a very simple editor well suited to editing small text files. The Visual Editor, vi, or its newer version, VI improved (vim), is a remarkably powerful editor but has a steep learning curve. We’ll focus on nano.

Type nano test.sh and you’ll see a screen similar to this:

GNU nano 2.2.6 File: test.sh modified

#!/bin/sh

echo "Hello, World!"

echo -n "the time is "

date

Page 5: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

^G Get Help ^O WriteOut ^R Read File ^Y Prev Page ^K Cut Text ^C Cur Po

^X Exit ^J Justify ^W Where Is ^V Next Page ^U UnCut Text^T To Spell

The nano editor has few features to get in your way. You simply type with your keyboard, using the arrow keys to move around and the delete/backspace button to delete text. Along the bottom of the screen you can see some commands available to you, which are context sensitive and change depending on what you’re doing. If you’re directly on the Linux machine itself, as opposed to connecting over the network, you can also use the mouse to move the cursor and highlight text.

To get familiar with the editor, start typing out a simple shell script while inside nano:

GNU nano 2.2.6 File: test.sh modified

#!/bin/sh

echo "Hello, World!"

echo -n "the time is "

date

Page 6: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

^G Get Help ^O WriteOut ^R Read File ^Y Prev Page ^K Cut Text ^C Cur Po

^X Exit ^J Justify ^W Where Is ^V Next Page ^U UnCut Text^T To Spell

Note that the bottom-left option is ^X Exit which means “press control and X to exit”. Press Ctrland X together and the bottom will change:

Save modified buffer (ANSWERING "No" WILL DESTROY CHANGES) ?

Y Yes

N No ^C Cancel

At this point, you can exit the program without saving by pressing the N key, or save first by pressing Yto save. The default is to save the file with the current file name. You can press the Enter key to save and exit.

You will be back at the shell prompt after saving. Return to the editor. This time press Ctrl and Otogether to save your work without exiting the editor. The prompts are largely the same, except that you’re back in the editor.This time use the arrow keys to move your cursor to the line that has "The time is”. Press Controland K twice to cut the last two lines to the copy buffer. Move your cursor to the remaining line and press Control and U once to paste the copy buffer to the current position. This makes the script echo the current time before greeting you and saved you needing to re-type the lines.

Other helpful commands you might need are:

Command Description

Ctrl + W search the document

Ctrl + W, then Control + R search and replace

Ctrl + G show all the commands possible

Ctrl + Y/V page up / down

Ctrl + C show the current position in the file and the file’s size

Page 7: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

 Previous Next 

9.4 Scripting BasicsYou got your first taste of scripting earlier in this chapter where we introduced a very basic script that ran a single command. The script started with the shebang line, telling Linux that /bin/bash (which is Bash) is to be used to execute the script.

Other than running commands, there are 3 topics you must become familiar with:

Variables, which hold temporary information in the script Conditionals, which let you do different things based on tests you

write Loops, which let you do the same thing over and over

 Previous Next 

9.4.1 VariablesVariables are a key part of any programming language. A very simple use of variables is shown here:

#!/bin/bash

ANIMAL="penguin"

echo "My favorite animal is a $ANIMAL"

After the shebang line is a directive to assign some text to a variable. The variable name is ANIMALand the equals sign assigns the string penguin. Think of a variable like a box in which you can store things. After executing this line, the box called ANIMAL contains the word penguin.

It is important that there are no spaces between the name of the variable, the equals sign, and the item to be assigned to the variable. If you have a space there, you will get an odd error such as “command not found”. Capitalizing the name of the variable is not necessary but it is a useful convention to separate variables from commands to be executed.

Page 8: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

Next, the script echos a string to the console. The string contains the name of the variable preceded by a dollar sign. When the interpreter sees that dollar sign it recognizes that it will be substituting the contents of the variable, which is called interpolation. The output of the script is then My favorite animal is a penguin.

So remember this: To assign to a variable, just use the name of the variable. To access the contents of the variable, prefix it with a dollar sign. Here, we show a variable being assigned the contents of another variable!

#!/bin/bash

ANIMAL=penguin

SOMETHING=$ANIMAL

echo "My favorite animal is a $SOMETHING"

ANIMAL contains the string penguin (as there are no spaces, the alternative syntax without using quotes is shown). SOMETHING is then assigned the contents of ANIMAL (because ANIMAL has the dollar sign in front of it).

If you wanted, you could assign an interpolated string to a variable. This is quite common in larger scripts, as you can build up a larger command and execute it!

Another way to assign to a variable is to use the output of another command as the contents of the variable by enclosing the command in back ticks:

#!/bin/bash

CURRENT_DIRECTORY=`pwd`

echo "You are in $CURRENT_DIRECTORY"

This pattern is often used to process text. You might take text from one variable or an input file and pass it through another command like sed or awk to extract certain parts and keep the result in a variable.

It is possible to get input from the user of your script and assign it to a variable through the readcommand:

#!/bin/bash

echo -n "What is your name? "

read NAME

echo "Hello $NAME!"

Page 9: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

The read command can accept a string right from the keyboard or as part of command redirection like you learned in the last chapter.

There are some special variables in addition to the ones you set. You can pass arguments to your script:

#!/bin/bash

echo "Hello $1"

A dollar sign followed by a number N corresponds to the Nth argument passed to the script. If you call the example above with ./test.sh the output will be Hello Linux. The $0 variable contains the name of the script itself.

After a program runs, be it a binary or a script, it returns an exit code which is an integer between 0 and 255. You can test this through the $? variable to see if the previous command completed successfully.

sysadmin@localhost:~$ grep -q root /etc/passwd

sysadmin@localhost:~$ echo $?

0

sysadmin@localhost:~$ grep -q slartibartfast /etc/passwd

sysadmin@localhost:~$ echo $?

1

The grep command was used to look for a string within a file with the –q flag, which means “quiet”. The grep, while running in quiet mode, returns 0 if the string was found and 1 otherwise. This information can be used in a conditional to perform an action based on the output of another command.Likewise you can set the exit code of your own script with the exit command:

#!/bin/bash

# Something bad happened!

exit 1

The example above shows a comment #. Anything after the hash mark is ignored which can be used to help the programmer leave notes. The exit 1 returns exit code 1 to the caller. This even works in the shell, if you run this script from the command line and then type echo $? you will see it returns1.

By convention, an exit code of 0 means “everything is OK”. Any exit codes greater than 0 mean some kind of error happened, which is specific to the program. Above you saw that grep uses 1 to mean the string was not found.

Page 10: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

 Previous Next 

9.4.2 ConditionalsNow that you can look at and set variables, it is time to make your script do different functions based on tests, called branching. The if statement is the basic operator to implement branching.A basic if statement looks like this:

if somecommand; then

# do this if somecommand has an exit code of 0

fi

The next example will run “somecommand” (actually, everything up to the semicolon) and if the exit code is 0 then the contents up until the closing fi will be run. Using what you know about grep, you can now write a script that does different things based on the presence of a string in the password file:

#!/bin/bash

if grep -q root /etc/passwd; then

echo root is in the password file

else

echo root is missing from the password file

fi

From previous examples, you might remember that the exit code of grep is 0 if the string is found. The example above uses this in one line to print a message if root is in the password or a different message if it isn’t. The difference here is that instead of an fi to close off the if block, there’s anelse. This lets you do one action if the condition is true, and another if the condition is false. Theelse block must still be closed with the fi keyword.

Other common tasks are to look for the presence of a file or directory and to compare strings and numbers. You might initialize a log file if it doesn’t exist, or compare the number of lines in a file to the last time you ran it. The if command is clearly the one to help here, but what command do you use to make the comparison?

Page 11: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

The test command gives you easy access to comparison and file test operators. For example:

Command Description

test –f /dev/ttyS0 0 if the file exists

test ! –f /dev/ttyS0 0 if the file doesn’t exist

test –d /tmp 0 if the directory exists

test –x `which ls` substitute the location of ls then test if the user can execute

test 1 –eq 1 0 if numeric comparison succeeds

test ! 1 –eq 1 NOT – 0 if the comparison fails

test 1 –ne 1 Easier, test for numeric inequality

test “a” = “a” 0 if the string comparison succeeds

test “a” != “a” 0 if the strings are different

test 1 –eq 1 –o 2 –eq 2 -o is OR: either can be the same

test 1 –eq 1 –a 2 –eq 2 -a is AND: both must be the same

It is important to note that test looks at integer and string comparisons differently. 01 and 1 are the same by numeric comparison, but not by string comparison. You must always be careful to remember what kind of input you expect.There are many more tests, such as –gt for greater than, ways to test if one file is newer than the other, and many more. Consult the test man page for more.

Page 12: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

test is fairly verbose for a command that gets used so frequently, so there is an alias for it called [(left square bracket). If you enclose your conditions in square brackets, it’s the same as running test. So, these statements are identical.

if test –f /tmp/foo; then

if [ -f /tmp/foo]; then

While the latter form is most often used, it is important to understand that the square bracket is a command on its own that operates similarly to test except that it requires the closing square bracket.The if statement has a final form that lets you do multiple comparisons at one time using elif(short for else if).

#!/bin/bash

if [ "$1" = "hello" ]; then

echo "hello yourself"

elif [ "$1" = "goodbye" ]; then

echo "nice to have met you"

echo "I hope to see you again"

else

echo "I didn't understand that"

fi

The code above compares the first argument passed to the script. If it is hello, the first block is executed. If not, the script checks to see if it is goodbye and echos a different message if so. Otherwise, a third message is sent. Note that the $1 variable is quoted and the string comparison operator is used instead of the numeric version (-eq).The if/elif/else tests can become quite verbose and complicated. The case statement provides a different way of making multiple tests easier.

#!/bin/bash

case "$1" in

hello|hi)

echo "hello yourself"

;;

Page 13: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

goodbye)

echo "nice to have met you"

echo "I hope to see you again"

;;

*)

echo "I didn't understand that"

esac

The case statement starts off with a description of the expression being tested: case EXPRESSIONin. The expression here is the quoted $1.

Next, each set of tests are executed as a pattern match terminated by a closing parenthesis. The previous example first looks for hello or hi; multiple options are separated by the vertical bar |which is an OR operator in many programming languages. Following that are the commands to be executed if the pattern returns true, which are terminated by two semicolons. The pattern repeats.The * pattern is the same as an else because it matches anything. The behavior of the casestatement is similar to the if/elif/else statement in that processing stops after the first match. If none of the other options matched the * ensures that the last one will match.

With a solid understanding of conditionals you can have your scripts take actions only if necessary.

 Previous Next 

9.4.3 LoopsLoops allow code to be executed repeatedly. They can be useful in numerous situations, such as when you want to run the same commands over each file in a directory, or repeat some action 100 times. There are two main loops in shell scripts: the for loop and the while loop.

for loops are used when you have a finite collection over which you want to iterate, such as a list of files, or a list of server names:

#!/bin/bash

Page 14: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

SERVERS="servera serverb serverc"

for S in $SERVERS; do

echo "Doing something to $S"

done

The script first sets a variable containing a space separated list of server names. The for statement then loops over the list of servers, each time it sets the S variable to the current server name. The choice of S was arbitrary, but note that the S has no dollar sign but the $SERVERS does, showing that $SERVERS will be expanded to the list of servers. The list does not have to be a variable. This example shows two more ways to pass a list.

#!/bin/bash

for NAME in Sean Jon Isaac David; do

echo "Hello $NAME"

done

for S in *; do

echo "Doing something to $S"

done

The first loop is functionally the same as the previous example, except that the list is passed to thefor loop directly instead of using a variable. Using a variable helps the clarity of the script as someone can easily make changes to the variable rather than looking at a loop.The second loop uses a * which is a file glob. This gets expanded by the shell to all the files in the current directory.The other type of loop, a while loop, operates on a list of unknown size. Its job is to keep running and on each iteration perform a test to see if it should run another time. You can think of it as “while some condition is true, do stuff.”

#!/bin/bash

i=0

while [ $i -lt 10 ]; do

echo $i

Page 15: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

i=$(( $i + 1))

done

echo “Done counting”

The example above shows a while loop that counts from 0 to 9. A counter variable, i, is initialized to 0. Then a while loop is run with the test being “is $i less than 10?” Note that the whileloop uses the same notation as an if statement!

Within the while loop the current value of i is echoed and then 1 is added to it through the $((arithmetic )) command and assigned back into i. Once i becomes 10 the while statement returns false and processing continues after the loop.

 Previous Next 

9.1 IntroductionThis is Lab 9: Scripting Commands of the Linux Essentials. By performing this lab, students will learn how to use the vi editor to create basic shell scripts using basic shell commands, variables and control statements.

In this lab, you will perform the following tasks:

Use the vi editor to create and edit text files. Create simple shell scripts. Create shell scripts with conditional execution. Use loops in the script for repetition.

 Previous Next 

9.2 Basic Text EditingMost distributions of Linux have more than one text editor. These may include simple text-only editors, such as nano, or graphical editors, such as gedit.

In this task, you will explore some of the basic text editing features of the vi editor. All distributions have some version of vi. The vi editor is a powerful text editor

Page 16: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

with a bit of a learning curve, but capable of performing a wide variety of text editing tasks.The vi editor has two modes: insert and command. In insert mode, you add text to a document. In command mode, operations can be performed such as navigation, searching, saving, and exiting the editor.

 Previous Next 

9.2.1 Step 1To create a new file, execute the following command:

vi myfile

Type an i to enter the “insert” mode of vi (more about this later). Then enter the following text:

Welcome to the vi editor.

It is a very powerful text editor.

Especially for those who master it.

Then press the Esc key to leave insert mode. Type :wq to write the file to disk and quit.

Note: Each of the previous commands will be covered in greater detail later in this lab. The purpose of the previous step was to create a file to work with during this lab.

 Previous Next 

9.2.2 Step 2Invoke the vi editor to modify the file you created. When vi is invoked ,you are placed in commandmode by default:

vi myfile

Page 17: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

Your output should be similar to the following:

Welcome to the vi editor.

It is a very powerful text editor.

Especially for those who master it.

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

"myfile" 3 lines, 97 characters

Notice in the bottom-left, the file name, the number of lines, and the number of characters in the file are displayed.

 Previous Next 

9.2.3 Step 3

Page 18: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

Press each of the following keys two times and observe how the cursor moves. Remember that you are in command mode:

Key Function

j Moves cursor down one line (same as down arrow)

k Moves cursor up line (same as up arrow)

l Moves cursor to the right one character (same as right arrow)

h Moves cursor to the left one character (same as left arrow)

w Moves cursor to beginning of next word

e Moves cursor to end of word

b Moves cursor to beginning of previous word

Warning: If you type any other keys then those listed above, you may end up in insert mode. Don't panic! Press the Esc key, then :q! + the Enter key. This should exit vi without saving any changes. Then execute vi myfile and you are back in the vi editor!

 Previous Next 

9.2.4 Step 4More vi cursor navigation: Press the following keys and observe how the cursor moves:

Page 19: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

Keys Function

$ Moves cursor to end of current line (same as End key)

0 (zero) Moves cursor beginning of current line (same as Home key)

3G Jumps to third line (nG jumps to the nth line)

1G Jumps to first line

Shift+G Jumps to the last line

.2.5 Step 5Move the cursor to the beginning of the word "very" by pressing the following keys:

G

k

8l (that's the number eight followed by the letter "l")

The cursor should be on the letter v of the word "very" as shown below:

Welcome to the vi editor.

It is a very powerful text editor.

Especially for those who master it.

~

~

 Previous Next 

9.2.7 Step 7Undo the last operation:

Page 20: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

u

Your screen should look similar to the following:

Welcome to the vi editor.

It is a very powerful text editor.

Especially for those who master it.

~

~

 Previous Next 

9.2.8 Step 8Delete two words:

2dw

Your screen should look similar to the following:

Welcome to the vi editor.

It is a text editor.

Especially for those who master it.

~

~

 Previous Next 

9.2.9 Step 9Undo the last operation:

u

Page 21: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

Your screen should look similar to the following:

Welcome to the vi editor.

It is a very powerful text editor.

Especially for those who master it.

~

~

 Previous Next 

9.2.10 Step 10Delete four characters, one at a time:

xxxx

Your screen should look similar to the following:

Welcome to the vi editor.

It is a  powerful text editor.

Especially for those who master it.

~

~

 Previous Next  

9.2.11 Step 11Undo the last 4 operations and recover the deleted characters:

4u

Your screen should look similar to the following:

Page 22: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

Welcome to the vi editor.

It is a very powerful text editor.

Especially for those who master it.

~

~

 Previous Next 

9.2.12 Step 12Delete 14 characters:

14x

Your screen should look similar to the following:

Welcome to the vi editor.

It is a text editor.

Especially for those who master it.

~

~

 Previous Next 

9.2.13 Step 13Undo the last operation:

u

Your screen should look similar to the following:

Welcome to the vi editor.

It is a very powerful text editor.

Page 23: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

Especially for those who master it.

~

~

 Previous Next 

9.2.14 Step 14Delete the five characters to the left of the cursor (type 5 then Shift+x):

5X

Your screen should look similar to the following:

Welcome to the vi editor.

It very powerful text editor.

Especially for those who master it.

~

~

 Previous Next 

9.2.15 Step 15Undo the last operation:

u

Your screen should look similar to the following:

Welcome to the vi editor.

It is a very powerful text editor.

Especially for those who master it.

~

Page 24: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

~

 Previous Next 

9.2.16 Step 16Delete the current line:

dd

Your screen should look similar to the following:

Welcome to the vi editor.

Especially for those who master it.

~

~

 Previous Next 

9.2.17 Step 17Whatever was lasted deleted or yanked can be “pasted”. Paste the deleted lines below the current line:

p

Your screen should look similar to the following:

Welcome to the vi editor.

Especially for those who master it.

It is a very powerful text editor.

~

~

Page 25: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

 Previous Next 

9.2.18 Step 18Undo the last two operations:

2u

Your screen should look similar to the following:

Welcome to the vi editor.

It is a very powerful text editor.

Especially for those who master it.

~

~

 Previous Next 

9.2.19 Step 19Delete two lines, the current and the next:

2dd

Your screen should look similar to the following:

Welcome to the vi editor.

~

~

 Previous Next 

Page 26: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

9.2.20 Step 20Undo the last operation:

u

Your screen should look similar to the following:

Welcome to the vi editor.

It is a very powerful text editor.

Especially for those who master it.

~

~

 Previous Next 

9.2.21 Step 21Move to the fourth word then delete from the current position to end of line Shift+D:

4w

D

Your screen should look similar to the following:

Welcome to the vi editor.

It is a very 

Especially for those who master it.

~

~

The command d$ also deletes to end of line. The $ character, as seen earlier, advances to end of line. Thus, d$ deletes to end of line.

 Previous Next 

Page 27: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

9.2.22 Step 22Undo the last operation:

u

Your screen should look similar to the following:

Welcome to the vi editor.

It is a very powerful text editor.

Especially for those who master it.

~

~

 Previous Next 

9.2.23 Step 23Join two lines, the current and the next by typing a capital J (Shift+J):

J

Your screen should look similar to the following:

Welcome to the vi editor.

It is a very powerful text editor. Especially for those who master it.

~

~

 Previous Next 

9.2.24 Step 24Undo the last operation:

Page 28: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

u

Your screen should look similar to the following:

Welcome to the vi editor.

It is a very powerful text editor.

Especially for those who master it.

~

~

 Previous Next 

9.2.25 Step 25Copy (or “yank”) the current word:

yw

When you copy text, no change will take place on the screen.

 Previous Next 

9.2.26 Step 26Paste (or “put”) the copied word before the current cursor:

P

Your screen should look similar to the following:

Welcome to the vi editor.

It is a very powerful powerful text editor.

Especially for those who master it.

~

Page 29: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

~

 Previous Next  

9.2.27 Step 27Undo the last operation:

u

Your screen should look similar to the following:

Welcome to the vi editor.

It is a very powerful text editor.

Especially for those who master it.

~

~

 Previous Next 

9.2.28 Step 28Move to the first line, then join three lines:

1G

3J

Your screen should look similar to the following:

Welcome to the vi editor.  It is a very powerful text editor. Especially for those who master it.

~

~

Page 30: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

 Previous Next  

9.2.29 Step 29Undo the last operation:

u

Your screen should look similar to the following:

Welcome to the vi editor.

It is a very powerful text editor.

Especially for those who master it.

~

~

 Previous Next 

9.2.30 Step 30Search for and delete the word text (add a space after the word text):

:%s/text //g

Your screen should look similar to the following:

Welcome to the vi editor.

It is a very powerful text editor.

Especially for those who master it.

~

~

~

~

~

Page 31: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

:%s/text //g

 Previous Next 

9.2.31 Step 31Navigate to beginning of file, then press i to enter insert mode to add text:

Keys Function

1G Go to beginning of file (Shift+G)

i Enter insert mode

Hello and Add text to document with a space after “and”

Hello and Welcome to the vi editor.

Page 32: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

It is a very powerful text editor.

Especially for those who master it.

~

~

 Previous Next 

9.2.32 Step 32Exit insert mode and return to command mode by pressing the Escape key:

ESC

 Previous Next 

9.2.33 Step 33Move forward one space by pressing the lower case l to place the cursor on the W and toggle it to lower case by pressing the tilde (~):

Keys Function

l Lowercase ‘L’ moves forward one space

~ Shift+` changes letter to lower case

Your screen should look similar to the following:

Hello and welcome to the vi editor.

It is a very powerful text editor.

Especially for those who master it.

~

Page 33: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

~

 Previous Next 

9.2.34 Step 34Save the file. Press the Esc key to ensure you are in command mode. Then type :w and the Enterkey:

:w

 Previous Next 

9.2.35 Step 35When you press Enter to commit the change, note the message in lower left indicating the file has been written:

~

~

"myfile" 3 lines, 102 characters written

 Previous Next 

9.2.36 Step 36Navigate to the space between the word "powerful" and "editor" in the second line as shown in the image below. You could press j followed by 10l or use the arrow keys:

Page 34: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

Command Function/Keys

j Move down to second line

10l 10 followed by the lowercase ‘L’

Your screen should look similar to the following:

Hello and welcome to the vi editor.

It is a very powerful editor.

Especially for those who master it.

~

~

 Previous Next 

9.2.37 Step 37Append text to the right of the cursor by pressing the letter a. This moves the cursor to the right and enters insert mode. Type the word text followed by a space as shown in image below:

Command Function/Keys

a Enter insert mode.

text text followed by a space

Hello and welcome to the vi editor.

It is a very powerful text editor.

Especially for those who master it.

~

~

Page 35: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

 Previous Next 

9.2.38 Step 38Exit insert mode by pressing the Esc key.

 Previous Next 

9.2.39 Step 39Open a blank line below the current line by typing a lowercase letter o:

o

Your screen should look similar to the following:

Hello and welcome to the vi editor.

It is a very powerful text editor.

_

Especially for those who master it.

~

~

 Previous Next 

9.2.40 Step 40Enter the following text:

This line added by pressing lowercase o.

Your screen should look similar to the following:

Page 36: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

Hello and welcome to the vi editor.

It is a very powerful text editor.

This line was added by pressing lowercase o. 

Especially for those who master it.

~

~

 Previous Next  

9.2.41 Step 41Exit insert mode by pressing the Esc key.

 Previous Next 

9.2.42 Step 42Open a blank line above the current line by pressing uppercase O:

O

Your screen should look similar to the following:

Hello and welcome to the vi editor.

It is a very powerful text editor.

This line was added by pressing lowercase o.

Especially for those who master it.

~

~

 Previous

Page 37: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

Next 

9.2.43 Step 43Enter the following text:

You just pressed O to open a line above.

Your screen should look similar to the following:

Hello and welcome to the vi editor.

It is a very powerful text editor.

You just pressed O to open a line above. 

This line was added by pressing lowercase o.

Especially for those who master it.

~

~

 Previous Next  

9.2.44 Step 44Exit insert mode by pressing the Esc key.

 Previous Next 

9.2.45 Step 45Save the file and close the vi editor using any one of the following methods that saves changes:

Page 38: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

Command Function/Keys

:x Will save and close the file.

:wq Will write to file and quit.

:wq! Will write to a read-only file, if possible, and quit.

ZZ Will save and close. Notice that no colon : is used in this case.

:q! Exit without saving changes

:e! Discard changes and reload file

:w! Write to read-only, if possible.

 Previous Next 

9.2.46 Step 46Once again open myfile using the vi editor:

vi myfile

 Previous Next 

9.2.47 Step 47Navigate to the third line, delete the third and fourth lines:

Page 39: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

3G

2dd

Your screen should look similar to the following:

Hello and welcome to the vi editor.

It is a very powerful text editor.

Especially for those who master it.

~

~

 Previous Next 

9.2.48 Step 48Press the Esc key to confirm you are in command mode.

 Previous Next 

9.2.49 Step 49Quit the vi editor without saving your changes:

:q!

 Previous Next 

9.2.50 Step 50Open myfile with the vi editor:

Page 40: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

vi myfile

Notice that lines 3 and 4 are still present.

 Previous Next 

9.2.51 Step 51Search forward for the word line. You’ll notice the cursor moves to the beginning of the first instance of the word line as shown in image below:

/line

Your screen should look similar to the following:

Hello and welcome to the vi editor.

It is a very powerful text editor.

You just pressed O to open a line above.

This line was added by pressing lowercase o.

Especially for those who master it.

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

Page 41: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

~

~

~

/ line

 Previous Next 

9.2.52 Step 52Search for the next instance of the word line by pressing the letter n:

n

Your screen should look similar to the following:

Hello and welcome to the vi editor.

It is a very powerful text editor.

You just pressed O to open a line above.

This line was added by pressing lowercase o.

Especially for those who master it.

~

~

~

~

~

~

~

~

~

~

~

~

~

~

Page 42: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

~

~

~

~

/ line

 Previous Next 

9.2.53 Step 53Search backward for the word line. You’ll notice the cursor moves to the beginning of the previous instance of the word line as shown in image below:

?line

Your screen should look similar to the following:

Hello and welcome to the vi editor.

It is a very powerful text editor.

You just pressed O to open a line above.

This line was added by pressing lowercase o.

Especially for those who master it.

~

~

~

~

~

~

~

~

~

~

~

~

Page 43: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

~

~

~

~

~

~

?line

 Previous Next 

9.2.54 Step 54Search for the previous instance of the word line by pressing the letter n. Since there are none in this direction, vi will wrap around the document:

n

Your screen should look similar to the following:

Hello and welcome to the vi editor.

It is a very powerful text editor.

You just pressed O to open a line above.

This line was added by pressing lowercase o.

Especially for those who master it.

~

~

~

~

~

~

~

~

~

~

Page 44: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

~

~

~

~

~

~

~

~

search hit TOP, continuing at BOTTOM

 Previous Next  

9.2.55 Step 55You will replace the word line with the word entry. When you press cw the word line will disappear and you will be in insert mode:

cw

entry

Your screen should look similar to the following:

Hello and welcome to the vi editor.

It is a very powerful text editor.

You just pressed O to open a line above.

This entry was added by pressing lowercase o.

Especially for those who master it.

~

~

 Previous Next 

Page 45: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

9.2.56 Step 56Press Esc key to exit insert mode.

 Previous Next 

9.2.57 Step 57Add text at the beginning of a line. Enter insert mode again and add a line by pressing upper case i:

I

Your screen should look similar to the following:

Hello and welcome to the vi editor.

It is a very powerful text editor.

You just pressed O to open a line above.

This entry was added by pressing lowercase o.

Especially for those who master it.

~

~

Insert modes include: i, I, a, A, o, and O.

 Previous Next 

9.2.58 Step 58Press the Esc key to return to command mode.

 Previous Next 

Page 46: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

9.2.59 Step 59Add text at the end of a line (uppercase A). First move to the second line and add the phraseIndeed!:

2G

A

[Space]Indeed!

Press the Esc key to return to command mode.

Your screen should look similar to the following:

Hello and welcome to the vi editor.

It is a very powerful text editor. Indeed!

You just pressed O to open a line above.

This entry was added by pressing lowercase o.

Especially for those who master it.

~

~

 Previous Next 

9.2.60 Step 60Save your changes and exit vi:

:x

 Previous Next 

9.3 Basic Shell Scripting

Page 47: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

Shell scripting allows you to take a complex sequence of commands, place them into a file and then run the file as a program. This saves you the time of having to repeatedly type a long sequence of commands that you routinely use.

This lab will focus on how to create simple shell scripts. For the purpose of this lab, it is assumed that you know how to use a text editor. Feel free to use the editor of your choice: vi, nano, gedit or any other editor that you like.

 Previous Next 

9.3.1 Step 1To create a simple shell script, you just need to create a text file and add commands. Create a file called sample.sh and add the following lines:

echo "Hello there! Here is the calendar for this month:"

cal

echo "Hello there! Here is the calendar for this month:"

cal

~

~

 Previous Next 

9.3.2 Step 2To make it clear that this is a BASH shell script, you need to include a special line at the top of the file called a "shbang" (or "shebang"). This line starts with #! and then contains the path to the BASH shell executable. Add the following line at the top of the sample.sh file:

#!/bin/bash

#!/bin/bash

echo "Hello there! Here is the calendar for this month:"

cal

Page 48: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

~

~

 Previous Next 

9.3.3 Step 3One way that you can run this program is by typing bash before the filename. Execute the following:

bash sample.sh

sysadmin@localhost:~$ bash sample.sh

Hello there! Here is the calendar for this month:

April 2016

Su Mo Tu We Th Fr Sa

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

sysadmin@localhost:~$

 Previous Next 

9.3.4 Step 4You can avoid having to type bash in front of the filename by making the file "executable" for all users. Run the following commands:

ls -l sample.sh

chmod a+x sample.sh

Page 49: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

ls -l sample.sh

./sample.sh

sysadmin@localhost:~$ ls -l sample.sh

-rw-rw-r-- 1 sysadmin sysadmin 73 Apr 9 22:44 sample.sh

sysadmin@localhost:~$ chmod a+x sample.sh

sysadmin@localhost:~$ ./sample.sh

Hello there! Here is the calendar for this month:

April 2016

Su Mo Tu We Th Fr Sa

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

sysadmin@localhost:~$

The chmod command is used to change permissions on the file so that the file can be executed.

 Previous Next 

9.3.5 Step 5A common feature used in scripting is "backquoting". With this technique, you can run a shell command "within" another shell command. The outcome of the internal command will be returned as an argument to the external command. Add the following to the bottom of the sample.sh file:

echo "Today is" `date +%A`

Now execute it:

cat sample.sh

./sample.sh

Page 50: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

sysadmin@localhost:~$ cat sample.sh

#!/bin/bash

echo "Hello there! Here is the calendar for this month:"

cal

echo "Today is" `date +%A`

sysadmin@localhost:~$ ./sample.sh

Hello there! Here is the calendar for this month:

April 2016

Su Mo Tu We Th Fr Sa

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

Today is Friday

sysadmin@localhost:~$

 Previous Next 

9.3.6 Step 6You have been using ./ in front of the sample.sh filename to indicate that the file is in the current directory. Execute the following to see how the shell would fail to find the file if you don't use the ./:

sample.sh

Your screen should look like the following:

sysadmin@localhost:~$ sample.sh

-bash: sample.sh: command not found

sysadmin@localhost:~$

Page 51: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

 Previous Next 

9.3.7 Step 7Recall that the $PATH variable is used to search for commands that you type. Execute the following to see the $PATH variable for the sysadmin account:

echo $PATH

sysadmin@localhost:~$ echo $PATH

/home/sysadmin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

sysadmin@localhost:~$

 Previous Next 

9.3.8 Step 8Note that /home/sysadmin/bin is one of the directories in the $PATH variable. This is a great place to put your shell scripts:

mkdir bin

mv sample.sh bin

sample.sh

sysadmin@localhost:~$ mkdir bin

sysadmin@localhost:~$ mv sample.sh bin

sysadmin@localhost:~$ sample.sh

Hello there! Here is the calendar for this month:

April 2016

Su Mo Tu We Th Fr Sa

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

Page 52: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

24 25 26 27 28 29 30

Today is Friday

sysadmin@localhost:~$

 Previous Next 

9.4 Conditional and Repetitive ExecutionNote that during this section examples that are more complex will be demonstrated. When doing so, you will be using a technique to describe what is happening in the program. The technique will look like the following:

Enter this column into drive.sh This column describes the code (don't enter into the file)

echo "Please enter your age"

# print a prompt

read age # read user input and place in $age variable

When following the instructions provided, you are to enter the text from the left column into the specified file (drive.sh in the example above). The right column is used to describe specific lines in the program. The pound sign # character is used because in a shell script, you can place comments within your program by using a # character.

 Previous Next 

9.4.1 Step 1Scripts that are more complex may make use of conditional execution. A conditional expression, like the if statement, can make use of the outcome of a

Page 53: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

command called test. The test statement compares two numbers (or two strings) for things like "equal to", "less than", etc.Create the following file (drive.sh) and make it executable to see how the if and teststatements work. Begin by placing the following in drive.sh:

Enter this column into drive.sh This column describes the code (don't enter into the file)

#!/bin/bash

echo "Please enter your age"

# print a prompt

read age # read user input and place in the $age variable

if test $age -lt 16 # test $age -lt 16 returns "true" if $age is numerically less then 16

then

  echo "You are not old enough to drive."

# executes when test is true

else

  echo "You can drive!" # executes when test is false

fi # This ends the if statement

Then make the file executable and run it:

cat drive.sh

chmod a+x drive.sh

./drive.sh

Your screen should look like the following:

Page 54: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

sysadmin@localhost:~$ cat drive.sh

#!/bin/bash

echo "Please enter your age"

read age

if test $age -lt 16

then

echo "You are not old enough to drive."

else

echo "You can drive!"

fi

sysadmin@localhost:~$ chmod a+x drive.sh

sysadmin@localhost:~$ ./drive.sh

Please enter your age

14

You are not old enough to drive.

sysadmin@localhost:~$

Verbally, you could read the if statement as "If $age is less than 16, then echo 'You are not old enough to drive', else echo 'You can drive!'". The fi ends the if statement.Note: $age must be an integer value. If not, the program will crash.

 Previous Next 

9.4.2 Step 2The test statement is automatically called when you place its arguments within square brackets [ ]surrounded by spaces. Modify the if line of drive.sh so it looks like the following:

if [ $age -lt 16 ]

Then run the program again:

cat drive.sh

Page 55: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

./drive.sh

Your screen should look like the following:

sysadmin@localhost:~$ cat drive.sh

#!/bin/bash

echo "Please enter your age"

read age

if [ $age -lt 16 ]

then

echo "You are not old enough to drive."

else

echo "You can drive!"

fi

sysadmin@localhost:~$ ./drive.sh

Please enter your age

21

You can drive!

sysadmin@localhost:~$

To see a full list of test conditions, run the command man test.Important: There must be spaces around the square brackets. [$age -lt 16] would fail, but[ $age -lt 16 ] would work.

 Previous Next 

9.4.3 Step 3You can also use the outcome of other shell commands as they all return "success" or "failure". For example, create and run the following program, which can be used to determine if a user account is on this system. Add the following to check.sh:

#!/bin/bash

echo "Enter a username to check: "

read name

Page 56: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

if grep $name /etc/passwd > /dev/null

then

echo "$name is on this system"

else

echo "$name does not exist"

fi

Then run the following commands:

chmod a+x check.sh

./check.sh

When prompted for a username, give the value of "root". Execute the command again (./check.sh) and provide the value of "bobby". Your screen should look like the following:

sysadmin@localhost:~$ cat check.sh

#!/bin/bash

echo "Enter a username to check: "

read name

if grep $name /etc/passwd > /dev/null

then

echo "$name is on this system"

else

echo "$name does not exist"

fi

sysadmin@localhost:~$ chmod a+x check.sh

sysadmin@localhost:~$ ./check.sh

Enter a username to check:

root

root is on this system

sysadmin@localhost:~$ ./check.sh

Enter a username to check:

bobby

bobby does not exist

sysadmin@localhost:~$

Page 57: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

 Previous Next 

9.4.4 Step 4Another common conditional statement is called the while loop. This statement is used to execute code repeatedly as long as a conditional check returns "true". Begin by placing the following in a file named num.sh:

Enter this column into num.sh This column describes the code (don't enter into the file)

#!/bin/bash

echo "Please enter a number greater than 100"

read num

while [ $num -le 100 ] # Execute code from "do" to "done if test condition is true

do

  echo "$num is NOT greater than 100."

  echo "Please enter a number greater than 100"

  read num

done # This ends the done statement

echo "Finally, $num is greater than 100"

Page 58: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

Then make the file executable and run it:

chmod a+x num.sh

./num.sh

When prompted for a number, enter 25. When prompted again, enter 99. Finally, enter 101 when prompted for a number the third time. Your screen should look like the following:

sysadmin@localhost:~$ cat num.sh

#!/bin/bash

echo "Please enter a number greater than 100"

read num

while [ $num -le 100 ]

do

echo "$num is NOT greater than 100."

echo "Please enter a number greater than 100."

read num

done

echo "Finally, $num is greater than 100"

sysadmin@localhost:~$ chmod a+x num.sh

sysadmin@localhost:~$ ./num.sh

Please enter a number greater than 100

25

25 is NOT greater than 100.

Please enter a number greater than 100.

99

99 is NOT greater than 100.

Please enter a number greater than 100.

101

Finally, 101 is greater than 100

sysadmin@localhost:~$

If the conditional check for the while statement ( [ $num -le 100 ] ) returns true, then the statements between do and done are executed.

Page 59: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

Once those statements have completed executing, the conditional check for the while statement is checked again. If true again, then again the statements between do and done are executed.

This will continue repeating until the while condition returns false, or when the value is greater than 100.

 Previous Next 

9.4.5 Step 5Scripting code is part of the BASH shell, which means you can use these statements on the command line just like you use them in a shell script. This can be useful for a statement like the for statement, a statement that will assign a list of values one at a time to a variable. This allows you to perform a set of operations on each value. For example, run the following on the command line:

for name in /etc/passwd /etc/hosts /etc/group

do

wc $name

done

Your screen should look like the following:

sysadmin@localhost:~$ for name in /etc/passwd /etc/hosts /etc/group

> do

> wc $name

> done

24 30 1001 /etc/passwd

7 15 161 /etc/hosts

46 46 561 /etc/group

sysadmin@localhost:~$

Note that the wc command was run three times: once for /etc/passwd, once for /etc/hostsand once for /etc/group.

 Previous

Page 60: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

Next 

9.4.6 Step 6Often the seq command is used in conjunction with the for statement. The seq command can generate a list of integer values, for instance from 1 to 10. For example, run the following on the command line to create 12 files named test1, test2, test3, etc. (up to test12):

ls

for num in `seq 1 12`

do

touch test$num

done

ls

sysadmin@localhost:~$ ls

Desktop Downloads Pictures Templates check.sh num.sh

Documents Music Public Videos drive.sh

sysadmin@localhost:~$ for num in `seq 1 12`

> do

> touch test$num

> done

sysadmin@localhost:~$ ls Desktop Music Templates drive.sh test10 test2 test5 test8 Documents Pictures Videos num.sh test11 test3 test6 test9 Downloads Public check.sh test1 test12 test4 test7

sysadmin@localhost:~$

 Previous Next 

10.1 IntroductionOne of the many advantages of having so many different Linux distributions is that some of them are designed to run on specific hardware platforms. In fact, there is a

Page 61: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

Linux distribution specifically designed for just about all modern hardware platforms.

Each of these hardware platforms has a great deal of variety in the hardware components that are available. In addition to several different types of hard drives, there are many different monitors and printers. With the popularity of USB devices, such as USB storage devices, cameras and cell phones, the number of available devices numbers in the thousands.

In some cases, this poses problems as these hardware devices typically need some sort of software (called drivers or modules) that allows them to communicate with the installed operating system. Hardware manufactures often provide this software, but typically for Microsoft Windows, not Linux. The Linux mascot,Tux, is however starting to appear more often on hardware products, indicating Linux support.

In addition to vendor support, there is a great deal of community support that strives to provide drivers for Linux systems. Although not all hardware has the necessary drivers, there is a good amount that does, making the challenge for Linux users and administrators to either find the correct drivers or choose hardware that has some level of support in Linux.

In this chapter, you will learn about core hardware devices, including how to use Linux commands to display vital hardware device information.

10.2 ProcessorsA Central Processing Unit (CPU or processor) is one of the most important hardware components of a computer. It performs the decision making as well as the calculations that need to be performed to properly run an operating system. The processor is essentially a computer chip.

The processor is connected to the other hardware via a motherboard, also known as the system board. Motherboards are designed to work with specific types of processors.

If a hardware system has more than one processor, the system is referred to as a multiprocessor. If more than one processor is combined into a single processor chip, then it is called multi-core.

Although support is available for more types of processors in Linux than any other operating system, there are primarily just two types of processors used on desktop and server computers: x86 and x86_64. On an x86 system, the system processes data 32 bits at a time; on a x86_64 the system processes data 64 bits at a time. A x86_64 system is also capable of also processing data 32 bits at a time in a backward compatible mode. One of the main advantages to a 64 bit system is that the system is able to work with more memory.

Page 62: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

The x86 family of processors was originated by Intel in 1978 with the release of the 8086 processor. Since that time, Intel has produced many other processors that are improvements to the original 8086; they are known generically as x86 processors. These processors include the 80386 (also known as the i386), 80486 (i486), the Pentium series (i586) and the Pentium Pro series ( i686). In addition to Intel, other companies like AMD and Cyrix have also produced x86 compatible processors. While Linux is capable of supporting processors back to the i386 generation, many distributions limit their support to i686 or later.

The x86_64 family of processors, including the 64 bit processors from Intel and AMD, have been in production since around the year 2000. As a result, most of the modern processors built today are x86_64. While the hardware has been available for over a decade now, the software to support this family of processors has been much slower to develop. Even as of 2013, there are many software packages that are available for the x86 architecture, but not the x86_64.You can see the family your CPU belongs to, using the arch command:

sysadmin@localhost:~$ arch

x86_64

sysadmin@localhost:~$

Another command you can use to identify the type of CPU in your system is the lscpu command:

sysadmin@localhost:~$ lscpu

Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 4 On-line CPU(s) list: 0-3 Thread(s) per core: 1 Core(s) per socket: 4 Socket(s): 1 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 44 Stepping: 2 CPU MHz: 2394.000 BogoMIPS: 4788.00 Virtualization: VT-x Hypervisor vendor: VMware Virtualization type: full L1d cache: 32K L1i cache: 32K L2 cache: 256K L3 cache: 12288K

Page 63: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

NUMA node0 CPU(s): 0-3 sysadmin@localhost:~$

The first line of this output shows that the CPU is being used in a 32 bit mode, as the architecture reported is i686. The second line of output shows that the CPU is capable of operating in either a 32 or 64 bit mode, therefore it is actually a 64 bit CPU.

The most detailed way of displaying information about your CPU(s) is viewing the /proc/cpuinfo file with the cat command:

sysadmin@localhost:~$ cat /proc/cpuinfo

processor : 0

vendor_id : GenuineIntel

cpu family : 6

model : 44

model name : Intel(R) Xeon(R) CPU E5620 @ 2.40GHz

stepping : 2

microcode : 0x15

cpu MHz : 2394.000

cache size : 12288 KB

physical id : 0

siblings : 4

core id : 0

cpu cores : 4

apicid : 0

initial apicid : 0

fpu : yes

fpu_exception : yes

cpuid level : 11

wp : yes

flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov

pat pse36 clflush dts mmx fxsr sse sse2 ss ht syscall nx rdtscp lm constant_tsarch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf pni pcl mulqdq vmx ssse3 cx16 sse4_1 sse4_2 x2apic popcnt aes hypervisor lahf_lm ida arat dtherm tpr_shadow vnmi ept vpid

Page 64: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

While much of the output of the lscpu and the contents of the /proc/cpuinfo file appears to be the same, one benefit to viewing the /proc/cpuinfo file is that the flags of the CPU are displayed. Theflags of a CPU are a very important component, since they indicate which features the CPU supports and the capabilities of the CPU.For example, the output from the previous example contains the flag lm (long mode), indicating that this CPU is 64-bit capable. There are also flags that indicated if the CPU is capable of supporting virtual machines (the ability to have multiple operating systems on a single computer).

 Previous Next  

10.3 Motherboards and BusesThe motherboard, or system board, is the main hardware board in the computer through which the CPU, Random Access Memory (RAM) and other components are all connected together. Some devices are attached directly to the motherboard, while other devices are connected via a bus to the motherboard.

 Previous Next 

10.3.1 dmidecodeThe system board of many computers contains what is known as Basic Input and Output System (BIOS).System Management BIOS (SMBIOS) is the standard that defines the data structures and how to communicate information about computer hardware. The dmidecode command is able to read and display the information from SMBIOS.

For devices directly attached to the motherboard, an administrator can use the dmidecode command to view them. There is a great deal of information provided by the output of this command.

The examples below provide you with a few ideas of what you can learn from the output of thedmidecode command. This command is not available within the virtual machine environment of this course.

Page 65: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

In the first example, you can see that the BIOS supports booting directly from the CD-ROM. This is important since operating system installs are often done by booting directly from the install CD:

# dmidecode 2.11

SMBIOS 2.4 present.

364 structures occupying 16040 bytes.

Table at 0x000E0010

Handle 0x0000, DMI type 0, 24 bytes

BIOS InformationVendor: Phoenix Technologies LTDVersion: 6.00Release Date: 06/22/2012Address: 0xEA0C0Runtime Size: 89920 bytesROM Size: 64 kBCharacteristics:

ISA is supportedPCI is supportedPC Card (PCMCIA) is supportedPNP is supportedAPM is supportedBIOS is upgradeableBIOS shadowing is allowedESCD support is availableBoot from CD is supported

--More--

In the next example, you can see that a total of 2048 (about 2GB) of RAM is installed on the system:

Socket Designation: RAM socket #0

Bank Connections: None

Current Speed: Unknown

Type: EDO DIMM

Installed Size: 2048 MB (Single-bank Connection)Enabled Size: 2048 MB (Single-bank Connection)Error Status: OK

 Previous Next  

Page 66: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

10.3.2 Random Access MemoryThe motherboard normally has slots where Random Access Memory (RAM) can be connected to the system. 32 bit architecture systems can use up to 4 gigabytes (GB) of RAM, while 64 bit architectures are capable of addressing and using far more RAM.

In some cases, the RAM your system has might not be enough to handle all of the operating system requirements. Each program needs to store data in RAM and the programs themselves are loaded into RAM when they execute.

To avoid having the system fail due to a lack of RAM, virtual RAM (or swap space) is utilized. Virtual RAM is hard drive space that is used to temporarily store RAM data when the system is running out of RAM. Data that is stored in RAM and that has not been used recently is copied on to the hard drive so currently active programs can use the RAM. If needed, this swapped data can be stored back into RAM at a later time.

To view the amount of RAM in your system, including the virtual RAM, execute the free command. Thefree command has a -m option to force the output to be rounded to the nearest megabyte and a -g option to force the output to be rounded to the nearest gigabyte:

sysadmin@localhost:~$ free -m

total used free shared buffers cached

Mem: 1894 356 1537 0 25 177 -/+ buffers/cache: 153 1741 Swap: 4063 0 4063 sysadmin@localhost:~$

The output of executing this free command shows that the system it was executed on a system has a total of 1,894 megabytes and is currently using 356 megabytes.

The amount of swap appears to be approximately 4 gigabytes, although none of it appears to be in use. This makes sense because so much of the physical RAM is free, so there is no need at this time for virtual RAM to be used.

 Previous Next  

10.3.3 Peripheral DevicesThe motherboard has buses that allow for multiple devices to connect to the system, including the Peripheral Component Interconnect (PCI) and Universal Serial

Page 67: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

Bus (USB). The motherboard also has connectors for monitors, keyboards and mice.

In order to view all of the devices connected by the PCI bus, execute the lspci command. The following is a sample output of this command. As you can see below in the highlighted sections, this system has a VGA controller (a monitor connector), a SCSI storage controller (a type of hard drive) and an Ethernet controller (a network connector):The graphics below provide examples of using the lspci command. This command is not available within the virtual machine environment of this course.sysadmin@localhost:~$ lspci

00:00.0 Host bridge: Intel Corporation 440BX/ZX/DX - 82443BX/ZX/DX Host bridge (rev 01)

00:01.0 PCI bridge: Intel Corporation 440BX/ZX/DX - 82443BX/ZX/DX AGP bridge (rev 01)

00:07.0 ISA bridge: Intel Corporation 82371AB/EB/MB PIIX4 ISA (rev 08)

00:07.1 IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)

00:07.3 Bridge: Intel Corporation 82371AB/EB/MB PIIX4 ACPI (rev 08)

00:07.7 System peripheral: VMware Virtual Machine Communication Interface (rev 10)

00:0f.0 VGA compatible controller: VMware SVGA II Adapter 03:00.0 Serial Attached SCSI controller: VMware PVSCSI SCSI Controller (rev 020b:00.0 Ethernet controller: VMware VMXNET3 Ethernet Controller (rev 01)

Executing the lspci command with the -nn option shows both a numeric identifier for each device, as well as the original text description:

sysadmin@localhost:~$ lspci -nn

00:00.0 Host bridge [0600]: Intel Corporation 440BX/ZX/DX - 82443BX/ZX/DX Host bridge [8086:7190] (rev 01)

00:01.0 PCI bridge [0604]: Intel Corporation 440BX/ZX/DX - 82443BX/ZX/DX AGP bridge [8086:7191] (rev 01)

00:07.0 ISA bridge [0601]: Intel Corporation 82371AB/EB/MB PIIX4 ISA [8086:7110](rev 08)

00:07.1 IDE interface [0101]: Intel Corporation 82371AB/EB/MB PIIX4 IDE [8086:7111] (rev 01)

00:07.3 Bridge [0680]: Intel Corporation 82371AB/EB/MB PIIX4 ACPI [8086:7113](rev 08)

00:07.7 System peripheral [0880]: VMware Virtual Machine Communication Interface [15ad:0740] (rev 10)

Page 68: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

00:0f.0 VGA compatible controller [0300]: VMware SVGA II Adapter [15ad:0405]

03:00.0 Serial Attached SCSI controller [0107]: VMware PVSCSI SCSI Controller

[15ad:07c0] (rev 02)

0b:00.0 Ethernet controller [0200]: VMware VMXNET3 Ethernet Controller

[15ad:07b0] (rev 01)

The highlighted section, [15ad:07b0], is referred to as the [vendor:device] section.

Using the [vendor:device] information can be useful for displaying detailed information about a specific device. By using the -d vendor:device option, you can select to view information about just one device.You can also view more detailed information by using either the -v, -vv or -vvv option. The more vcharacters, the more verbose the output will be. For example:

sysadmin@localhost:~$ lspci -d 15ad:07b0 -vvv

0b:00.0 Ethernet controller: VMware VMXNET3 Ethernet Controller (rev 01)

Subsystem: VMware VMXNET3 Ethernet Controller

Physical Slot: 192

Control: I/O+ Mem+ BusMaster+ SpecCycle- MemWINV- VGASnoop- ParErr- Step

ping- SERR- FastB2B- DisINTx+

Status: Cap+ 66MHz- UDF- FastB2B- ParErr- DEVSEL=fast >TAbort- <TAbort-

<MAbort- >SERR- <PERR- INTx-

Latency: 0, Cache Line Size: 32 bytes

Interrupt: pin A routed to IRQ 19

Region 0: Memory at fd4fb000 (32-bit, non-prefetchable) [size=4K]

Region 1: Memory at fd4fc000 (32-bit, non-prefetchable) [size=4K]

Region 2: Memory at fd4fe000 (32-bit, non-prefetchable) [size=8K]

Region 3: I/O ports at 5000 [size=16]

[virtual] Expansion ROM at fd400000 [disabled] [size=64K]

Capabilities: <access denied>

Kernel driver in use: vmxnet3 Kernel modules: vmxnet3sysadmin@localhost:~$

Page 69: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

The lspci command shows detailed information about devices connected to the system via the PCI bus. This information can be helpful to determine if the device is supported by the system, as indicated by a Kernel driver or Kernel module in use, as shown in the last couple of lines of output above.

 Previous Next  

10.3.4 Universal Serial Bus DevicesWhile the PCI bus is used for many internal devices such as sound and network cards, many external devices (or peripherals) are connected to the computer via USB. Devices connected internally are usually cold-plug, meaning the system must be shut down in order to connect or disconnect a device. USB devices are hot-plug, meaning they can be connected or disconnected while the system is running.Note: The graphics below provide examples of using the lsusb command. This command is not available within the virtual machine environment of this course.To display the devices connected to the system via USB, execute the lsusb command:

sysadmin@localhost:~$ lsusb

Bus 001 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

sysadmin@localhost:~$

The verbose option, -v, for the lsusb command shows a great amount of detail about each device:

sysadmin@localhost:~$ lsusb -v

Bus 001 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

Couldn’t open device, some information will be missing

Device Descriptor:

bLength 18

bDescriptorType 1

bcdUSB 1.10

bDeviceClass 9 Hub

bDeviceSubClass 0 Unused

Page 70: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

bDeviceProtocol 0 Full speed (or root) hub

bMaxPacketSize0 64

idVendor 0x1d6b Linux Foundation

idProduct 0x0001 1.1 Linux Foundation

bcDevice 2.06

iManufacturer 3

iProduct 2

iSerial 1

 Previous Next 

10.4 Hardware Abstraction LayerHAL is the Hardware Abstraction Layer. The daemon for HAL is hald, a process that gathers information about all devices connected to the system. When events occur that change the state of the connected devices, such as when a USB device is attached to the system, then hald broadcasts this new information to any processes that have registered to be notified about new events.Note: The graphic below provides an example of using the lshal command. This command is not available within the virtual machine environment of this course.The lshal command allows you to view the devices detected by HAL. This command produces a huge amount of output; the following provides a small sample:

Page 71: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

 Previous Next 

10.5 Disk DevicesDisk devices (AKA, hard drives) may be attached to the system in a number of ways; the controller may be integrated into the motherboard, on a PCI (Peripheral Component Interconnect) card or a USB device.

Hard drives are divided into partitions. A partition is a logical division of a hard drive, designed to take a large amount of available storage space and break it up into smaller "chunks". While it is common on Microsoft Windows to have a single partition for each hard drive, on Linux distributions, multiple partitions per hard drive is common.

Some hard drives make use of a partitioning technology called Master Boot Record (MBR) while others make use of a partitioning type called GUID Partitioning Table (GPT). The MBR type of partitioning has been used since the

Page 72: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

early days of the Personal Computer (PC) and the GPT type has been available since the year 2000.

An old term used to describe an internal hard disk is "fixed disk", as the disk is fixed (not removable). This term gave rise to several command names: the fdisk, cfdisk and sfdisk commands, which are tools for working with the MBR partitioned disks.

The GPT disks use a newer type of partitioning, which allows the user to divide the disk into more partitions than what MBR supports. GPT also allows having partitions which can be larger than two terabytes (MBR does not). The tools for managing GPT disks are named similar to the fdisk counterparts: gdisk, cgdisk, andsgdisk.

There is also a family of tools that attempts to support both MBR and GPT type disks. This set of tools includes the parted command and the graphical gparted tool.

Hard drives are associated with file names (called device files) that are stored in the /dev directory. Different types of hard drives are given slightly different names: hd for IDE (Intelligent Drive Electronics) hard drives andsd for USB, SATA (Serial Advanced Technology Attachment) and SCSI (Small Computer System Interface) hard drives.

Each hard drive is assigned a letter, for example, the first IDE hard drive would have a device file name of/dev/hda and the second IDE hard drive would have be associated with the /dev/hdb device file.

Partitions are given unique numbers for each device. For example, if a USB hard drive had two partitions, they could be associated with the /dev/sda1 and /dev/sda2 device files.

In the following output, you can see that this system has three sd devices: /dev/sda, /dev/sdb and/dev/sdc. Also, you can see there are two partitions on the first device (as evidenced by the /dev/sda1and /dev/sda2 files) and one partition on the second device (as evidenced by the /dev/sdb1 file):

root@localhost:~$ ls /dev/sd*

/dev/sda /dev/sda1 /dev/sda2 /dev/sdb /dev/sdb1 /dev/sdc

root@localhost:~$

In the following example, the fdisk command is used to display partition information on the first sd device.

Note: The following command requires root accessroot@localhost:~# fdisk -l /dev/sda

Page 73: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

Disk /dev/sda: 21.5 GB, 21474836480 bytes

255 heads, 63 sectors/track, 2610 cylinders, total 41943040 sectors

Units = sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x000571a2

Device Boot Start End Blocks Id System

/dev/sda1 * 2048 39845887 19921920 83 Linux

/dev/sda2 39847934 41940991 1046529 5 Extended

/dev/sda5 39847936 41940991 1046528 82 Linux swap / Solaris

root@localhost:~#

Creating and modifying partitions is beyond the scope of this course.

 Previous Next 

10.6 Optical DisksOptical disks, often referred to as CD-ROMs, DVDs, or Blue-Ray are removable storage media. While some devices used with optical disks are read-only, others are capable of burning (writing to) disks, when using a writable type of disk. There are various standards for writable and rewritable disks, such as CD-R, CD+R, DVD+RW, and DVD-RW. These media standards go beyond the scope of the curriculum.

Where these removable disks are mounted in the file system is an important consideration for a Linux administrator. Modern distributions often mount the disks under the /media folder, while older distributions typically mount them under the /mnt folder.

Upon mounting, most GUI interfaces will prompt the user to take an action, such as open the contents of the disk in a file browser or start a media program. When the user is finished using the disk, it is prudent to unmount it using a menu or the eject command. While pressing the eject button will open the disk tray, some programs will not realize that the disk is no longer mounted on the filesystem.

Page 74: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

 Previous Next 

10.7 Video Display DevicesIn order to display video (output to the monitor), the computer system must have a video display device (AKA,video card) and a monitor. Video display devices are often directly attached to the motherboard, although they can also be connected through the PCI bus slots on the motherboard.

Unfortunately, since the early days of the PC, no video standard has been approved by the major vendors, so each video display device usually requires a proprietary driver provided by the vendor. Drivers are software programs that allow the operating system to communicate with the device.

Drivers must be written for the specific operating system, something that is commonly done for Microsoft Windows, but not always for Linux. Fortunately, the three largest video display vendors all now provide at least some level of Linux support.

There are two types of video cables commonly used: the analog 15 pin Video Graphics Array (VGA) cable and the 29 pin Digital Visual Interface (DVI).

In order for monitors to work properly with video display devices, they must be able to support the same resolution as the video display device. Normally, the software driving the video display device (commonly the X.org server) will normally be able to automatically detect the maximum resolution that the video display and monitor can both support and set the screen resolution to that value.

Graphical tools are normally provided to change your resolution, as well as the maximum number of colors that can be displayed (known as the color depth) with your Linux distribution. For distributions using the X.org server, the /etc/X11/xorg.conf file can be used to change resolution, color depth and other settings.

 Previous Next 

10.8 Managing Devices

Page 75: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

In order for a device to be used in Linux, there may be several different kinds of software required. First of all there is the driver software. The driver could be compiled as part of the Linux kernel, loaded into the kernel as a module or loaded by a user command or application. Most devices have the driver either built-in to the kernel or have it loaded into the kernel, as the driver may require the low-level kind of access that the kernel has with devices.

External devices, like scanners and printers, typically have their drivers loaded by an application and these drivers in turn communicate through the device via the kernel through an interface such as USB.

In order to be successful in enabling devices in Linux, it is best to check the Linux distribution to see if the device is certified to work with that distribution. Commercial distributions like Red Hat and SUSE have web pages dedicated to listing hardware that is certified or approved to work with their software.

Additional tips on being successful with connecting your devices: avoid brand new or highly specialized devices and check with the vendor of the device to see if they support Linux before making a purchase.

 Previous Next 

10.9 Power SuppliesPower supplies are the devices that convert alternating current (120v, 240v) into direct current, which the computer uses at various voltages (3.3v, 5v, 12v, etc.). Power supplies are generally not programmable, however their proper function has a major impact on the rest of the system.

Although they are not surge suppressors, these devices often protect the computer from fluctuations in voltage coming from the power source. It is wise for a network administrator to choose a power supply based on quality rather than price, since a failing power supply can result in major destruction to a computer system.

 Previous Next 

10.1 Introduction

Page 76: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

This is Lab 10: Understanding Computer Hardware. By performing this lab, students will learn about commands to display information about the computer's hardware.

In this lab, you will perform the following tasks:

Use commands to list hardware.

10.2 Listing Computer HardwareIn this task, you will execute some commands and examine some files to display your hardware configuration.

10.2.1 Step 1In order to determine the type of CPU execute the lscpu command:

lscpu

Your output will be similar to the following:

Being able to display CPU information can be important when trying to determine if more advanced Linux features can be used on your system. For even more details about your CPU(s), you can examine the/proc/cpuinfo file, especially, the "flags" that are listed that determine whether or not your CPU has certain features.

10.2.2 Step 2

Page 77: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

View the /proc/cpuinfo file:

cat /proc/cpuinfo

10.2.3 Step 3To discover how much RAM and swap space is being used, use the free command:

free -m

free -g

The output shows the amount of memory in megabytes when the -m option is used and in gigabytes when the -g option is used:

In the output above, you can see that the system has 16049 megabytes (roughly 15 gigabytes) of physical memory (RAM). Of that only 1066 megabytes are being used, a good sign that you have enough memory for your system's needs.

Page 78: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

In the event that you run out of memory, Swap is used. Swap is hard drive space that is used to temporarily store data that is supposed to be stored in RAM.

10.2.4 Step 4To see what devices are connected to the PCI bus, use the lspci command:

lspci

Notice from the partial output below, that many of the devices connected to the system board are displayed:

The output of the lspci command can be very important to identify devices that are not supported by the Linux kernel. Some devices like video cards may only provide basic functionality without installing proprietary driver software.

10.2.5 Step 5Use the lspci command with the -k option to show devices along with the kernel driver and modules used:

Page 79: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

10.2.6 Step 6Attempt to list the USB connected devices:

lsusb

The output of this command is unusual since no USB devices are detected:

sysadmin@localhost:~$ lsusb

unable to initialize libusb: -99

sysadmin@localhost:~$

Due to this system being virtualized, the USB devices do not appear as they normally would when executing the lsusb command. Normally, if USB devices are present, it would have shown something like this:

sysadmin@localhost:~$ lsusb

Bus 001 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

sysadmin@localhost:~$

HAL is the Hardware Abstraction Layer. The daemon for HAL is hald, which gathers information about devices connected to the system. When events occur that somehow change the state of the connected devices, then hald broadcasts this information to any processes that have registered for the events. On systems that use HAL, the lshal command will be able to list the devices of that system.

Page 80: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

 Previous Next 

10.2.6 Step 6Attempt to list the USB connected devices:

lsusb

The output of this command is unusual since no USB devices are detected:

sysadmin@localhost:~$ lsusb

unable to initialize libusb: -99

sysadmin@localhost:~$

Due to this system being virtualized, the USB devices do not appear as they normally would when executing the lsusb command. Normally, if USB devices are present, it would have shown something like this:

sysadmin@localhost:~$ lsusb

Bus 001 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

sysadmin@localhost:~$

HAL is the Hardware Abstraction Layer. The daemon for HAL is hald, which gathers information about devices connected to the system. When events occur that somehow change the state of the connected devices, then hald broadcasts this information to any processes that have registered for the events. On systems that use HAL, the lshal command will be able to list the devices of that system.

 Previous Next 

10.2.7 Step 7For hardware to function, the Linux kernel usually loads a driver or module. Use the lsmod command to view the currently loaded modules:

Page 81: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

lsmod

Partial output of the command is shown below. The first column is the module name, and the second is the amount of memory used by the module. The number in the "Used by" column indicates how many other modules are using the module. The names of the other modules using the module may also be listed in the "Used by" column, but is often incomplete:

10.2.8 Step 8The system board of many computers contains what is known as BIOS, or a Basic Input and Output System. System Management BIOS, or SMBIOS, is a standard defining the data structures and how to communicate information about computer hardware. The dmidecode command is able to read and print the information from the SMBIOS of many systems. We will not demonstrate the command here, since it does not function properly within a virtual environment.The fdisk command can be used in two ways: interactively and non-interactively.

When the -l option is used with fdisk, then the command will non-interactively list block devices, which includes disks (hard drives) and logical volumes.Without the -l option the fdisk command enters an interactive mode that is typically used to modify partitions on a disk device.

10.2.9 Step 9

Page 82: tigger.itc.mxtigger.itc.mx/conacad/cargas/EUUC700411523/FM7/linux...  · Web view2019. 8. 6. · The motherboard has buses that allow for multiple devices to connect to the system,

Execute the fdisk command to list the disk devices non-interactively, in sectors, and without DOS compatibility warnings:

fdisk -cul

The partial output of this command below shows the first couple of disks, /dev/sda and /dev/sdb, and their partitions. The third disk, /dev/sdc, and the logical volumes that followed it have been omitted: