I need help with this assignment which is due by Monday.

profileizakurev
cs345_shell_script_3.docx

Makarevich - Key Assignment Doc. Page 11

William Makarevich

Colorado Technical University

CS345-1601A-01: UNIX Systems Programming

Key Assignment Document - Unit 4IP

Professor James LeMaster

February 1, 2016

Table of Contents Section 1: UNIX Programming (Week 1 IP) 3 1.1 Listing of the UNIX Shell Script 3 1.2 UNIX Run Levels 5 Section 2: UNIX Processes (Week 2 IP) 8 2.1 Listing of the UNIX Shell Scripts 8 2.1.1 Group and User Add & Delete Script 8 2.1.2 Kill Script 10 User maintenance in UNIX script and user kill processes 11 2.2 User Processes and Daemons 12 Section 3: Introduction to Perl (Week 3 IP) 13 3.1 Listing of the Perl Script 13 3.2 Variables in Perl 14 Section 4: Writing Perl Programs (Week 4IP) 16 4.1 Listing of the Perl Script 16 4.2 Regular Expressions in Perl 18 Section 5: UNIX Tools (Week 5 IP) 20 5.1 Final Script Listing 20 5.2 Listing of the array (flat file database) file created by IP5’s script “kill task”. 20 5.3 Compiling a Program 20 5.4 Compiled versus Interpreted Programs 20 Section 6: References 21

Section 1: UNIX Programming (Week 1 IP)

1.1 Listing of the UNIX Shell Script

#!/bin/bash

###########################################################################

# This script prompts the system administrator for all valid input parameters.

# Prompts if a wall needs to be sent, and if the response is ‘yes’, sends the message.

# Prompts if this is a restart or shutdown, and executes accordingly.

###########################################################################

echo "Do you want to send a wall"

read vwall

string=${vwall,,}

if [ "$string" == "yes" ]; then

echo "Sending message to all users"|wall

exit

fi

echo "Would you like to restart or shut down the server?"

read i

string=${i,,}

if [ "$string" == "shutdown" ]; then

echo "Restarting the server"

shutdown -h +5 "Warning: the server is going down to shut down in 5 minutes."

elif [ "$string" == "restart" ]; then

echo "Restarting the server"

shutdown -r +5 "Warning: the server will restart in 5 minutes."

else

echo "Goodbye"

fi

1.2 UNIX Run Levels

Automatic System Shutdown Procedures Using UNIX Programming
In UNIX system programming, there are different ways to achieve automatic system shutdown procedures using command system applications available in the program. The process requires proper use of script language in the order it initiates a successful shutdown process. Every system administrator knows that an automatic system shutdown process cannot be achieved by simply issuing one simple command and bring down the entire system in an effective way (Toulas, 2014).  This is especially true in a production environment where system sensitivity relies on the proper utilization of the process required. 
If this process is being used by users who ignore essential steps in a proper shutdown process rather than rush the system into the immediate shutdown, they risk crashing the entire system operation.  The users need to be warned to avoid such careless system application procedures and follow the necessary steps for the system to shut down in the appropriate time. There are different options to consider when initiating system reboot and shutdown procedures that will enable users to use the necessary system applications during automatic system shutdown and reboot (Toulas, 2014).  
In an automatic shutdown process, there are various init run levels that are very important to ensure how the system is shutdown. It is very necessary to understand the various run levels associated with a system shutdown. There are two different run levels that will result into the system shutdown automatically. These levels initiate different application concepts that will enable the user to understand the state of the system upon use.  To understand the different concept levels in system shutdown, this paper will describe in detail the various processes that will result in the successful automatic shutdown.
To prompt the system administrator for all valid input parameters, the system will have to initiate the different run levels into the application. In this case, there are two different run levels that will result into the system shutdown (Parlante, 2001). The first run level that ensures proper system shutdown procedure is the init 0. The init 0 run level simply indicates that the system is not running at all. This means the system is in a shutdown state and there are no system operations or application software that is in play. Another important level is the init 6 run level (Parlante, 2001). The init 6 run level is a restart level that is designed and reserved to restart the system upon premature shutdown. These two run levels are important in understanding the concepts of automatic shutdown procedures. 
There are different command parameters to issue a shutdown, for example, a broadcast message can be issued to alert the system administrator that the system is shutting down. The system administrator can also be told to wait for sixty seconds before the automatic shutdown procedure initiates (Parlante, 2001). The system administrator can also be provided with a confirmation prompt through the use of script language such as "read -p "Are you sure?" -n 1 –r", "echo    # (optional) move to a new line", "if [[! $REPLY =~ ^ [My] $]]", then "exit 1" and finally "fi" (Parlante, 2001). Alternatively, the system can also be run to a specific run level. 
To learn all these command parameters, the user needs to understand the shutdown command in detail. This can be achieved by using the command pages that provide a platform to issue commands to the system. But, there are few options a user can use to perform proper automatic shutdown procedures. These options will enable the user to determine whether the system needs a wall and how to send the message. 
 For instance,–h (the minus h) command takes the system into a halt state. In this case, -h command tells the system to go all the way down, not to go down and reboot to another level, not to shut down partially or shutdown the system completely (Parlante, 2001). The –y (the minus y) command provides a time out or a number of seconds that will be required to elapse before the system is brought down. So, for example, -y 300 this means the system is going to shut down within five minutes (Parlante, 2001). 
The –r command message is responsible for system shutting down and reboot to a default rc level. This command message enables the user to determine if the system needs to be restarted after a reboot level. Basically, -r is simply a reboot rather than a real shutdown (Parlante, 2001). Finally, the –now command is another option the user can use in successful shutting down the system. This command is equivalent to a y 0 which can be illustrated with the following reference "-now = y 0 = no wait" (Parlante, 2001). So if the user were to send the message to the system to wait zero seconds before shutting down, the system would automatically shut down immediately. The system will not wait but instead, shift into an automatic shutdown mode. This command parameter is just like telling the system to shut down immediately. 
To properly determine if the system is undergoing an automatic shutdown procedure, the user will have to identify the various ways to communicate with the system and assign an appropriate command for shut down purposes. To reboot the system, the user can issue a shutdown –ry 120 command that will communicate to the system to reboot to the default run level in y seconds, in which in this case the y was passed to 120 seconds (Parlante, 2001). To shut down the system, the user can issue a shutdown –h now command. This will completely bring down the system to a halt. Therefore, by learning the option of all the command to be used as a system administrator, one can understand the power of one command doing two completely different tasks.

Section 2: UNIX Processes (Week 2 IP)

2.1 Listing of the UNIX Shell Scripts

2.1.1 Group and User Add & Delete Script

#!/bin/bash

# Group and User Add & Delete Script

# January 18, 2016

# Please run script as admin to work like needed

while true

do

clear

echo "***User Administration Menu ***";

echo "1 Create UNIX Group";

echo "2 Drop UNIX Group";

echo "3 Create User Account";

echo "4 Drop User Account";

echo "q to exit the program"

echo "Please Enter 1, 2, 3, 4 or q";

read answer

case "$answer" in

1) echo "what will be the group name ?"

read name

groupadd $name

echo "group $name sucessfullly created";;

2) echo "Enter group name to drop"

read name

groupdel $name

echo "group $name sucessfully deleted";;

3) echo "Please provide username to use"

read username

useradd $username

echo "User $username sucessfully added";;

4) echo "Enter username to drop"

read username

userdel $username

echo "user $username sucessfully deleted";;

q) exit

esac

echo -e "Enter return to continue \c"

read input

done

2.1.2 Kill Script

#!/bin/bash

# Kill processes for a particular user

# January 18, 2016

# Please run script as admin to work like needed

while true

do

clear

echo "***User Administration Menu ***";

echo "What username processes ?"

read name

top -u $name

userID=$(id -u $name)

echo "Are you sure you want to kill this user($name) processes y/n ?"

read answer

case "$answer" in

y) pkill -u $userID

echo "user processes successfully killed !";;

n) exit

esac

echo -e "Enter return to continue \c"

read input

done

User maintenance in UNIX script and user kill processes

In this script, users and groups creation process will be widely illustrated in both basic and scripted language. To begin with, in UNIX programming there are two types of users. There are root users and normal users, where root users are usually represented by hash front while the normal users are usually represented by dollar front. There is normally one type of root users and many normal users. The normal users are created by root users. Therefore, I will focus first on creating normal users which is the most common programming platform for all UNIX programmers (McIntire, 2006).

In the creation of normal users, let’s focus on the creation of users and groups. In group creation, the first part of the script is to indicate the group name that is written in script language as #groupadd g_1 (McIntire, 2006). This script language indicates the group name but does not indicate the group identification. Default group identification will be generated by the system. Also, the system user can create group identification by specifying some identification through #groupadd –g 1010 g_2. In this case, 1010 is the group identification, and g_2 is the name of the group. The –g option is used to specify the group identification (McIntire, 2006).

In a situation where the system user wants to change the group identification, the user will use the command # groupmod –g 1020 g_1. To delete the group created, the system user can use the command # group deal g_1. In a situation where the system user wants to see all of the information about the groups, specific commands such as # cat (space) /, etc./group. The last part of the command (group) is the file name that is present in the etc. directory. When following the commands illustrated in UNIX programming, the system user can establish various grounds in maintaining users within the system application (McIntire, 2006).

In the creation of users, the name of the user is the first command the system administrator must issue. This can be achieved through the command of # user add u_1 where, u_1 is the name of the user (Linux corps, 2012). It is important to note that, in user creation, there is no default identification for users provided in the user maintenance process. The user can only create his or her identification, unlike the group creation process.

This can be achieved by initiating a command such as # user add –u 3010 u_2, where, 3010 is the identification for the user, u_2 is the name of the user and –u is the option to assign identification to the user. In the modification of the user, the following command must be initiated; # usermod –u 3020 u_1 (Linux corps, 2012). In the process of deleting a user, the system administrator can issue a command as # usermod u_2 and if the system administrator wants to see all the information about the user, he or she can issue a command #cat (spaced) /etc./passwd. The passwd stands for password and also represents the file that is contained in the etc directive.

2.2 User Processes and Daemons

Daemons are a system program that runs as a background routine somewhat being in direct control of an interactive system administrator (Linux corps, 2012). The initial process of a daemon is usually the init process during booting of a computer program. The daemon is usually created during the init process through a process known as forking creating the child process. A Daemon program operates by forwarding a request to other operating programs. In computer programming, all servers have web pages that are labeled hypertext transfer protocol daemon (httpd) that waits to receive request from users and other clients (Linux corps, 2012).

Section 3: Introduction to Perl (Week 3 IP)

3.1 Listing of the Perl Script

#!/usr/bin/perl # January 25, 2016 while(true){ print "***User Administration Menu *** \n\n"; print "1 Show current date and time \n"; print "2 Show users currently logged \n"; print "3 Show the name of the working directory \n"; print "4 Show the contents of the working directory \n"; print "Please Enter 1, 2, 3, or 4\n"; $value = <STDIN>; if($value=='1'){ $datestring = localtime(); print "Current Date Time: ".$datestring . "\n"; }elsif($value=='2'){ $user = getlogin || getpwuid($<); print "Currently Logged Users: ".$user ."\n"; }elsif($value=='3'){ use Cwd; my $pwd = cwd(); print "My working directory ".$pwd . "\n"; }elsif($value=='4'){ use Cwd; my $pwd = cwd(); print $pwd . "\n"; } }

3.2 Variables in Perl

Perl Programming

Perl programming is a rather complex programming technique that requires a good educational background in different areas of programming (Perl organization, 2015). These techniques are necessary to generate proper coding skills and get proper feedback on the programming technique used. Therefore, the first line of code is by typing the hash tag sign which is accomplished through #! /usr/bin/Perl (Perl organization, 2015).

What this line of code does is just tell the compiler or operating system where to open up the program or what program to use to run the program file. In this case, a menu is created for the purpose of seeking feedback from the user on what task to perform. The system, in this case, is going to communicate by telling the system administrator that the system will be running in Perl program.

The next step in coding is by creating a menu that will be useful to the user in performing a certain task. The user can type print ("Menu, Perl/n/n"); (Webfiend, 2004) and finally save the coding in either valuable document within the system operator. It is essential to note that when creating a file name in the writing of a Perl program, that the user save the file in the computer with an extension of pl, for example, one can save the file as "Menu perl.Pl" (Webfiend, 2004). This is important because the system needs to know that it is a Perl program.

Another important function creating a Perl program is to show the users currently logged in. This can be achieved by typing print ("users logged in: ");and then followed by typing $name = <STDIN>; (Webfiend, 2004). This will provide a platform to show which users are currently logged in the system. But in order to create a visible platform, the user can type print ("Users logged," $name "\n\n"); (Webfiend, 2004). After issuing the above commands, the system user can run the script and the system will respond by displaying the command. The user can then type and view all the users logged in the system.

The next step in writing a Perl program is to show the name of the working directory in which can be achieved by issuing certain command scripts. In showing the name of the working directory, the user can see what the system is doing and make necessary changes if the need arises. To attain this, the user will have to issue a print command to the system by typing print ("show the name of the working directory,"); (Webfiend, 2004).

The system will then require typing the commanding code by initiating a command of $working directory = <STDIN>; (Webfiend, 2004). The $working directory is the container that limits the system to provide information on the working directory and the <STDIN>; is the communication system platform that enables it to communicate with the user (Webfiend, 2004).

Therefore, to accomplish the requested command, the user will issue a print ("show working directory," $working directory, "\n\n"); (Webfiend, 2004). Once the user has issued the described command, the user can run the script in which the system will respond by displaying the command and the working directory requested.

In the system display, the user can then type a command to show the content of the working directory. The system will respond by displaying all the content of the working directory suggesting a successful Perl program writing.

Section 4: Writing Perl Programs (Week 4IP)

4.1 Listing of the Perl Script

#!/usr/bin/perl # Group and User Add & Delete Script # February 1, 2016 print "***User Administration Menu ***\n\n"; print "1 - Create UNIX Group\n"; print "2 - Drop UNIX Group\n"; print "3 - Create User Account\n"; print "4 - Drop User Account\n"; print "Please Enter 1, 2, 3, or 4\n"; $value = <STDIN>; if($value=='1'){ print "Create a new group"; print "Enter the group ID"; $id=<STDIN>; print "Enter group name"; $name=<STDIN>; #addgroup “g $group_ID $group_name }elsif($value=='2'){ print "Drop Group\n"; print "Enter group name\n"; $name=<STDIN>; #groupdel $group_name; }elsif($value=='3'){ print "Drop Group\n"; print "Enter group name\n"; $name=<STDIN>; print "Enter UID\n"; $id=<STDIN>; print "Enter default group name\n"; $group_id=<STDIN>; print "Enter default shell\n"; $shell_name=<STDIN>; print "Enter home directory location\n"; $hm_dir=<STDIN>; #adduser “u $user_id “g $group_id - $user_name -$home_dir “m “s $shell_name $user_name; }elsif($value=='4'){ print "Drop user\n"; print "Enter user name\n"; $user_name=<STDIN>; #userdel “rf $user_name; }else{ #message- Invalid option; print"That is not a valid value"; }

4.2 Regular Expressions in Perl

A regular expression (regex in short) is a special text string for describing a search pattern on a given set of strings. Regular expressions are just like mathematical expressions, except that instead of operating on numbers, they operate on characters or strings. They are appropriately used where there are rules when dealing with your strings or characters for instance “replace this but only when it begins with a specific letter.” (Good, 2007)

Regular expressions enable searches and searching and replacing a particular string or character to be intelligent and have much more logic contained in a small space (Good, 2007).

Regular expression can be compared to a pattern which specifies a set of strings or characters and it is said to match certain strings (Fitzgerald, 2012).

Also of importance, there are some scenarios you should not use regular expression for instance when it will take you longer to figure out the expression than to filter bad data manually. For instance, if you know the data well enough, you have an idea of the stray data you can encounter. You can consider manually eliminating this data instead of spending more time coming up with a regular expression to take care of the same situation. (Good, 2007)

Perl has a built in support for regular expressions which is a big reason for the popularity of regexes in the current tech-world. An example is the Search and Replace algorithm whereby Perl supports regular substitution using s/regex/replace/operator. Through this, it adds named capture to the regular expression syntax. (Goyvaerts & Levithan, 2012)

Perl offers an easy way to gain access to regular expressions using two operators: M and S, which allow you to match and replace strings using the logic, packed in regular expressions.

The M operator is a conditional statement which you use in an if block or a while loop. It is basically used for comparing Strings. The S operator is used in String substitutions. A summary of the modifiers is as follows (Good, 2007) :

Modifier What it says

e The right side is also a regular expression

g Make global replacements.

i Ignore the case in the matching pattern

m Superscript and $ match the newline \n (Multiline search)

o Compile the pattern once.

s Allow to match the new line available

x White space in the pattern should be ignored.

Section 5: UNIX Tools (Week 5 IP)

5.1 Final Script Listing

TBD

5.2 Listing of the array (flat file database) file created by IP5’s script “kill task”.

TBD

5.3 Compiling a Program

TBD

5.4 Compiled versus Interpreted Programs

TBD

Section 6: References

Fitzgerald, M. (2012). Introducing Regular Expressions. "O'Reilly Media, Inc.", 2012. Good, N. A. (2007). Regular Expression Recipes: A Problem-Solution Approach. Apress, 2007. Goyvaerts, J., & Levithan, S. (2012). Regular Expressions Cookbook. "O'Reilly Media, Inc.", 2012. Linux corps. (2012). Advanced Bash-Scripting Guide. Retrieved January 17, 2016, from System and Administrative Commands: http://www.tldp.org/LDP/abs/html/system.html McIntire, T. (2006, September 26). developerWorks. Retrieved January 17, 2016, from UNIX tips and tricks for a new user, Part 1: File maintenance tools: http://www.ibm.edu/developerworks/aix/tutorials/au-unixtips1/

Parlante, Z. (2001). Unix Programming Tools. Stanford University, 10-14.

Perl organization. (2015). The Perl Programming Language. Retrieved January 24, 2016, from Learning Perl programming: https://www.perl.org/

Toulas, B. (2014, April 23). World Of Gnome. Retrieved January 9, 2016, from How to Auto-Shutdown your Linux System: http://worldofgnome.org/how-to-auto-shutdown-your-linux-system/

Webfiend. (2004, December 30). Perl Babysteps 1: Your First Simple Scrip. Retrieved January 24, 2016, from Perlmonks: http://www.perlmonks.org/?node_id=418141