Cos 231 Introduction to unix spring semester 2017, aubg professor Volin Karagiozov Homework Assignment 2



Yüklə 23,68 Kb.
tarix08.10.2017
ölçüsü23,68 Kb.
#3702



COS 231 Introduction to UNIX

Spring semester 2017, AUBG

Professor Volin Karagiozov
Homework Assignment 2
Independent tutorial work – 40%

Problem solving – 60 %

HW 01 due 01 February (Wednesday), 10:00 pm

HW 02 due 15 February (Wednesday), 10:00 pm

HW 03 due 29 March (Wednesday), 10:00 pm

HW 04 due 19 April (Wednesday), 10:00 pm
Please upload your solution in elearn
No credit will be given for a problem if there is insufficient work/explanation.

You must show enough work, or give sufficient explanation, in each problem to clearly indicate how you obtain your answer.


Warning: In eLearn you can upload only a single file. If needed use ZIP archives for multiple file solutions.

On each page of your solution write your name and student ID.



Home Work Assignment Late Submission

HW #

Due date

Up to 24 hours late penalty

More than 24 hours late penalty

HW 01

01 February, 10:00 pm

20%

100 % (no credits given)

HW 02

15 February, 10:00 pm

20%

100 % (no credits given)

HW 03

29 March, 10:00 pm

20%

100 % (no credits given)

HW 04

19 April, 10:00 pm

20%

100 % (no credits given)




  • Homeworks turned in up to 24 hours late will receive a 20% penalty;

  • No credit will be given for any assignment submitted later than 24 hours from the due date..For example, the first assignment will not be accepted after February 05, 10:00pm.

  • If you are at risk of missing a deadline due to a busy week, you should hand in as much as you can before the regular deadline.

  • Start working on your assignment at least one week before the deadline, so that you can get a feel for how much time they are going to take you to complete. Don’t wait until the last minute to start an assignment.

  1. Independent Tutorial based Study:


Learn UNIX - http://www.tutorialspoint.com/unix/index.htm









  1. Go through the “Unix Shell Programmin” chapters of the tutorial.

  2. For each of the chapters, write 2-3 sentences summarizing what you have learned and provide examples similar to those in the tutorial.

  3. Use the on-line terminal for CentOS provided by :

http://www.tutorialspoint.com/unix_terminal_online.php

  1. Alternatively, use “Execute BASH SHELL online” provided by

http://www.tutorialspoint.com/execute_bash_online.php





  1. What is Shell?

  • A Shell provides you with an interface to the Unix system. It is an environment in which we can run our commands, programs, and shell scripts. There are two major shell types: Bourne shell and C shell. If you are using a Bourne-type shell, the $ character is the default prompt. If you are using a C-type shell, the % character is the default prompt.

  1. Using Variables

  • A variable is a character string to which we assign a value. A variable is nothing more than a pointer to the actual data. The shell enables you to create, assign, and delete variables. You can define variables with the following syntax: “ variable_name = variable_value ”. There are 3 variable types: Local - A local variable is a variable that is present within the current instance of the shell; Environment - An environment variable is available to any child process of the shell; Shell Variable - A shell variable is a special variable that is set by the shell and is required by the shell in order to function correctly.

  1. Special Variables

  • There are a number of variables that are called “Special” and are predefined with a certain value. For example, the $ character represents the process ID number, or PID, of the current shell. There are also special parameters that allow accessing all the command-line arguments at once. $* and $@ both will act the same unless they are enclosed in double quotes, "".\

  1. Using Arrays

  • An array variable is a variable that can hold multiple values at once. Instead of creating a new name for each variable that is required, you can use a single array variable that stores all the other variables. The syntax of defining an array variable is as follows: “array_name[index] = value”. To access a value from the array you use the following command syntax: “${array_name[index]}”

  1. Basic Operators

  • There are various operators supported by each shell. The chapter discusses the following operators:

  • Arithmetic Operators

  • Relational Operators

  • Boolean Operators

  • String Operators

  • File Test Operators

  • The operators in each shell might differ.

  1. Decision Making

  • Unix Shell supports conditional statements which are used to perform different actions based on different conditions. The two main decision making conditions are the “if-else” and “case-esac”. If else statements are useful decision-making statements which can be used to select an option from a given set of options. The case...esac statement in the Unix shell is very similar to the switch...case statement we have in other programming languages like C or C++.

  1. Shell Loops

  • A loop is a powerful programming tool that enables you to execute a set of commands repeatedly. The chapter focuses on the four main types of loops.

  • The while loop executes the given commands until the given condition remains true

  • The until loop executes until a given condition becomes true.

  • The for loop operates on lists of items. It repeats a set of commands for every item in a list.

  • The select loop provides an easy way to create a numbered menu from which users can select options.

  1. Loop Control

  • This chapter focuses on how to control, get in or get out of a loop. The two main statements it covers are :

  • The break statement is used to terminate the execution of the entire loop, after completing the execution of all of the lines of code up to the break statement. It then steps down to the code following the end of the loop.

  • The continue statement is similar to the break command, except that it causes the current iteration of the loop to exit, rather than the entire loop.

  1. Shell Substitutions

  • The shell performs substitution when it encounters an expression that contains one or more special characters. Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the place of the commands. Variable substitution enables the shell programmer to manipulate the value of a variable based on its state.

  1. Quoting Mechanisms

  • Unix Shell provides various metacharacters which have special meaning while using them in any Shell Script and causes termination of a word unless quoted. Characters within single quotes are quoted just as if a backslash is in front of each character. With this, the echo command displays in a proper way. Characters within single quotes are quoted just as if a backslash is in front of each character. This helps the echo command display properly.

  1. IO Redirections

  • Most Unix system commands take input from your terminal and send the resulting output back to your terminal.

  • Output redirection: The output from a command normally intended for standard output can be easily diverted to a file instead. This capability is known as output redirection.

  • Input Redirection: Just as the output of a command can be redirected to a file, so can the input of a command be redirected from a file.

  1. Shell Functions

  • Functions enable you to break down the overall functionality of a script into smaller, logical subsections, which can then be called upon to perform their individual tasks when needed. Declaring a function is based on the following syntax:

function_name () {

list of commands

}

  • You can define a function that will accept parameters while calling the function. If you execute an exit command from inside a function, its effect is not only to terminate execution of the function but also of the shell program that called the function. One of the more interesting features of functions is that they can call themselves and also other functions. A function that calls itself is known as a recursive function.

  1. Manpage Help

  • All the Unix commands come with a number of optional and mandatory options. It is very common to forget the complete syntax of these commands. Unix's version of Help files are called man pages. The syntax is simple. You just call “man” in front of a command/function. Man pages are generally divided into sections, which generally vary by the man page author's preference.


  1. Problem solving




  1. Create a script that takes as command-line argument your birthday and displays how many days and hours remain to your birthday from now (system date at the moment of running the script) (30%




  1. Create a script that will display for each currently logged-in user (loop over the result of the “users” command) the number of sessions to stud.linux.aubg.bg (take logs with “last” command and accumulated from all sessions time of the duration of the sessions. (30%)

Show results in this format:

---username--- ---# of sessions--- ---Total duration---



(hh:mm:ss)



Yüklə 23,68 Kb.

Dostları ilə paylaş:




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə