17
CIT Linux Lab Manual 2017-08-05 1 CIT Linux Lab Manual Version 2017-08-05 CIT Linux Lab Manual .......................................................................................................................2 Introducti on .................................................................................................................................2 Linux fundamentals ......................................................................................................................2 The Linux command line ..........................................................................................................2 The shell program ....................................................................................................................2 The command prompt .............................................................................................................2 Linux Tutorials ..........................................................................................................................3 Common Linux Commands ......................................................................................................3 CIT computer a ccounts ................................................................................................................4 Using SSH to access the CIT Linux server remotely .................................................................4 Accessing the server from Windows ....................................................................................5 Accessing the server from OS X or Linux ..............................................................................5 Passwords .............................................................................................................................7 Changing your password from the command line ...........................................................7 Changing your password from a Web browser ...............................................................7 Validity .............................................................................................................................7 Automa ti c Ban......................................................................................................................7 Local Email ............................................................................................................................8 Logging out ...........................................................................................................................8 Transferring files to/from the server ...........................................................................................8 File Transfer using MobaXterm................................................................................................9 File transfer using FileZilla........................................................................................................9 Serving Web pa ges .....................................................................................................................10 Progra mmi ng tools .....................................................................................................................10 Text editors ............................................................................................................................10 Compiling and executing a C program ...................................................................................11 Compiling and executing a C++ program ...............................................................................11 Additional compiler options ...................................................................................................12 Using make to compile a C or C++ program ..........................................................................13 Compiling and executing a Java program ..............................................................................13 Editing and executing a Python program...............................................................................14 Other tools useful to programmers .......................................................................................14 Spell check your source code.............................................................................................14 Automa ti cally forma t your source code ............................................................................14 Check for memory leaks .....................................................................................................15 Analyze program execution ...............................................................................................15 Software version control ....................................................................................................16 Submitting homework assignments ..........................................................................................17

CIT Linux Lab Manual - CSN · 1 CIT Linux Lab Manual Version 2017-08-05 ... There are many different shell programs, but on most Linux systems, bash (Bourne Again SHell) is the default

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CIT Linux Lab Manual - CSN · 1 CIT Linux Lab Manual Version 2017-08-05 ... There are many different shell programs, but on most Linux systems, bash (Bourne Again SHell) is the default

CIT Linux Lab Manual

2017-08-05

1

CIT Linux Lab Manual Version 2017-08-05

CIT Linux Lab Manual .......................................................................................................................2

Introduction .................................................................................................................................2

Linux fundamentals......................................................................................................................2

The Linux command line ..........................................................................................................2

The shell program ....................................................................................................................2

The command prompt .............................................................................................................2

Linux Tutorials ..........................................................................................................................3

Common Linux Commands ......................................................................................................3

CIT computer accounts ................................................................................................................4

Using SSH to access the CIT Linux server remotely .................................................................4

Accessing the server from Windows....................................................................................5

Accessing the server from OS X or Linux..............................................................................5

Passwords.............................................................................................................................7

Changing your password from the command line...........................................................7

Changing your password from a Web browser ...............................................................7

Validity .............................................................................................................................7

Automatic Ban......................................................................................................................7

Local Email............................................................................................................................8

Logging out...........................................................................................................................8

Transferring files to/from the server ...........................................................................................8

File Transfer using MobaXterm................................................................................................9

File transfer using FileZilla........................................................................................................9

Serving Web pages.....................................................................................................................10

Programming tools.....................................................................................................................10

Text editors ............................................................................................................................10

Compiling and executing a C program ...................................................................................11

Compiling and executing a C++ program ...............................................................................11

Additional compiler options...................................................................................................12

Using make to compile a C or C++ program ..........................................................................13

Compiling and executing a Java program ..............................................................................13

Editing and executing a Python program...............................................................................14

Other tools useful to programmers .......................................................................................14

Spell check your source code.............................................................................................14

Automatically format your source code ............................................................................14

Check for memory leaks.....................................................................................................15

Analyze program execution ...............................................................................................15

Software version control....................................................................................................16

Submitting homework assignments ..........................................................................................17

Page 2: CIT Linux Lab Manual - CSN · 1 CIT Linux Lab Manual Version 2017-08-05 ... There are many different shell programs, but on most Linux systems, bash (Bourne Again SHell) is the default

CIT Linux Lab Manual

2017-08-05

2

CIT Linux Lab Manual

Introduction The following manual presents basic information necessary for s tudents utilizing the College of Southern Nevada’s (CSN) Linux Server in conjunction with a variety of CIT, CS, and IS courses. Many of the policies established in this document are security related. Effective security i s rarely convenient. The most current version of this document i s always available at http://bellagio.csn.edu/docs/labmanual.pdf.

Linux fundamentals The Linux command line There are two common kinds of user interfaces: graphical user interfaces (GUI’s) and command l ine interfaces (CLI’s). With a GUI, you control things by pointing and clicking with your mouse, and in a CLI you type commands with your keyboard. In the early days, before GUIs, the command line was the only interface available with any operating system. The CLI i s still important; a ll modern operating systems have one. For example, Windows has a CLI called cmd that i s closely related to the old DOS prompt.

Because the Linux CLI can sometimes resemble cmd, some people confuse them and think the Linux CLI i s just like Windows’ cmd. Don’t confuse these! The Linux CLI i s much more powerful than cmd.

The shell program When you type commands at the Linux command line, there’s always a program called the shell running. The shell is the program that acts as the interface between you and the kernel, the central core of the operating system (see Figure 1). When you type commands to execute various tools/applications, i t’s the shell that reads those commands from your keyboard and processes them. There are many di fferent shell programs, but on most Linux systems, bash (Bourne Again SHell) is the default shell. Other common shells include include csh, ksh, and zsh.

The command prompt When you log in, you will have a bash shell prompt in front of you. It

provides a great deal of information. Let’s deconstruct the prompt you’ll see.

Figure 1 - The shell is an interface between the user

and the kernel.

Page 3: CIT Linux Lab Manual - CSN · 1 CIT Linux Lab Manual Version 2017-08-05 ... There are many different shell programs, but on most Linux systems, bash (Bourne Again SHell) is the default

CIT Linux Lab Manual

2017-08-05

3

Usually the prompt consists of your user name and machine name, followed by some symbol, usually $. The standard prompt looks like this:

username@bellagio:~$

username@bellagio:/home/shared$

The fi rst example shows the user’s name (your username), machine name (bellagio), the current di rectory (in this case the tilde character (~) represents the user’s home directory), and the $ character indicating that this individual has logged in as a regular user.

The second example shows the user is currently in the /home/shared di rectory.

The prompt is waiting for you to type a command or a name of a program. Actually, these are the same thing, because most Linux commands are small programs themselves. You run the programs by typing their name. For example, we can list the files in a directory l ike so:

username@bellagio:/home/shared$ ls

alphabet.txt emacs_refcard.pdf science.txt

cit131 enquire.c speed.c

cit176 GDB_Cheat_Sheet.pdf TLCL-09.12.pdf

cit231 Introduction_to_Linux-20100512.pdf TLCL-13.07.pdf

cit251 labmanual.pdf turnin-ng.pdf

cit252 lorem.txt units-2.11.tar.gz

cs135 Makefile-c vi_refcard.pdf cs202 Makefile-cpp war_and_peace.txt

darkness.txt nano_exercise_files Zappos_April_2015

Doxyfile ruler

Al l shell commands shown in this manual use a s ingle $ to represent the prompt for clarity.

Linux Tutorials Teaching Linux is beyond the scope of this manual. However, there are several excellent resources available to you: http://bellagio.csn.edu/linuxtut/ Linux Tutorial for Beginners, hosted locally http://l inuxcommand.org/tlcl.php The Linux Command Line – free PDF eBook http://ryanstutorials.net/linuxtutorial/ Learn the Bash Command Line

Common Linux Commands There are hundreds of Linux commands available, but you need know only a few to unlock the power of the command line. Here are some good references to the most useful commands: http://www.nscee.edu/userSupport/unixRef.html A short list of useful commands https ://www.howtoforge.com/useful_linux_commands A more extensive list

Page 4: CIT Linux Lab Manual - CSN · 1 CIT Linux Lab Manual Version 2017-08-05 ... There are many different shell programs, but on most Linux systems, bash (Bourne Again SHell) is the default

CIT Linux Lab Manual

2017-08-05

4

CIT computer accounts Most CIT computer accounts are created from class enrollment lists taken from MyCSN. Students will receive an email at their CSN student email address1 with their login name and temporary password.

• Your CIT account will allow you to log into CSN’s remote Linux server.

• Your account username and initial password will be sent to your CSN student email. • Access to this server, including personal Web content, i s governed by CSN’s Acceptable

Use Policy. Failure to comply with official policy may result in the deletion of your account.

• CIT s tudent accounts are Web enabled for personal, non -commercial content. • CIT s tudent accounts expire automatically. If you enroll in another CIT course that

uti l izes the department’s Linux server in the following semester, your account expiration date will be extended automatically.

• CIT computer accounts unused for more than s ix months may be purged as storage requirements dictate.

• Accounts not activated within 2 weeks of the beginning of class may be deleted.

• If you cannot remember your password, you can request a password reset by contacting the administrator, Kevin Mess. When your request i s received and your account verified, an email with a new temporary password will be sent to your s tudent email address.

• If you have any problems with regard to your account, contact the administrator, Kevin Mess .

Using SSH to access the CIT Linux server remotely Secure Shell (SSH) is a cryptographic network protocol for secure data communication, remote command-line login, remote command execution, and other secure network services between two networked computers. bellagio.csn.edu (heretofore referred to as bellagio) is a Linux server maintained by the CIT department to provide remote access to computing resources for CIT s tudents. You may connect to this server from any CSN computer and/or your personal computer. However, you may require additional software to access this server from your personal computer.

This server is accessible only from within the United States.

You can expect good performance when accessing the server from the CSN local area network, but off-campus performance is dependent upon your Internet connection.

1 Students must establish a CSN student email account at http://studentmail.csn.edu/. It i s important that students check their s tudent email regularly.

Page 5: CIT Linux Lab Manual - CSN · 1 CIT Linux Lab Manual Version 2017-08-05 ... There are many different shell programs, but on most Linux systems, bash (Bourne Again SHell) is the default

CIT Linux Lab Manual

2017-08-05

5

Accessing the server from Windows To access the Linux server using Windows, you will need to download and install MobaXterm (free from http://mobaxterm.mobatek.net/.) MobaXterm is installed on many CSN lab and classroom computers. Ask a lab monitor to direct you to the pods with this software. MobaXterm is available as a s tandalone program, and it works well from a US B thumb drive or network share.

Figure 2 - Connecting to CSN's Linux server using MobaXterm for Windows

To connect to the server, launch MobaXterm, then select “New Session” followed by “SSH”. Fill in the server name (bellagio.csn.edu). Leave the port number unchanged. You may optionally check the “Specify username” box and enter your username so that you do not need to type this with each login. (Note that MobaXterm is updated frequently, so your Session Settings dia log may be different from shown in Figure 2.)

If you have entered and saved incorrect information (e. g. an incorrect username or password) that prevents MobaXterm from connecting to the server, you can right-click on the saved session and edit i t to correct the error(s). Or, you can delete the saved session and create a new one.

Accessing the server from OS X or Linux SSH is normally installed by default on most *nix operating systems (e.g. UNIX/Linux/OS X). Open a terminal window and type the following command, using your username. (Note: Linux is case sensitive. The 'X' character is capitalized.) See Figure 3.

Page 6: CIT Linux Lab Manual - CSN · 1 CIT Linux Lab Manual Version 2017-08-05 ... There are many different shell programs, but on most Linux systems, bash (Bourne Again SHell) is the default

CIT Linux Lab Manual

2017-08-05

6

$ ssh -X [email protected]

Figure 3 - Connecting to CSN's Linux server from OS X

The Mac computers in CSN’s computer labs are pre-configured to properly access the Linux server. However, Mac users may need to install XQuartz to run graphical applications. XQuartz i s available free from http://xquartz.macosforge.org/.

The first time you connect to a new host, SSH prints the following message: The authenticity of host 'bellagio.csn.edu (131.216.135.130)' can't be

established.

RSA key fingerprint is c2:59:75:3f:5b:c3:c8:93:d0:c0:66:e1:8b:06:39:c3.

Are you sure you want to continue connecting (yes/no)?

This is a security feature. It is both expected and normal, and you should answer “yes.” Your cl ient should not prompt you again.

When entering your password, the characters you type will not display on the screen. This is a security feature. Type your password carefully, then press ENTER to continue.

After you have successfully changed your password, the system may disconnect your session. This is a security feature. To continue, log in again using your new password.

Page 7: CIT Linux Lab Manual - CSN · 1 CIT Linux Lab Manual Version 2017-08-05 ... There are many different shell programs, but on most Linux systems, bash (Bourne Again SHell) is the default

CIT Linux Lab Manual

2017-08-05

7

Passwords Passwords must meet a minimum set of rules to be considered acceptable. Your new password must contain at least eight alphanumeric characters including at least one special character. A special character is defined as a non-alphanumeric character (e.g., ~, !, @, #, $, %, ^, &, *, (, ), -, =, +, ?, [, ], {, }).

Longer and more complex passwords offer better security.

Changing your password from the command line The passwd uti lity i s used to update your password. You can only change the password of your own account. The passwd uti lity will prompt you for your current password to proceed. You

wi l l then enter your new password twice.

When entering your password, the characters you type will not display on the screen. This is a security feature. Type your password carefully, then press ENTER to continue. Example:

$ passwd

Changing password for 1234567890.

(current) UNIX password:

New password:

Re-type new password:

passwd: password updated successfully.

Changing your password from a Web browser You can change your password using a Web browser. Browse to https://bellagio.csn.edu:5950/.

You must use a secure connection by using “https” to go to the correct Web page. If you forget to use “https” you will be redirected to the CIT Department’s Web page.

You must include the port number, 5950, to go to the correct Web page.

Validity • Passwords are va lid for 180 days. The system will generate reminders 30, 7, and 3 days

prior to password expiration. These reminders are sent to your CSN student email. • Accounts are disabled 30 days after password expiration and require administrator

assistance to reactivate. • Accounts are subject to deletion 180 days after password expiration.

Automatic Ban Repeated failed login attempts will result in temporary, and in some cases, permanent bans. This is a security feature. If you cannot remember your username or password, contact the server administrator, Kevin Mess, for a password reset.

Page 8: CIT Linux Lab Manual - CSN · 1 CIT Linux Lab Manual Version 2017-08-05 ... There are many different shell programs, but on most Linux systems, bash (Bourne Again SHell) is the default

CIT Linux Lab Manual

2017-08-05

8

Don’t lock yourself out!

Local Email Every user has a local email account used to forward important system messages (e. g. password expiration, excessive disk usage, etc.). By default, emails sent to your local account are forwarded to your CSN student email account.

Do not edit or delete the .forward fi le in your home directory.

Logging out When you’re ready to end your session with the bellagio server, type logout or exit. You

may now close the program you are using to access the server (e. g. MobaXterm, Terminal).

Always log out using logout or exit!

Transferring files to/from the server To transfer fi les to/from the bellagio server, you will need a file transfer application that supports Secure Copy (scp) or the Secure File Transfer Protocol (SFTP). There are numerous applications that support each of these protocols on each operating system. This manual explains two of the applications. Regardless of the file transfer application used, you will use the same username and password. Configure your application to connect to bellagio.csn.edu. If necessary, specify port 22.

Linux file systems are case-sensitive; two filenames that differ only in case, such as afile.txt and AFile.TXT, are considered unique. Both files can exist in a single directory, and i f you enter the filename’s case incorrectly, Linux won’t be able to access an existing file. Windows, by contrast, is case-retentive: filename case is preserved when you save a file, but the OS doesn’t care a bout case when you specify a filename.

Windows-based editors generally put an extra carriage return at the end of each line of text. This may cause problems for some Linux-based editors. To correct this problem, use the dos2unix uti l ity on each text file you upload from your Windows machine. This utility i s built in to the MobaXterm program. It can also be executed at the command prompt on the bellagio server: Example:

$ dos2unix filename.txt

Conversely, Linux-based editors use a single newline character at the end of each line of text. This may cause problems with some Windows-based editors that expect an extra carriage return. To correct this problem, execute the built-in utility unix2dos on each text file you

Page 9: CIT Linux Lab Manual - CSN · 1 CIT Linux Lab Manual Version 2017-08-05 ... There are many different shell programs, but on most Linux systems, bash (Bourne Again SHell) is the default

CIT Linux Lab Manual

2017-08-05

9

download to your Windows machine. This utility i s built in to the MobaXterm program. It can a lso be executed at the command prompt on the bellagio server: Example:

$ unix2dos filename.txt

File Transfer using MobaXterm When you log in to a remote bellagio session using SSH, a graphical SFTP (Secure File Transfer Protocol ) browser appears in the left s idebar allowing you to drag and drop files directly to or from the bellagio server using the SFTP connection. To manually open a new SFTP session, create a new session (see Figure 4).

File transfer using FileZilla Fi leZilla is a fast and reliable cross-platform SFTP client with an intuitive graphical user interface. An installable version can be downloaded for free from https://filezilla-project.org/. A portable version (i.e. a version that can be stored on a USB thumb drive or network share) is ava ilable for free from http://portableapps.com/apps/internet/filezilla_portable. To create a new connection to the bellagio server, open FileZilla and select File->Site Manager. Cl ick the New Site button to open a dialog similar to that shown in Figure 5.

Figure 4 - Opening a new SFTP connection in MobaXterm

Page 10: CIT Linux Lab Manual - CSN · 1 CIT Linux Lab Manual Version 2017-08-05 ... There are many different shell programs, but on most Linux systems, bash (Bourne Again SHell) is the default

CIT Linux Lab Manual

2017-08-05

10

Figure 5 - Creating a new SFTP connection in FileZilla

Enter the server’s address in the host field, select “SFTP - SSH” as the protocol, then enter your username and password in the appropriate fields. Leave the port field blank to use the default port (22). Finally, click on the Connect button to connect to the server.

Serving Web pages Every s tudent account is enabled to serve Web pages and other documents using the Apache Web server. (Explaining how to write these documents is beyond the scope of this document.) Place your HTML files in the public_html di rectory found in your home directory.

Only put fi les you want to share with everyone into your public_html di rectory!

To view your personal Web site, enter the following address, using your username, into your favori te Web browser:

http://bellagio.csn.edu/~username/

The ti lde character (~) must appear before your username!

Programming tools

Text editors The bellagio server has many different editors that you can use to create and maintain your source code files. Editors may be console-based or Graphical User Interface (GUI) based. At a minimum, the following console text editors are available:

• nano – A friendly, yet powerful editor with syntax highlighting. • vim – A powerful editor, typically installed by default on all *nix operating systems.

Page 11: CIT Linux Lab Manual - CSN · 1 CIT Linux Lab Manual Version 2017-08-05 ... There are many different shell programs, but on most Linux systems, bash (Bourne Again SHell) is the default

CIT Linux Lab Manual

2017-08-05

11

• emacs – A powerful and extensible editor. Commonly found on *nix operating systems.2 Severa l GUI editors are also available. You will require a fast Internet connection for these to be usable:

• gedit – A general purpose text editor with syntax highlighting.

• geany – A l ightweight text editor and basic Integrated Development Environment (IDE).

When launching a GUI application from the command prompt, always append the ampersand character (&) to launch the program as a background task, freeing the command

l ine for further use. Example:

$ gedit &

Compiling and executing a C program The C compi ler that is used to evaluate C programming assignments is called clang.3 In order

for a fi le to be recognized by the clang compiler as a C program file, the name of the file must have an appropriate extension. The extension to be used for C programs is .c. Compile your

program using the following command, substituting your source code file name:

$ clang progname.c

If the program file contains no syntax errors, an executable file with the name a.out wi l l be

created. This file should not be displayed, as i t is not in a human readable form. The program can now be executed with the following command:

$ ./a.out

If the program contained syntax errors, a series of error messages will be displayed. You mu st take note of the lines at which the errors occurred and then go back to the original p rogram file (the .c fi le) to locate and correct the mistakes. Save your updated file. Then, recompile.

Compiling and executing a C++ program The C++ compi ler used to evaluate C++ programming assignments is called clang++.45 In order for a file to be recognized by the clang++ compiler as a C++ program file, the name of

2 Students transferring to UNLV should learn emacs. 3 Another very common C compiler is the GNU gcc compiler. Its command line interface is s imilar to clang. However, its warning messages can be cryptic. 4 Another very common C++ compiler is the GNU g++ compiler. Its command line interface is s imilar to clang++. However, i ts warning messages can be cryptic. 5 Students transferring to UNLV should practice using the g++ compiler.

Page 12: CIT Linux Lab Manual - CSN · 1 CIT Linux Lab Manual Version 2017-08-05 ... There are many different shell programs, but on most Linux systems, bash (Bourne Again SHell) is the default

CIT Linux Lab Manual

2017-08-05

12

the fi le name must have an appropriate extension. The extension to be used for C++ programs is .cpp. Compi le your program using the following command, substituting your source code fi le name:

$ clang++ progname.cpp

If the program file contains no syntax errors, an executable file with the name a.out wi l l be

created. This file should not be displayed, as i t is not in a human readable form. The program can now be executed with the following command:

$ ./a.out

If the program contained syntax errors, a series of error messages will be displayed. You mu st take note of the lines at which the errors occurred and then go back to the original program file (the .cpp file) to locate and correct the mistakes. Save your updated file. Then, recompile.

Additional compiler options The following compiler options are quite helpful. See the compiler’s man page for a listing and description of a ll i ts options.

Option Description -ansi Instruct the compiler to adhere to the ISO standard for the language.

-std=___ Instruct the compiler to adhere to the specified standard for the language. Examples: -std=c99, -std=c++14

-ggdb Instruct the compiler to generate debug information.

-o filename Instruct the compiler to use the specified filename for the executable instead of a.out.

-O0

-O1

-O2

-O3

Instruct the compiler to optimize the resulting executable. -O0 Turn off a ll optimization. (Default if not specified.) -O1 Provides a basic level of optimization. -O2 Provides more advanced optimization. -O3 Provides the highest level of optimization.

-pg Generate profiling code in executable.

-pedantic Force compiler to issue all warnings demanded by s trict ISO standards.

-Wall Enables warnings about questionable constructions that are easy to avoid. (However, it does not enable all warnings.)

-Wextra Enables some extra warning flags that are not enabled by -Wall. -Weverything Enables ALL warnings. Very picky (not available with gcc or g++).

-lm

(C only) Link the math library to your executable. Required when your program includes math.h. The -lm option should be appended to

the end of your compile command.

Page 13: CIT Linux Lab Manual - CSN · 1 CIT Linux Lab Manual Version 2017-08-05 ... There are many different shell programs, but on most Linux systems, bash (Bourne Again SHell) is the default

CIT Linux Lab Manual

2017-08-05

13

As an example, to compile a program with maximum adherence to standards, specify the fol lowing options:

$ clang -std=c99 -pedantic -Wall -Wextra progname.c

$ clang++ -std=c++14 -pedantic -Wall -Wextra progname.cpp

Note that the commands are very s imilar between C and C++.

Using make to compile a C or C++ program An a l ternative method for compiling a C or C++ program is to use the Linux make uti lity. This wi l l automatically invoke the compiler using the –o option and create an executable file with the same name as the source code file. The command to compile and create the executable is:

$ make progname

The fi le extension should not be included in this command! In order to execute the program, type the command:

$ ./progname

You can find general purpose Makefiles for the C and C++ languages in the /home/shared

di rectory that simplify the build of large projects (multiple source code files).

Compiling and executing a Java program The Java compiler used to evaluate Java programming assignments is called javac. In order for a fi le to be recognized by the javac compiler as a Java program file, the name of the file

name must have an appropriate extension. The extension to be used for Java program file names is .java. Compi le your program using the following command, substituting your source

code fi le name:

$ javac –Xlint:all Progname.java

If the program file contains no syntax errors, an executable file with the name Progname.class wi ll be created. This file should not be displayed, as i t is not in a human

readable form. The program can now be executed with the following command:

$ java Progname

Notice that you only use the class name (i.e. no extension) when executing the program. If the program contained syntax errors, a series of error messages will be displayed. You must take note of the l ines at which the errors occurred and then go back to the original program file (the .java fi le) to locate and correct the mistakes. Save your updated file. Then, recompile.

Page 14: CIT Linux Lab Manual - CSN · 1 CIT Linux Lab Manual Version 2017-08-05 ... There are many different shell programs, but on most Linux systems, bash (Bourne Again SHell) is the default

CIT Linux Lab Manual

2017-08-05

14

Editing and executing a Python program Python provides an Integrated Development Environment for Python (IDLE) suitable for beginners. It has multiple windows, a Python shell, and an integrated debugger. It is a GUI application, so remember to append the ampersand (&) character:

$ idle3 progname.py &

There is a nice IDLE tutorial available at http://www.annedawson.net/Python_Editor_IDLE.htm.

Other tools useful to programmers There are several tools on the server to assist you in writing better programs.

Spell check your source code aspell i s an interactive spell-checking tool you can use to check the spelling in a file. When

checking source code files, it may not recognize all the variable names and keywords, even i f they are syntactically correct. Ignore these words as they are presented to you. It i s most useful in checking the spelling of words within your documentation. To spell check a file:

$ aspell –c filename

Automatically format your source code astyle i s a tool that will s tandardize the indentation and formatting of your source code (a source code beautifier). If you do not specify any options, astyle wi l l replace tab characters

with spaces and use four spaces per indent level, but i t will not change bracket placement or make other formatting changes. You can optionally specify predefined bracket s tyles for your code (see http://en.wikipedia.org/wiki/indent_style for details). Some examples include: –A2 Java s tyle formatting/indenting with attached brackets. –A3 Kernighan & Ri tchie s tyle formatting/indenting with Linux brackets. –A4 Stroustrup style formatting/indenting with Stroustrup brackets. –A10 One True Brace Style formatting/indenting with Linux Brackets and adds brackets

to a l l conditionals. Command to format and indent your C source code in Kernighan & Ri tchie style:

$ astyle –A3 progname.c

Command to format and indent your C++ source code in Stroustrup style:

$ astyle –A4 progname.cpp

Command to format and indent your Java source code in Java s tyle:

Page 15: CIT Linux Lab Manual - CSN · 1 CIT Linux Lab Manual Version 2017-08-05 ... There are many different shell programs, but on most Linux systems, bash (Bourne Again SHell) is the default

CIT Linux Lab Manual

2017-08-05

15

$ astyle –A2 Classname.java

Check for memory leaks You can check your compiled C or C++ program for memory leaks by using valgrind, a flexible program for debugging and profiling Linux executables. At i ts simplest, try: $ valgrind ./a.out

==24298== Memcheck, a memory error detector

==24298== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. ==24298== Using Valgrind-3.10.0.SVN and LibVEX...

==24298== Command: ./a.out

==24298==

==24298== HEAP SUMMARY:

==24298== in use at exit: 102,400 bytes in 100 blocks

==24298== total heap usage: 100 allocs, 0 frees, 102,400 bytes allocated

==24298==

==24298== LEAK SUMMARY:

==24298== definitely lost: 102,400 bytes in 100 blocks ==24298== indirectly lost: 0 bytes in 0 blocks

==24298== possibly lost: 0 bytes in 0 blocks

==24298== still reachable: 0 bytes in 0 blocks

==24298== suppressed: 0 bytes in 0 blocks

==24298== Rerun with --leak-check=full to see details of leaked memory

==24298==

==24298== For counts of detected and suppressed errors, rerun with: -v

==24298== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

More options are available. Check the valgrind man page for a complete description.

Analyze program execution The profiler (gprof) i s a program used to analyze program execution and determine where “hot spots” are in the application. Your C and C++ programs must be compiled with the –pg option to include profiling code.

After that, the program can be run normally:

$ clang demo.c -pg

$ ./a.out

When the program finishes, you will find a ca ll graph profile file named gmon.out in the same directory as the executable. You can then run the gprof program against the a.out program

and save the output to a file:

$ gprof a.out >gprof.txt

Page 16: CIT Linux Lab Manual - CSN · 1 CIT Linux Lab Manual Version 2017-08-05 ... There are many different shell programs, but on most Linux systems, bash (Bourne Again SHell) is the default

CIT Linux Lab Manual

2017-08-05

16

Notice that the gmon.out fi le was not referenced in the command line, just the name of the executable program. gprof automatically uses the gmon.out fi le located in the same directory. This example redirected the gprof output to a file named gprof.txt. The resulting file contains the complete gprof report for the program. Here are the first few lines

of a report:

$ cat gprof.txt

Flat profile:

Each sample counts as 0.01 seconds.

% cumulative self self total

time seconds seconds calls us/call us/call name

76.72 31.93 31.93 50000 638.65 840.76 foo

24.33 42.06 10.13 50100 202.11 202.11 bar

...

The report shows the total processor time and times called for each individual function that was called. Following this report is the ca ll graph, which shows the breakdown of time by individual functions and how the functions were called. These reports can help you identify functions that require the most amount of processing time as the program runs. Often they are the most mathematically intensive functions, but that is not a lways the case. Functions that are also I/O intensive can increase processing time. In the example above, foo consumed nearly 77% of the processor’s time. Which function would you

scrutinize first for potential optimization?

Software version control Vers ion control is the management of changes to documents, computer programs, large web s i tes, and other collections of information. While cri tical in multi-programmer environments, individual programmers find in indispensable, too. Have you ever:

• Made a change to code, realized i t was a mistake and wanted to revert back? • Lost code or had a backup that was too old? • Had to maintain multiple versions of a program?

• Wanted to see the difference between two (or more) versions of your code? • Wanted to prove that a particular change broke or fixed a piece of code? • Wanted to review the history of some code?

• Wanted to experiment with a new feature without interfering with working code? In these cases, and no doubt others, a version control system makes your life easier. And, knowledge of version control systems is a highly desired skill in the job market. The bellagio server has one of the most popular version control systems in use today, git,

originally wri tten by Linus Torvalds. It’s easy to use. There are several tutorials available for free:

Page 17: CIT Linux Lab Manual - CSN · 1 CIT Linux Lab Manual Version 2017-08-05 ... There are many different shell programs, but on most Linux systems, bash (Bourne Again SHell) is the default

CIT Linux Lab Manual

2017-08-05

17

• http://www.git-tower.com/learn/ • http://try.gi thub.com/

• http://gi tref.org/ You may want to publish your code for prospective employers, and a good way to do that is us ing the free GitHub service. (https://github.com/)

Submitting homework assignments The turnin program is a means by which students can electronically turn in their class

assignments, i f the instructor has configured the system to accept assignments in this manner. For the purposes of the following examples, we will use as our course cs135 and for an assignment we will use lab03. If you’re uncertain of what projects are open for submiss ion, you can generate a listing of projects for a specific course:

$ turnin –c cs135 ––list

You can submit a single file by issuing the command:

$ turnin –c cs135 –p lab03 –v filename

Multiple filenames may be submitted at once from the command l ine, for instance:

$ turnin –c cs135 –p lab03 –v file_1.h file_1.cpp out.txt

$ turnin –c cs135 –p lab03 –v *.cpp *.h

Each use of the turnin command will overwrite a previous submission, so be careful to

a lways include all required files. Note that filenames that contain spaces will not function properly with turnin, s ince the spaces inside the filenames act as delimiters between

separate filenames. Avoid potential problems by a lways using the '_' character instead of spaces in your filenames.