0% found this document useful (0 votes)
67 views60 pages

LSP All Experiments

The document provides details and sample outputs for 12 lab experiments on Linux and shell scripting. The experiments cover tasks like emulating the ln command, reading and writing file bytes, creating zombies, using system calls, IPC between processes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views60 pages

LSP All Experiments

The document provides details and sample outputs for 12 lab experiments on Linux and shell scripting. The experiments cover tasks like emulating the ln command, reading and writing file bytes, creating zombies, using system calls, IPC between processes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

LSP Lab Experiments with

output to be displayed
LSP Lab Preparation for Internals (upto Eight Experiments)
Write a program to emulate the ln
command.

• The ln command is used to create links to files or directories. (“ln” is


short for “link”.)
• The command is given to the Linux command line (also called the
shell), which can be opened and operated using a terminal window.
• It is one of the most important and most frequently used terminal
commands and can be used to create soft or hard links.
Soft links
• Soft links are cross references that point to a file or directory.
• If you move or delete the original file, there is no longer a target and
the link leads to nothing.
• On the other hand, if you delete the link, the original final link remains
intact.
• Soft links are also called symbolic links or symlinks.
• Soft links can be created in the Linux terminal and in the graphical
user interface in the Linux file manager. You can spot a soft link in
Linux by the arrow symbol on the file icon.
Hard links
• A hard link is a mirror copy of the original file, which has its own
directory entry. Several hard links can point to the same file.
• In fact, Linux doesn’t distinguish technically or operationally between
the original and a hard link generated later, meaning that all directory
entries are treated the same and work independently of one another.
Output to be displayed
LAB EXPERIMENT 5

Write a program to read the alternate nth byte and write it


in another file.
Output to be displayed
• Output can be explained as following manner
• My name is Renuka
• When n=2
• First letter, then second letter, then 2 * 2, alternate 4th bytes
• It will print: Mnin
• My name is Renuka. Linux is open source. Linux is multiuser system.
• When n=5
• First letter, then fifth letter, then alternate 10th byte
• It will print Me.o.my
LAB EXPERIMENT 6

Write a program that creates a zombie and then calls


system to execute the ps command to verify that the
process is zombie.
Output to be displayed
Output:
LAB EXPERIMENT 7

Write a program to implement the system function.


Output to be displayed
LAB EXPERIMENT 8
Write a program which demonstrates inter-process communication between a
reader process and a writer process. (Use mk fifo, open, read, write and close APIs)
Output to be displayed
LAB EXPERIMENT 9
9a) Write a shell script to accept a file and check if it is
executable. If not make it executable.

9b) Write a shell script that will accept a filename and


starting and ending line numbers and displays those lines
from the given file.
9a) Write a shell script to accept a file and check if it is
executable. If not make it executable.
Output to be displayed
How to show the output:
• Open the test.txt with the cat command.
• View the file permission of the file with ls -l name of file
• Use chmod +x exp9a.sh (To make Shell program executable)
• ./exp9a.sh (execute the .sh file)
• View the file permission of the file with ls -l name of file
9b) Write a shell script that will accept a filename and
starting and ending line numbers and displays those lines
from the given file.
Output to be displayed
View the contents of file
Execute the file exp9b.sh
LAB EXPERIMENT 10

10a) Write a shell script which displays a list of all the files in the current
directory to which you have read, write and execute permissions.
10b) A shell script receives an even number of filenames as arguments.
Suppose four files are supplied as arguments then the first file should get
copied into the second, the third file into the fourth, and so on. If an odd
number of filenames is supplied then no copying should take place and an
error message should be displayed.
10a) Write a shell script which displays a list of all the files in the current
directory to which you have read, write and execute permissions.
Output to be displayed
10b) A shell script receives an even number of filenames as arguments. Suppose four files are
supplied as arguments then the first file should get copied into the second, the third file into the
fourth, and so on. If an odd number of filenames is supplied then no copying should take place
and an error message should be displayed.
Output to be displayed
LAB EXPERIMENT 11

11a) Write a shell script which gets executed the moment the user logs in. It
should display the message, “ Good Morning”, “ Good Afternoon”, “ Good
Evening”, depending upon the time at which the user logs in.

11b) Write a shell script which accepts any number of arguments and prints
them in reverse order. Ex: If file name is test then $sh test A B C should
produce C B A.
11a) Write a shell script which gets executed the moment the user logs in. It
should display the message, “ Good Morning”, “ Good Afternoon”, “ Good
Evening”, depending upon the time at which the user logs in.
Output to be displayed
11b) Write a shell script which accepts any number of arguments and prints
them in reverse order. Ex: If file name is test then $sh test A B C should
produce C B A.

Output to be displayed
LAB EXPERIMENT 12

12a) Write scripts to demonstrate built-in variables available in AWK

12b) Write scripts to demonstrate built in functions available in AWK


Built-In Variables In Awk
• Awk’s built-in variables include the field variables—$1, $2, $3, and so on ($0 is
the entire line) — that break a line of text into individual words or pieces called
fields.
• NR: NR command keeps a current count of the number of input records.
Remember that records are usually lines. Awk command performs the
pattern/action statements once for each record in a file.
• NF: NF command keeps a count of the number of fields within the current input
record.
• FS: FS command contains the field separator character which is used to divide
fields on the input line. The default is “white space”, meaning space and tab
characters. FS can be reassigned to another character (typically in BEGIN) to
change the field separator.
Built-In Variables In Awk
• RS: RS command stores the current record separator character. Since, by default, an input
line is the input record, the default record separator character is a newline.

• OFS: OFS command stores the output field separator, which separates the fields when
Awk prints them. The default is a blank space. Whenever print has several parameters
separated with commas, it will print the value of OFS in between each parameter.

• ORS: ORS command stores the output record separator, which separates the output lines
when Awk prints them. The default is a newline character. print automatically outputs the
contents of ORS at the end of whatever it is given to print.
Another use of NR built-in variables (Display Line From 3 to 6)

• To print the first item along with the row number(NR) separated with ”
– “ from each line in students.txt
• To print the first item and second item separated with ” – “ from each
line in students.txt
• NF: NF command keeps a count of the number of fields within the current input
record.
LAB EXPERIMENT 12

12b) Write scripts to demonstrate built in functions available in AWK


Built-in functions for Numeric operations

1. Awk int(n) function:


int() function gives us the integer part of the given argument.
This produces the lowest integer part of given n.
n is any number with or without a floating point.
If you the whole number as an argument, this function returns the same.
Built-in functions for Numeric operations
2. awk log(n) function:
log() function provides natural logarithmic(with base e) of given amount
n.
log() returns logarithmic value only when n is positive number.
If we give any invalid number(even negative), it throws an error.
Built-in functions for Numeric operations
3. awk sqrt(n) function:
sqrt() function gives the positive root for the given integer n. This
function also accepts the positive number.
Built-in Functions for String Operations

1. awk index(str1, str2) Function:


This searches the string str1 for the first occurrences of the string str2,
and returns the position in characters where that occurrence begins in
the string str1. String indices in awk starts from 1.
Built-in Functions for String Operations
2. awk length(string) Function: The length() function calculates the
length of a string.
Built-in Functions for String Operations
3. awk substr(s, p, n) Function: The length() function is used to extract
substring function from a string. Returns substring of string s at
beginning position p up to a maximum length of n. If n is not supplied,
the rest of the string from p is used.
Built-in Functions for String Operations
4. awk tolower(s) Function: Translate all uppercase characters in string s
to lowercase and returns the new string.

5. awk toupper(s) Function: Translate all lowercase characters in string s


to uppercase and returns the new string.
Built-in Functions for String Operations
6. awk split(string, array, fieldsep) Function:
This divides string into pieces separated by fieldsep, and stores the
pieces in array. The first piece is stored in array[1], the second piece in
array[2], and so forth. The string value of the third argument, fieldsep,
describe where to split string.

You might also like