Thursday, 26 December 2013
Saturday, 21 December 2013
Bsc Computer Science Lab program : Linux Programming
Linux Programming
- Find the sum of the digits of a given number
- Find the reverse of a number
- Perform basic arithmetic operations using case
- Display multiplication table
- Check whether a number is prime or not using while
- Convert lowercase to uppercase using tr statement
- Check for an adam number
- Check pattern matching using grep
- Find the number of users who have logged in
12. Write
a menu driven program to display today’s date, Processes of the system, user’s of the system, list files of the system
19. To
create and append a file
Linux or Unix lab programs
Linux or Unix lab programs
1.Bsc ( Computer Science )
2.BCA
3.Msc ( CS & IT )
Write a linux shell program to perform find age of a person using set date
Find age of a person using set date
Code:
TODAY=$(date +"%Y-%m-%d")echo "May I know your name"
read name
echo "Your birthdate(yyyy-mm-dd)"
read birthdate
tmpDays=$( printf '%s' $(( $(date -u -d"$TODAY" +%s) - $(date -u -d"$birthdate" +%s))) )
age=$(( $tmpDays / 60 / 60 / 24 / 364 ))
echo Hai $name you are $age years old.
Output:
May I know your nameSatheesh
Your birthdate(yyyy-mm-dd)
1989-12-08
Hai Satheesh you are 24 years old.
Developing simple exercises in Unix Graphics.
Developing simple exercises in Unix Graphics.
Code:
clearname=$(zenity --entry --title="Greeting user" --text="Enter your Name ")
d=$(date '+%H')
if [ $d -ge 0 ] && [ $d -lt 12 ]
then
zenity --info --title="Greeting user" --text="Good Morning $name"
elif [ $d -ge 12 ] && [ $d -lt 15 ]
then
zenity --info --title="Greeting user" --text="Good Afternoon $name"
else
zenity --info --title="Greeting user" --text="Good Evening $name"
fi
Output:
Writing C program to implement UNIX commands like grep.
Writing C program to implement UNIX commands like grep.
Code:
Output:
elcot@elcot:~$ gcc -o g1 grepc.celcot@elcot:~$ ./g1 hai test1
hai how are you
Hints:
elcot@elcot:~$ gcc -o g1 grepc.c ->Compilegrepc.c ->program name
g1 -> create object
elcot@elcot:~$ ./g1 hai test1 -> Run
g1 -> object
hai -> string
test1 -> file name
This is grep command implementation.
grep syntax:
grep <String> <file name>
grep command is used to search a particular word in file.etc.
Study of UNIX filters like grep, tr, sed etc.
Study of UNIX filters like grep, tr, sed etc.
Code:
clearecho -------------------------------------------------
echo '\tStudying grep,tr,sed commands'
echo -------------------------------------------------
echo Enter the file name
read f1
echo Number of lines in a file is :$(sed -n '$=' $f1)
echo Number of blank lines in a file : $(grep -c '^$' $f1)
echo File content in Uppercase
tr '[ a-z ]' '[ A-Z ]' < $f1
Output:
-------------------------------------------------Studying grep,tr,sed commands
-------------------------------------------------
Enter the file name
t2
Number of lines in a file is :5
Number of blank lines in a file : 2
File content in Uppercase
THIS IS MY
FIRST PROGRAM
SEE SOURCECODEPOINT.BLOGSPOT.COM
Write a shell script that reads lines in a file and echo the line followed by the string “This line is line n” n=1, 2, 3…
Write a shell script that reads lines in a file and echo the line followed by the string “This line is line n” n=1, 2, 3…
Code:
clearecho -------------------------------------------------
echo '\tNumbers for each line'
echo -------------------------------------------------
echo Enter the file name
read fname
sed = $fname | sed 'N;s/\n/\. /'
Output:
Write a shell script that changes the text color
Write a shell script that changes the text color
Code:
# Hints# run in bash.For example :~$bash color1
clear
echo ---------------------------------------------
echo " Change text color"
echo ---------------------------------------------
echo -e "\e[33mThis is yellow\e[0m"
Output:
Hints:
In a program 33 is a color codeIf you want change text color. You should change number only.
Some Color code:
White - 1
Red - 31
Green - 32
Yellow -33
Blue -34
Write a shell script that counts the number of blank lines in a file, count how many lines have a text ‘new’ in it, count how many lines have the last word having a value > 1000
Write a shell script that counts the number of blank lines in a file, count how many lines have a text ‘new’ in it, count how many lines have the last word having a value > 1000
Code:
clear
echo ----------------------------------
echo '\tNumber of lines'
echo ----------------------------------
echo Enter the file name
read f1
tl=$(sed -n '$=' $f1)
echo Total lines in a file is :$tl
bl=$(grep -c '^$' $f1)
echo Number of blank lines in a file : $bl
sl=$(expr $tl - $bl)
echo Number of text lines in file :$sl
Output:
----------------------------------
Number of lines
----------------------------------
Enter the file name
t2
Total lines in a file is :6
Number of blank lines in a file : 3
Number of text lines in file :3
Write a shell script that checks if the contents of two files are same. If so, delete the second file.
Write a shell script that checks if the contents of two files are same. If so, delete the second file.
Code:
clear
echo ---------------------------------------------------
echo '\tCheck the file , Delete if it same'
echo ---------------------------------------------------
echo Enter the first file name
read first
echo Enter the second file name
read second
if cmp $first $second
then
rm $second
echo The file is deleted.
else
echo Not Equal.
fi
Output:
---------------------------------------------------
Check the file , Delete if it same
---------------------------------------------------
Enter the first file name
vvv
Enter the second file name
vvv1
The file is deleted.
Write a shell script that displays the calendar for present month, current working directory, and the files with the extension sash
Write a shell script that displays the calendar for present month, current working directory, and the files with the extension sash
Code:
#! /bin/sash -f
#Hints-1
#Open gedit and type the following program and Save with extension .sash and run $ sh programname.sash
#or
#Hints-2
# echo " " >> programname.sash ,type this line in last below a program and run sh programname and see the directory
#programname.sash file was created.
clear
echo The calendar for present month
cal
echo Current working directory
pwd
Output:
The calendar for present month
December 2013
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Current working directory
/home/elcot
Current working directory
/home/elcot
Write a shell script that searches for a particular pattern in files and displays the name of such files
Write a shell script that searches for a particular pattern in files and displays the name of such files
Code:
clear
echo ------------------------------------------------------------
echo '\tsearches for a particular pattern in files'
echo ------------------------------------------------------------
echo Enter the word to searches for a particular pattern in files
read wname
echo Result a particular pattern:
for File in *
do
if [ -r $File ]
then
if [ $(grep $wname $File | wc -w) -gt 0 ]
then
echo $File.
fi
fi
done
Output:
------------------------------------------------------------
searches for a particular pattern in files
------------------------------------------------------------
Enter the word to searches for a particular pattern in files
satheesh
Result a particular pattern:
name_a.
name_asce.
name_desc.
satheesh.
vino.
Write a shell script to read user name and find whether the user is currently working in the system or not
Write a shell script to read user name and find whether the user is currently working in the system or not
Code:
clearecho ---------------------------------------------
echo '\tUser currently working or not'
echo ---------------------------------------------
echo Enter the user name
read name
if who | grep $name
then
echo User is working currently.
else
echo User is not working currently.
fi
Output:
---------------------------------------------User currently working or not
---------------------------------------------
Enter the user name
elcot
elcot tty7 2013-12-21 11:02 (:0)
elcot pts/0 2013-12-21 11:06 (:0.0)
User is working currently
Write a shell script to all files whose size is greater than specific size.
Write a shell script to all files whose size is greater than specific size.
Code:
clearecho -------------------------------
echo '\tFile List'
echo -------------------------------
echo Enter the file size
read s1
echo Files list whose size is greater than $s1 bytes size.
for File in *
do
if [ -f $File ]
then
s2=$(stat -c %s $File)
if [ $s2 -gt $s1 ]
then
echo $File
fi
fi
done
Output:
-------------------------------
File List
-------------------------------
Enter the file size
1025
Files list whose size is greater than 1025 bytes size.
f1
satheesh1.pls
yuvan.pls
Write a shell script to get three arguments from the command line operand 1, operand 3 and perform all arithmetic operations
Write a shell script to get three arguments from the command line operand 1, operand 3 and perform all arithmetic operations
Code:
if [ $# -ne 3 ]then
echo "$0:Given argument was wrong.Example sh programname 24 13 + " >&2
exit 1
fi
echo -----------------------------------------------------------
echo '\tArithmetic Expression using command argument'
echo -----------------------------------------------------------
a=$1
b=$2
op=$3
case $op in
'+')echo Addition : $(expr $a + $b);;
'-')echo Suubtraction : $(expr $a - $b);;
'*')echo Multiplication : $(expr $a \* $b);;
'/')echo Division : $(expr $a / $b);;
'%')echo Modules : $(expr $a % $b);;
*)echo This is not a choice
esac
Output:
elcot@elcot:~$ sh sum 24 12 /
-----------------------------------------------------------
Arithmetic Expression using command argument
-----------------------------------------------------------
Division : 2
Hints:
Example for run a program to all expression
elcot@elcot:~$ sh sum 24 12 +
elcot@elcot:~$ sh sum 24 12 -
elcot@elcot:~$ sh sum 24 12 “*”
elcot@elcot:~$ sh sum 24 12 /
elcot@elcot:~$ sh sum 24 12 %
Write a shell script to print the names of all files in the current working directory in alphabetical order using for loop
Write a shell script to print the names of all files in the current working directory in alphabetical order using for loop
Code:
clearecho -------------------------------------------------------
echo '\t file Names in current working directory'
echo -------------------------------------------------------
for File in *
do
if [ -f $File ]
then
echo $File
fi
done
Output:
Write a shell script to get your name and greet you depending on time
Write a shell script to get your name and greet you depending on time
Code:
clearecho -------------------------------------------------
echo '\tGreeting user depending upon time'
echo -------------------------------------------------
echo Enter your name
read name
d=$(date '+%H')
echo " "
if [ $d -gt 1 ] && [ $d -lt 12 ]
then
echo Good Morning $name
elif [ $d -ge 12 ] && [ $d -lt 15 ]
then
echo Good Afternoon $name
else
echo Good Evening $name
fi
Output:
-------------------------------------------------
Greeting user depending upon time
-------------------------------------------------
Enter your name
Satheesh
Good Morning Satheesh
Write a linux shell program to perform count number of user’s login and print first login user information.
Write a shell script to count number of user’s login and print first login user information.
Code:
clear
echo --------------------------
echo '\tLogin User'
echo --------------------------
echo The number of users loggin in are
who | wc -l
echo First login user
who | head -1
Output:
--------------------------
Login User
--------------------------
The number of users loggin in are
2
First login user
elcot tty7 2013-12-21 11:02 (:0)
Thursday, 19 December 2013
Linux basic shell scripting questions
Basic shell scripting questions
1.
How do you find out what’s your shell? - echo $SHELL
2. What’s
the command to find out today’s date? - date
3. What’s
the command to find out users on the system? - who
4. How
do you find out the current directory you’re in? - pwd
5.
How do you remove a file? - rm
6. How
do you remove a <=""
b="">- rm -rf
7. How
do you find out your own username? -
who am i
8. How
do you send a mail message to somebody? -
mailsomebody@techinterviews.com -s ‘Your subject’ -c ‘cc@techinterviews.com‘
9. How
do you count words, lines and characters in a file? - wc
10. How
do you search for a string inside a given file? - grep string filename
11. How
do you search for a string inside a directory? - grep string *
12. How
do you search for a string in a directory with the subdirectories recursed? - grep -r string *
13. What
are PIDs? -
They are process IDs given to processes. A PID can vary from 0 to 65535.
14. How
do you list currently running process? -
ps
15. How
do you stop a process? -
kill pid
16. How
do you find out about all running processes? - ps -ag
17. How
do you stop all the processes, except the shell window? - kill 0
18. How
do you fire a process in the background? -
./process-name &
19. How
do you refer to the arguments passed to a shell script? - $1, $2 and so on. $0 is your script name.
20. What’s
the conditional statement in shell scripting? - if {condition} then … fi
21. How
do you do number comparison in shell scripts? - -eq, -ne, -lt, -le, -gt, -ge
22. How
do you test for file properties in shell scripts? - -s filename tells you if the file is not empty, -f filename
tells you whether the argument is a file, and not a directory, -d filename
tests if the argument is a directory, and not a file, -w filename tests for
writeability, -r filename tests for readability, -x filename tests for
executability
23. How
do you do Boolean logic operators in shell scripting? - ! tests for logical not, -a tests for logical and, and -o tests
for logical or.
24. How
do you find out the number of arguments passed to the shell script? - $#
25. What’s
a way to do multilevel if-else’s in shell scripting? - if {condition} then {statement} elif {condition} {statement} fi
26. How
do you write a for loop in shell? -
for {variable name} in {list} do {statement} done
27. How
do you write a while loop in shell? -
while {condition} do {statement} done
28. How
does a case statement look in shell scripts? - case {variable} in {possible-value-1}) {statement};;
{possible-value-2}) {statement};; esac
29. How
do you read keyboard input in shell scripts? - read {variable-name}
30. How
do you define a function in a shell script? - function-name() { #some code here return }
31. How
does getopts command work? -
The parameters to your script can be passed as -n 15 -x 20. Inside the script,
you can iterate through the getopts array as while getopts n:x option, and the
variable $option contains the value of the entered option.
What are PIDs in linux?
What are PIDs ?
They are process IDs given to processes. A PID can vary from 0 to 65535.What are links and symbolic links in UNIX file system?
What are links and symbolic links in UNIX file system?
Link is a utility program in UNIX which establishes a hard link from one directory to another directory....What is use of sed command?
What is use of sed command?
Sed command reads from a standard input and places it into the pattern space.......How can shell scripts be debugged in unix?
How can shell scripts be debugged in unix?
A user when programming in shell may face an error. The user can make use of the -v and -x option with sh or bash command to debug the shell script. The syntax format of using the option is :sh option {shell-script-name} or
bash option {shell-script-name}
The option in the above syntax can be either -v or -x. The -v option prints the shell input lines as they are read. The -x option on the other hand expands a simple command and along with it displays the value of the ps4 variable followed by the command. It also shows the complete list of expanded arguments.
What is GUI Scripting?
What is GUI Scripting?
Graphical user interface provided the much needed thrust for controlling a computer and its applications. This form of language simplified repetitive actions. Support for different applications mostly depends upon the operating system. These interact with menus, buttons, etc.Explain the “Exit” command?
Explain the “Exit” command?
Every program whether on UNIX or Linux should end at a certain point of time and successful completion of a program is denoted by the output 0. If the program gives an output other than 0 it defines that there has been some problem with the execution or termination of the problem. Whenever you are calling other function, exit command gets displayed.What is shell variable?
What is 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. Some of these variables are environment variables whereas others are local variables.What is environment variable?
What is environment variable?
An environment variable is a variable that is available to any child process of the shell. Some programs need environment variables in order to function correctly. Usually a shell script defines only those environment variables that are needed by the programs that it runs.What is Interactive mode Linux?
What is Interactive mode Linux?
Interactive mode means that the shell expects to read input from you and execute the commands that you specify. This mode is called interactive because the shell is interacting with a user. This is usually the mode of the shell that most users are familiar with: you log in, execute some commands, and log out. When you log out using the exit command, the shell exits.What is "grep" programe ?
What is "grep" programe ?
The grep program is the primary tool for extracting interesting lines of text from input datafiles. POSIX mandates a single version with different options to provide the behavior traditionally obtained from the three grep variants: grep, egrep, and fgrep.What is awk ?
What is awk ?
An awk invocation can define variables, supply the program, and name the input files.What is Shell's Responsibilities ?
What is Shell's Responsibilities ?
The shell is responsible for the execution of all programs that you request from your terminal. Each time you type in a line to the shell, the shell analyzes the line and then determines what to do.The line that is typed to the shell is known more formally as the command line. The shell scans this command line and determines the name of the program to be executed and what arguments to pass to the program.Linux is which kind of Operating System?
Linux is which kind of Operating System?
Linux is an Operating System which supports Multi User, Running a Number of Processes performing different tasks simultaneously.Is Linux Operating system Virus free?
Is Linux Operating system Virus free?
No! There doesn’t exist any Operating System on this earth that is virus free. However Linux is known to have least number of Viruses, till date, yes even less than UNIX OS. Linux has had about 60-100 viruses listed till date. None of them actively spreading nowadays. A rough estimate of UNIX viruses is between 85 -120 viruses reported till date.What is the basic difference between UNIX and Linux Operating System.
What is the basic difference between UNIX and Linux Operating System.
Linux Operating System is Free and Open Source Software, the kernel of which is created by Linus Torvalds and community. Well you can not say UNIX Operating System doesn’t comes under the category of Free and Open Source Software, BSD, is a variant of UNIX which comes under the category of FOSS. Moreover Big companies like Apple, IBM, Oracle, HP, etc. are contributing to UNIX Kernel.What is the core of Linux Operating System?
What is the core of Linux Operating System?
Kernel is the core of Linux Operating System. Shell is a command Line Interpreter, Command is user Instruction to Computer, Script is collection of commands stored in a file and Terminal is a command Line Interface.Write a linux shell program to perform string manipulation
To perform string manipulation
Code:
clear
echo ---------------------------------------------
echo '\tString Manipulation'
echo ---------------------------------------------
echo Enter the String1
read s1
echo Enter the String2
read s2
s3=$s1$s2
len=$(expr length $s3)
lstring=$(expr $s3 | tr '[:upper:]' '[:lower:]')
ustring=$(expr $s3 | tr '[:lower:]' '[:upper:]')
echo "\nConcatinated String : $s3"
echo String Length : $len
echo String in Lowercase : $lstring
echo String in Uppercase : $ustring
Output:
---------------------------------------------
String Manipulation
---------------------------------------------
Enter the String1
as
Enter the String2
WIN
Concatinated String : asWIN
String Length : 5
String in Lowercase : aswin
String in Uppercase : ASWIN
Write a linux shell program to perform compare two files
To compare two files
Code:
clear
echo -----------------------------------
echo '\tCompare two files'
echo -----------------------------------
echo Enter the name of the First file
read first
echo Enter the name of the Second file
read second
cmp $first $second
Output:
-----------------------------------
Compare two files
-----------------------------------
Enter the name of the First file
satheesh
Enter the name of the Second file
ex1
satheesh ex1 differ: byte 1, line 1
Write a linux shell program to perform to check the given file is a directory or not
To check the given file is a directory or not
Code:
clear
echo ---------------------------------------------
echo '\t Check directory or not'
echo ---------------------------------------------
echo Enter the file name
read fname
if [ -d $fname ]
then
echo It is a directory.
else
echo It is not a directory.
fi
Output:
---------------------------------------------
Check directory or not
---------------------------------------------
Enter the file name
sat
It is not a directory.
Write a linux shell program to perform set the attributes of a given file
To set the attributes of a given file
Code:
clear
echo ---------------------------------------------
echo '\tSetting The Attributes of A File'
echo ---------------------------------------------
echo Enter the name of the File
read filename
if chmod +x $filename
then
echo Successfully run mode attribute was set for file.
else
echo Attribute not set for file.
fi
Output:
---------------------------------------------
Setting The Attributes of A File
---------------------------------------------
Enter the name of the File
sat
Successfully run mode attribute was set for file.
Write a linux shell program to perform get mark details of a student and display total and grade
Get mark details of a student and display total and grade
Code:
Output:
Write a linux shell program to perform a menu driven program to check for file existence,file readable or not,file writeable or not,
Write a menu driven program to check for file existence, file
readable or not, file writeable or not
Code:
Output:
------------------------Check file
------------------------
Enter the file name
satheesh
1.File existence or not
2.Readable or not
3.Writeable or not
4.Exit
Enter your choice
1
File was existence.
Write a linux shell program to perform Read 10 names from a file and sort in Ascending order Descending order
Read 10 names from a file and sort in Ascending order and Descending order
Code:
clear
echo ---------------------------------------------
echo '\t Ascending and Descending Order'
echo ---------------------------------------------
echo Enter the file name containing names
read name
sort -g $name > name_asce
sort -g -r $name > name_desc
echo The Ascending order names are
cat name_asce
echo The Descending order names are
cat name_desc
Output:
Write a linux shell program to perform a menu driven program to display today’s date,Processes of the system,user’s of the system,list files of the system
Write a menu driven program to display today’s date, Processes of the system, user’s of the system, list files of the system
Code:
clear
echo ---------------------------------------------
echo '\tMenu Implementation'
echo ---------------------------------------------
echo 1.Today DATE
echo 2.Process of the system
echo 3.Users of the system
echo 4.List of files
echo Enter your choice
read choice
case $choice in
1)date;;
2)ps;;
3)who;;
4)ls -1;;
*)echo This is not a choice
esac
Output:
---------------------------------------------
Menu Implementation
---------------------------------------------
1.Today DATE
2.Process of the system
3.Users of the system
4.List of files
Enter your choice
2
PID TTY TIME CMD
2458 pts/0 00:00:00 bash
2477 pts/0 00:00:00 sh
2479 pts/0 00:00:00 ps
Wednesday, 18 December 2013
Write a linux shell program to perform check for palindrome
Check for palindrome
Code:
Output:
--------------------------------Polindrome
--------------------------------
Enter the number
121
The given number is Polindrome.
Write a linux shell program to perform find the number of users who have logged in
Find the number of users who have logged in
Code:
clear
echo -----------------
echo '\tLogin'
echo -----------------
echo The number of users loggin in are
who | wc -l
Output:
-----------------
Login
-----------------
The number of users loggin in are
3
Write a linux shell program to perform check pattern matching using grep
Check pattern matching using grep
Code:
clear
echo ---------------------------------------------
echo '\tSearch a word in A particular File'
echo ---------------------------------------------
echo Type the string to be searched
read string
echo Type the name of the file
read filename
if grep $string $filename
then
echo A word in a paricular file.
else
echo A word not in paricular file.
fi
Output:
---------------------------------------------
Search a word in A particular File
---------------------------------------------
Type the string to be searched
this
Type the name of the file
vino
A word not in paricular file.
Write a linux shell program to perform check for an adam number
Check for an Adam number
Code:
Output:
--------------------------------Adam or Not
--------------------------------
Enter the number
13
The given number is Adam.
Write a linux shell program to perform convert lowercase to uppercase using tr statement
Convert lowercase to uppercase using tr statement
Code:
clear
echo ---------------------------------------------
echo '\tlower case to UPPERCASE'
echo ---------------------------------------------
echo Enter the name of the File
read filename
echo File content in Uppercase
tr '[ a-z ]' '[ A-Z ]' < $filename
Output:
Write a linux shell program to perform check whether a number is prime or not using while
Prime or not using while
Code:
clear
echo ---------------------------------------------
echo '\tPrime number or Not'
echo ---------------------------------------------
echo Enter the number
read n
i=2
ans=0
while [ $i -le $(expr $n - 1) ]
do
if [ $(expr $n % $i) -eq 0 ]
then
ans=$(expr $ans + $i)
fi
i=$(expr $i + 1)
done
if [ $ans -eq 0 ]
then
echo The number $n is a prime.
else
echo The number $n is not a prime.
fi
Output:
---------------------------------------------
Prime number or Not
---------------------------------------------
Enter the number
11
The number 11 is a prime
Write a linux shell program to perform display multiplication table
Multiplication table
Code:
clear
echo -----------------------------------
echo '\tMultiplication Table'
echo -----------------------------------
echo Enter table number
read tn
echo Enter how many rows
read n
i=1
while [ $i -le $n ]
do
k=$(expr $i \* $tn)
echo "$i * $tn = $k"
i=$(expr $i + 1)
done
Output:
Enter table number
6
Enter how many rows
5
1 * 6 = 6
2 * 6 = 12
3 * 6 = 18
4 * 6 = 24
5 * 6 = 30
Write a linux shell program to perform basic arithmetic operations using case
Basic arithmetic operations using case
Code:
Output:
-----------------------------------------------------Evaluation of Arithmetic expression
-----------------------------------------------------
Enter the a value
45
Enter the b value
5
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Modules
Enter your choice
4
Division : 9
Write a linux shell program to perform find the reverse of a number
Reverse of a number
Code:
Output:
-----------------------------------
Reverse A Number
-----------------------------------
Enter the n value
123
Reverse the number 123 is 321




