Saturday 21 January 2012

Linux Commands, Emacs killed data recover Buffer,rm * command deletes all files


1. Write command to display following text in a shell prompt. Explain your command.

Hi $2, I’m number * (guess)! 20 Marks


The text to be displayed contains special characters, which are generally used in a shell to

perform specific functions. In this case it is required that shell interpret them as characters and

thus it is required that these characters be “escaped”, i.e., preventing shell to try executing

them. Special characters can be escaped by two ways:

i) By placing \ before the special character. In this case we can give the following

command to generate the desired output:

echoHi\$2,I\’m number\*\(gues\)\!

ii) By placing the statement within single quotes, since single quotes can escape any

enclosed character. However, single quotes can not escape itself. Since one single quote

is present in the desired statement, the whole statement can not just be enclosed in

single quotes. Therefore, we can enclose single quote in double quote or escape it using

\.

echo‘Hi$2,I’\’m number*(gues)!’ {Between I and m, first there is

a single quote to close single quote started before Hi, then \ to escape next

single quote and then an opening single quote which is closed after !}

echo‘Hi$2,I’”’”’m number*(gues)!’ {Between I and m, first there is

a single quote to close single quote started before Hi, then a opening double

quote which enclose a single quote and then a closing double quote. Then there

is an opening single quote which is closed after !}

2. In emacs, killed data is stored in a buffer and yanking is used to recover this data when required.
What is this buffer called? How can we look at all the contents of this buffer? How can we
empty this buffer? 15 Marks


The buffer is called kill-ring.

The contents of buffer can be seen by writing kill-ring in a new line in the editor and while cursor

at its right end, press C-x C-e. C-x C-e evaluates LISt Processing (LISP) expression from start of

the line to the current cursor position. The echo area displays the output of the expression.

Evaluating a buffer name in LISP outputs its contents. Hence, in this case evaluating kill-ring

displays its contents in the echo area. M-: is also used to evaluate LISP expression, which means

M-: kill-ring can be used to list its contents. Contents can also be displayed by C-h v kill-ring.

Contents of this buffer can be cleared by writing appropriate expression in LISP and evaluating

it. Expression to clear kill ring is (set ‘kill-ring ‘nil) OR (setq kill-ring ‘nil). Hence, write either one

of them in the editor and while cursor at its right end, press C-x C-e. Kill-ring can also be cleared

by M-: (set ‘kill-ring ‘nil) OR M-: (setq kill-ring ‘nil).

3. Use touch command to create a new file named –i. How different this command is from usual usage of touch to create files? How can you remove the file named –i? 15 Marks

touch command is used to create new blank files or “touch” existing files, i.e., just updating

modification time without modifying file contents. Usual format for touch is:

touch

filename

However, in this case file name has a special character -. Shell expects character(s) implying

option(s) after -. -- can be added before –i filename, since -- implies end of options and shell

expects a filename after that. Thus, command to create file named –i is given as:

touch -- -i

Similarly, this file can be removed by:

rm -- -i

4.
rm *
command deletes all files in the current directory. Does it delete directories and hidden files? If not, what changes in the command are required to delete all contents of working
directory including directories and hidden files. What happens if there are write protected files in the directory? How can we make sure that we delete all write protected files as well, without being asked for confirmation? 20 Marks


rm * does not delete directories and hidden files. In order to delete directories –r option is used

and in order to delete hidden files .* is required in addition to *. Hence command to delete all

directories, files and hidden files is:

rm –r * .*

However, this will also try deleting . and .. entries in the directory. Since, these two can not be

deleted, shell will give error messages that they can not be deleted. Nevertheless, all other files

and directories will be deleted. In order to avoid getting these error messages, following

command can be used:

rm –r .[^.]* *

This will delete everything except . and .. entries. In case there are write protected files, shell

will ask for confirmation for each file before deleting it. In order to avoid getting this

confirmation, -f option can be used which force shell to delete write protected files without

asking for confirmation.

Including –f option, the rm command can be given as:

rm –rf .[^.]* *

5. Explain output of following commands:

echo ‘testing’ > test

grep t test > test

cat test 15 Marks


testing is saved in the file named test. grep command redirects its output to test file as well.

When output redirection is used, a file is created or overwritten before evaluating the

command. Thus, in this case first a test file is overwritten and then t is searched in its contents.

Since, it is now an empty file grep can not find t in it and hence test remains an empty file. Last

command confirms that test is empty by giving no output.

6. Explain the output of following command:

wc –m *|sort –nr|head -2|tail -1

What will be the difference if we use –r instead of –nr. 15 Marks


Multiple pipes are used in the command. First one wc –m * lists character count for each file

and total number of characters in all the files present in the working directory. sort –nr reverse

numerically sorts them, so that total number of characters in all files come at the top as total

has to be higher than any individual entry. The second line would be the file containing highest

number of characters. The combination of head and tail displays the second line, i.e., the file

containing highest number of characters.

Linux Bash Scripting ,Bash Scrip examples


Lab13: Bash Scripting
Lab Tasks:
1. Write a bash script to find out the biggest number from given three numbers. The numbers are supplied as command line argument to the script. Print an error message if sufficient arguments are not supplied.

#!/bin/bash
if(($#!=3));
then
    echo ERROR: Please specify exactly three number as argument;
    exit 1;
else
{
    if [ $1 -gt $2 ]; then
    if [ $2 -gt $3 ]; then
    echo $1 is biggest;
    elif [ $1 -gt $3 ]; then
    echo $1 is biggest;
else
echo $3 is biggest;
fi
else
    if [ $2 -gt $3 ]; then
    echo $2 is biggest;
    else
    echo $3 is biggest;
fi
fi
}
fi


2. Write a bash script to find out the volume of a pizza. Both the radius and height of the pizza should be input by the user and can be real numbers i.e numbers like 3.14259 (Hint: Use read and bc utilities).

#! /bin/bash
echo "Please Enter Height of Pizza"
read height
echo "Please Enter Radius of Pizza"
read radius
result=$(echo 3.14259*$radius*$radius*$height | bc)
echo "Volume of Pizza is =$result"


3. Write a bash script that calculates the factorial of a number given as the argument, using a while or until loop.

#! /bin/bash

if [ $# -ne 1 ]; then
echo 'Only one argument is required'
else
num=$1
fac=1
while [ $num -gt 1 ]
 do
 fac=$(( $fac*$num ))
 num=$(( $num - 1 ))
 done
echo "Factorial is $fac"
fi

4. Write a bash script that checks if the apache and cron daemons are running on your system. If apache/cron is running, the script should print a message like, "Apache/Cron is Alive!!!" (Hint: Use ps to check on processes).
SERVICE='cron'
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
   echo "Cron is Alive!!!!!!!!!"
else
   echo "$SERVICE is not running"
fi
apachee='apache'

if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
   echo "Apache is Alive!!!!!!!!!"
else
   echo "$SERVICE is not running"
fi



Bonus: Write a script that calculates the factorial of a number given as the argument, using a recursive function.



#!/bin/bash
factorial()
{
 if [ $# -ne 1 ];then
 echo "Error: Only one argument is required "
 exit 1
 fi
 local i=$1
 local f
 declare -i i
 declare -i f
 [ $i -le 2 ] && echo $i || { f=$(( i - 1 ));f=$(factorial $f);f=$(( f * i ));echo $f;}
}
factorial $1

Linux Software Installation,Runlevels,XPenguins 2.2,powertop version 1.1 on your Linux System


Lab12: Software Installation

Lab Tasks:


1. Runlevels

· Make Runlevel 3 your default Runlevel

If you have an /etc/inittab file, edit it. Locate the following line:

id:2:initdefault:

Change the number after the first colon to the runlevel you want to be the default.


However, most people won't have that file, in which case you should edit /etc/init/rc-sysinit.conf instead and change the following line:

env DEFAULT_RUNLEVEL=2



· Initiate the single user Runlevel from GRUB console and try to change the root password

Choose the "(recovery mode)" option from GRUB; add "-s", "S" or "single" to the kernel command-line; or from a running machine, run "telinit 1" or "shutdown now".

2. Software Installation



· Install XPenguins 2.2 on your Linux System from its RPM
Rpm -I xpenguins-2.2.rpm //alien is used for rpm packages + install rmp if not installed

xpenguins

· Install powertop version 1.1 on your Linux System from its source code
tar -zxvf powertop-1.1.tar.gz //v for gz

cd powertop-1.1

make

make install

powertop

Linux Job Scheduling Ubuntu

· Create a file test_at in your ~

o Touch test_at;

· Schedule a job that will mv this file to the /tmp directory five minutes from now and append the current date and time in it

o At now+5minutes

o At> Cat date≫~/test_at;

o At> Mv ~/test_at /tmp

o At> CTRL+D

· Create a compressed backup of your home directory on a local /backup directory, which will run at 10 am

o at 10:00am

o at> tar -Cvzf ~/baCkup/home.tar /home/nasir/

· Schedule a job as a normal user that will append the output of the uptime command to a uptime.log file in your home directory every 2 minutes on weekdays but only in the month of July

o */2 * * 6 Mon-Fri uptime >> $HOME/uptime.log

· Schedule a job as a normal user that will run a bash script called report.sh in your home directory every day exactly 23 minutes after every even hour
o 23 0/2 * * * $HOME/report.sh

Process Management,background process,kill process ,current memory usage


Lab10: Process Management
Lab Tasks:

Processes and Services:

· Start a background process, bring that process to foreground and kill that process using PID

xeyes &; // run xeyes in backgroud

fg 1; //make it in foreground

kill %1; // kill process with job id 1 i.e. xeyes

· List all the current running process owned by the current user in user oriented format

ps u U $USER // u option display in user oriented form, U selects the user and $USER is username

· List all the processes only which are in sleep mode and tell the command used for that process

ps u r -N // ‘u r’ find all running process and -N negated the result

· List only the running processes in tree mode

ps f u r // ‘f’ displays in tree mode and ‘u r’ in running process

· Reduce the running time of a running process to 5

nice -n -5 xeyes; // reduce running time of xeyes by 5

· Start xinetd service and stop that service

/etc/init.d/xinetd start; /etc/init.d/xinetd status; /etc/init.d/xinetd stop; // You can check the command also by starting and stoping ssh service.

· Figure out the current memory usage and CPU usage

Top // displays all the information about cpu, memory and processes.

Monitoring Linux


Monitoring a Linux System
·       Search the boot log to see all the section where the network interface has been mentioned
dmesg | grep eth0                  
dmesg | grep network | grep interface
·       Find out what is the cache size of your PC’s processor
vmstat     (or hwinfo)   
·       Find out the IRQ number associated with the interrupts generated by your Ethernet card
dmesg | grep irq
·       Find out the PCI Bus IDs of the USB Controllers on your PC
lspci
·       Start the ssh service on your PC and establish a local connection to it by using ssh username@localhost
o   Use netstat to find out the process ids of all TCP connections established on your machine
sudo apt-get install ssh;
sudo /etc/init.d/ssh start   or sudo service ssh start;
netstat
·       Which version of Linux kernel is currently installed on your system?
uname -mrs (-a all)
·       Show the duration of last 5 logins at your system
last -5

RAID volume group and logical volumes,Software RAID based VG,Linux RAID


1.    Create the volume group and logical volumes
·        Create a primary partition of 100 MB using fdisk and choose its mount point as /boot
·        Create 3 logical partitions of sizes 2, 4 and 6 GB and for the File system ID select Linux LVM (Do not format or create a file system on these partitions)
·        Commit these changes and reboot (or use partprobe)
·        Select LVM in the YaST Expert Partitioner
·        Create a VG named systemVG
o   Physical Extent Size: The physical extent size defines the smallest unit of a logical volume group
·        Add the 3 PVs to your systemVG volume group
·        Create 4 LVs in your systemVG volume group with following characteristics:-
·        2 GB LV called root mounted at / formatted with ReiserFS
·        1 GB LV called home mounted at /home formatted with ext2
·        3 GB LV called linuxStuff mounted at /usr formatted with xfs
·        2 GB LV called virtualMem formatted as swap
Try resizing the home LV and see what happens
2.    Create a Software RAID based VG
·        Create two partitions of exact same size (10 GB each) and for the File system ID select Linux RAID Auto
·        Use the YaST RAID Wizard to set them up as RAID 0 (Stripping mode)
·        Create a VG called fastVG and add this RAID PV to it
·        Create a LV on it formatted with JFS and mounted on /moodle

Oracle Partitioning Overview

Friday 20 January 2012

Victory of Pakistan against England in Dubai 2012 IndiaTv Report

 
Free Host | lasik surgery new york