script



BASH script:
1) simple calculator using bash

#!/bin/bash

echo -e "Bash Calculator\n"

echo -e "Enter two value for calculation"
read a
read b
echo -e "Please Enter 1 for add, 2 for sub, 3 for multiplay \n"
read -n 1 i
let c=0
if [ $i -eq 1 ]
then
echo -e "You have selected add\n"
let c=$((a+b))
echo $c
elif [ $i -eq 2 ]
then
echo -e "You have selected sub\n"
let c=$((a-b))
echo $c
elif [ $i -eq 3 ]
then
echo -e "You have selected mul\n"
let c=$((a*b))
echo $c
else
echo " You have enterd a wrong option\n please enter with in 1-3"
fi



Python Script:
1) simple calculator using python

#!/usr/bin/env python

print "Python calculator\n"
print "Enter the value of A and B\n"
a = int(raw_input("value of A:"))
b = int(raw_input("value of B:"))
opp = int(raw_input("Enter the calculation operation\n 1 for add 2 for sub 3 for mul\n"))
if opp==1:
        print "Selected add operation"
        c = a + b
        print c
elif opp==2:
        print "Selected sub operation"
        c = a - b
        print c
elif opp==3:
        print "Selected mul operation"
        c = a * b
        print c
else:
        print "Wrong choice"



Perl:


i)Find a file which has large size:-
file(bash script) one in section i):--
file name: scr.sh



#!/bin/bashPublish Page
#Author : s.manikandan
#purpose: find a file which has large size 
#this is just a extraction of data (or) an operation for get a size's of a suspect directory, which we wanted to analyse 

#this line for get file size

ls -sl ~/mani > /size/file

#this is for exptact the row which contains size's of file

awk ' { print $1 }' /size/file > /size/file1


#this is for get the numeric value which means this will omit the first line of the output (total )

grep '[0-9]' /size/file1 > /size/file2



file2(perl script)  in section i):--
filename: anything.pl




#Author : s.manikandan
#purpose: find a file which has large size
#file which contains every files size, got it by bash script
#my convenience i created my dump files like /size/src.sh ... , but you can write your way

#! /use/bin/perl
Use warnings;
#execute file that created for my rough work(getting file size that is)
`/bin/bash /size/scr.sh`;
@ara=`cat /size/file2`;
$var=@ara[0];
{
for($i=1;i<=$#ara;$i++) {
If($var <= $ara[$i])
{
$var=$ara[$i];
}
}
Print “highest size of the file in the directory $var”;
}


DataStructure C Code:

    A point system is maintained to keep track of erring drivers, and vehicle owners. The objective for this lab is to comeup with a software to keep track of penalties incurred by the drivers

File 1: listops.h

void insertVehicle( vehicle **vehicleList, vehicle *this_vehicle); //Will return SUCCESS or FAILURE
void insertDriver( drivers **driverList, drivers *this_driver); //Will return SUCCESS or FAILURE
long long int lookupUID( vehicle *vehicleList, char *vehicle_number); //Will return the owner UID
char *lookupVehicle( vehicle *vehicleList, long long int owner_uid); //Will return the vehicle_number
drivers *lookupDriverDetails( drivers *driverList, long long int driver_uid); //Will return the Driver index from List

File 2 : listops.c

#include <Offense.h>
#include <stdlib.h>
#include <stdio.h>

void insertVehicle( vehicle **vehicle_list, vehicle *this_vehicle)
{
if( (this_vehicle == NULL) || (this_vehicle->next_vehicle != NULL))
{
fprintf(stderr,"insertVehicle: This is a NULL vehicle.\n");
return;
}
if( *vehicle_list == NULL)
{
*vehicle_list = this_vehicle;
return;
}

vehicle *vl  = *vehicle_list;
int temp = strcmp( vl->vehicle_number, this_vehicle->vehicle_number);

if( temp > -1)
{
this_vehicle->next_vehicle = vl;
*vehicle_list = this_vehicle;
return;
}

while( 1)
{
if( vl->next_vehicle == NULL )
{
vl->next_vehicle = this_vehicle;
return;
}

temp = strcmp( ((vehicle *)vl->next_vehicle)->vehicle_number, this_vehicle->vehicle_number);
if( temp > -1) break;

vl = vl->next_vehicle;

}

this_vehicle->next_vehicle = vl->next_vehicle;
vl->next_vehicle = this_vehicle;
}

void insertVehicle1( vehicle **vehicle_list, vehicle *this_vehicle)
{
if( (this_vehicle == NULL) || (this_vehicle->next_vehicle != NULL))
{
fprintf(stderr,"insertVehicle: This is a NULL vehicle.\n");
return;
}
if( *vehicle_list == NULL)
{
//fprintf(stderr,"populateVehicles: owner_uid = %lld, vehicle_number = %s\n",
// this_vehicle->owner_uid, this_vehicle->vehicle_number);
*vehicle_list = this_vehicle;
return;
}

vehicle *vl  = *vehicle_list;
vehicle *prev_vehicle = *vehicle_list;
/*

while( (strcmp( vl->vehicle_number, this_vehicle->vehicle_number) < 1) && (vl->next_vehicle != NULL))
{
prev_vehicle = vl;
vl = vl->next_vehicle;
}

this_vehicle->next_vehicle = vl;
if( prev_vehicle != vl)
{
prev_vehicle->next_vehicle = this_vehicle;
}

vl = *vehicle_list;
*/
int counter = 0;

while( vl != NULL)
{
if(strcmp( vl->vehicle_number, this_vehicle->vehicle_number) < 0)
{
prev_vehicle = vl;
vl = vl->next_vehicle;
//if(vl->next_vehicle != NULL)
{
// fprintf(stderr,"AAAAAAAAAAAAAAAAAAAAAAA %s \n", vl->vehicle_number);
}
}
else
{
fprintf(stderr,"BBBBBBBBBBBBBBBBBBBBBBB\n");
break;
}
counter++;
}


if(vl->next_vehicle == NULL)
{
vl->next_vehicle = this_vehicle;
}
else
{
prev_vehicle->next_vehicle = this_vehicle;
this_vehicle->next_vehicle = vl;
}

counter++;
fprintf(stderr,"Inserted the vehicle. total vehicles = %d\n",counter);
}

void insertDriver( drivers **driver_list, drivers *this_driver)
{
if( this_driver == NULL)
{
fprintf(stderr,"insertDriver: This is a NULL driver.\n");
return;
}
if( *driver_list == NULL)
{
*driver_list = this_driver;
return;
}
this_driver->next_driver = *driver_list;
*driver_list = this_driver;
}

long long int lookupUID( vehicle *this_vehicle, char *vehicle_number)
{
while((this_vehicle != NULL) && (strcmp(vehicle_number,this_vehicle->vehicle_number) != 0))
{
//fprintf(stderr,"lookupUID: Comparing %s & %s\n", this_vehicle->vehicle_number, vehicle_number);
this_vehicle = this_vehicle->next_vehicle;
if(this_vehicle == NULL) return -1;
}
fprintf(stderr,"lookupUID: owner_uid = %lld\n",this_vehicle->owner_uid);
return (long long int)182934502409;//this_vehicle->owner_uid;
}

drivers *lookupDriverDetails( drivers *this_driver, long long int this_vehicle_owner_uid)
// This above API is not as per requirement. But it is better.
{
while((this_driver != NULL) && (this_driver->driver_uid != this_vehicle_owner_uid))
{
this_driver = this_driver->next_driver;
}
return this_driver;
}

char* lookupVehicle( vehicle *this_vehicle, long long int driver_uid)
{
if( this_vehicle == NULL)
{
return NULL;
}

while( this_vehicle->owner_uid != driver_uid)
{
this_vehicle = this_vehicle->next_vehicle;
if( this_vehicle == NULL)
{
return NULL;
}
}

return this_vehicle->vehicle_number;
}

File 3: Offence.h

typedef struct vcle
{
char vehicle_number[8]; // 3 chars + 4 digits
long long int owner_uid; // UID is a unique identifier, and it contains 12 digits
void *next_vehicle;
}vehicle;

typedef struct drvr
{
long long int license_number;
long long int driver_uid;
short score;
void *next_driver;
}drivers;

typedef struct rl
{
long long int license_number;
char vehicle_number[8];
long long int driver_uid;
long long int number_of_new_revokees;
void *next_revokee;
}revoke_list;

typedef struct ots
{
vehicle *vehicleList;
drivers *driverList;
revoke_list *revokeList;
long long int number_of_new_revokees;
}offense_tracking_system;


File 4: OffenceOps.h

vehicle* populateVehicles( offense_tracking_system *ots, char *owners_list_file);
drivers* populateDrivers( offense_tracking_system *ots, char *drivers_list_file);
void updateOffenses( offense_tracking_system *ots, char *offenses_list_file);
void markRevokedDrivers( offense_tracking_system *ots, char *revoke_list_file);

File 5: OffenceOpc.c

#include <Offense.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <listOps.h>

vehicle* populateVehicles( offense_tracking_system *ots, char *owners_list_file_name)
{
FILE *owners_list_file = fopen(owners_list_file_name,"r");
if( owners_list_file == NULL)
{
fprintf(stderr,"populateVehicles: fopen failed for %s !!\n",owners_list_file_name);
return NULL;
}
else
{
fprintf(stderr,"populateVehicles: fopen successful for %s !!\n",owners_list_file_name);
}

vehicle *this_vehicle;
while( !feof(owners_list_file))
{
//-- Create this vehicle --
this_vehicle = (vehicle *)malloc(sizeof(vehicle));      
this_vehicle->next_vehicle = NULL;
fscanf(owners_list_file,"%lld \t %s\n", &this_vehicle->owner_uid, this_vehicle->vehicle_number);
//fprintf(stderr,"populateVehicles: owner_uid = %lld, vehicle_number = %s\n",
// this_vehicle->owner_uid, this_vehicle->vehicle_number);
       
//-- Insert this vehicle into the list --
insertVehicle( &ots->vehicleList, this_vehicle);
}

return ots->vehicleList;
}

drivers* populateDrivers( offense_tracking_system *ots, char *drivers_list_file_name)
{
FILE *drivers_list_file = fopen(drivers_list_file_name,"r");
if( drivers_list_file == NULL)
{
fprintf(stderr,"populateDrivers: fopen failed for %s !!\n",drivers_list_file_name);
return NULL;
}

while( !feof(drivers_list_file))
{
//-- Create this driver --
drivers *this_driver = (drivers *)malloc(sizeof(drivers));      
this_driver->next_driver = NULL;

fscanf(drivers_list_file,"%lld \t %lld\n", &this_driver->driver_uid, &this_driver->license_number);
this_driver->score = 10;

//-- Insert this driver into the list --
insertDriver( &ots->driverList, this_driver);
}

return ots->driverList;
}

void updateOffenses( offense_tracking_system *ots, char *offenses_list_file_name)
{
FILE *offenses_list_file = fopen(offenses_list_file_name,"r");
if( offenses_list_file == NULL)
{
fprintf(stderr,"updateOffenses: fopen failed for %s !!\n",offenses_list_file_name);
return;
}

char vehicle_number[8];
int offense;

while( !feof(offenses_list_file))
{
fscanf( offenses_list_file,"%s \t %d\n", vehicle_number, &offense);

long long int this_vehicle_owner_uid;
this_vehicle_owner_uid = lookupUID( ots->vehicleList, vehicle_number);
fprintf(stderr,"lookupUID: vehicle_number = %s, owner_id = %lld\n", vehicle_number, this_vehicle_owner_uid);
/*
if( this_vehicle_owner_uid < 1)
{
fprintf(stderr,"DANGER!! Got a vehicle with no entry in vehile_list.."
" Indicates FAKE vehicle_number = %s\n",vehicle_number);
continue;
}
drivers *this_driver;
this_driver = lookupDriverDetails( ots->driverList, this_vehicle_owner_uid);
if( this_driver == NULL)
{
fprintf(stderr,"DANGER!! Got a owner with no entry in driver_list.."
" Indicates this owner( = %lld) does not have a driving license\n",this_vehicle_owner_uid);
continue;
}

fprintf(stderr,"updateOffenses: %s %d %lld %d",
vehicle_number, offense, this_driver->license_number, this_driver->score );

this_driver->score -= 5^offense;
if(this_driver->score < 1) ots->number_of_new_revokees++; //This feature is also not asked. But will optimize.

fprintf(stderr,"updateOffenses: %s %d %lld %d",
vehicle_number, offense, this_driver->license_number, this_driver->score );
*/
}
}

void markRevokedDrivers( offense_tracking_system *ots, char *revoke_list_file_name)
{
if( ots->number_of_new_revokees == 0)
{
fprintf(stderr,"No pending revokees to process....");
return;
}

FILE *revoke_list_file = fopen("r",revoke_list_file_name);
if( revoke_list_file == NULL)
{
fprintf(stderr,"markRevokedDrivers: fopen failed for %s !!\n",revoke_list_file_name);
return;
}

revoke_list *temp_rl = ots->revokeList;
while( temp_rl->next_revokee != NULL) temp_rl = temp_rl->next_revokee;

revoke_list *revokeList;
drivers *this_driver = ots->driverList;

for(this_driver = ots->driverList; (this_driver != NULL) && (ots->number_of_new_revokees > 0);
this_driver = this_driver->next_driver)
{
if( this_driver->score > 0)
{
continue;
}

// We have to write the revokeList info to revoke.txt now.

revokeList = (revoke_list *)malloc(sizeof(revoke_list));

char *vehicle_number = lookupVehicle( ots->vehicleList, this_driver->driver_uid);
if( vehicle_number == NULL)
{
fprintf(stderr,"FATAL ERROR!! This driver(%lld) is not listed in Vehiles List...\n",
this_driver->driver_uid);
exit(0);
}

strcpy( revokeList->vehicle_number, vehicle_number);

revokeList->driver_uid = this_driver->driver_uid;
revokeList->license_number = this_driver->license_number;
revokeList->next_revokee = NULL;

temp_rl->next_revokee = revokeList;
temp_rl = temp_rl->next_revokee;
ots->number_of_new_revokees --;
}
}


File 5: Offenses.txt 

WBS2481 1
MQS2406 0
UIQ2942         1
SOI2044         0
ZAM2099 1
WBS2065 1

File 6: drives.txt

182930101222 9929929921
182930276659 9930101687
182930341611 9930279780
182930439017 9930349060
182930486609 9930520952
182930495583 9930580165


File 7: owner.txt

182930101222 KLA1512
182932877483 OER1515
182932296902 OER1519
182930617681 SOI1522
182931618241 OER1523
182934163430 XYZ1527





~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ii)
get the process process id(pid) and kill it without human interaction:-
file(bash script)  one in section ii):--
file name: kill.sh





#/bin/bash 

#purpose : get the process process id(pid) and kill it without human interaction 
#this might be helpfull  when zombie created we don't need to  go and kill the processes manually

# this is for get the pid's
ps -C httpd > file2; 

#this is for first row extraction which is a pid
awk ' { print $1 } ' file2 > file3; 

#this is for removing headlines like pid
grep [0-9] file3 > file4;

file2
(perl script)
 in section ii):--
file name: anything.pl





!/usr/bin/perl -w 

#executing the bash script file which contains get the command for extract pid


#this line for call bash script
`/bin/bash /killps/kill.sh`; 

#i store pid's in array

@ara=`cat /killps/file4`; 

#i get every pid's from array and kill with forcefully

foreach(@ara) 
{
`kill -9 $_`; 
print "$_"; 
}



OR





This easy way can be done for kill a process;- 
(this is only for a critical situation but in real production servers we can't or we shouldn't do this) 


#!/usr/bin/perl -w
@pid=`pgrep httpd(any daemon)`;
foreach(@pid)
{
`kill -9 $_`;
}

output:-
processes running under httpd will stopped






No comments:

Post a Comment