[go: up one dir, main page]

0% found this document useful (0 votes)
35 views26 pages

DEEPU LINUX File

ASFASFASDFASFASF

Uploaded by

caption america
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)
35 views26 pages

DEEPU LINUX File

ASFASFASDFASFASF

Uploaded by

caption america
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/ 26

22756

Panipat Institute of Engineering & Technology, Samalkha,


Panipat

Department of Computer Applications

Lab Manual of Linux and Shell Programming Lab

Sub Code-MCA-20-22

Submitted To Submitted By
Ms. Nisha Mam DEEPANSHU
220167540

Affiliated to

Kurukshetra University Kurukshetra, India

Linux / MCA 2 / DEEPANSHU


22756

Practical No 6

Aim: Write a shell script to determine whether given file exists or not, filename is supplied as
command line arguments, also check for sufficient number of command line arguments has
been passed.

Software and Hardware Requirements: Ubuntu

Code:

if [ $# -eq 0 ]

then

echo "File have not been passed as asguments"

exit;

fi

if [ -f $1 ]

then

echo "File exist"

else

echo "File Doesn't Exist"

fi

Input /Output :

Linux / MCA 2 / DEEPANSHU


22756

Short Answer Questions: (5 to 10)


Question: Is it possible to use shortcuts for a long pathname?

Ans: To create a shortcut to a long path, move into the directory you want and use the -a flag
to add an alias for that directory in gogo, as shown. You can also create aliases for connecting
directly into directories on a remote Linux servers.

Question: What is redirection?

Ans: whenever an individual runs a command, it can take input, give output, or do both.
Redirection helps us redirect these input and output functionalities to the files or folders we
want, and we can use special commands or characters to do so.

Question: What is grep command?

Ans: The grep command searches for the pattern specified by the Pattern parameter and
writes each matching line to standard output. The patterns are limited regular expressions in
the style of the ed or egrep command. The grep command uses a compact non-deterministic
algorithm.

Question: What could be the problem when a command that was issued gave a different
result from the last time it was used?

Ans: One highly possible reason for getting different results from what seems to be the same
command has something to do with case sensitivity issues. Since Linux is case sensitive, a
command that was previously used might have been entered in a different format from the
present one. For example, to lists all files in the directory, you should type the command ls,
and not LS. Typing LS would either result in an error message if there is no program by that
exact name exist, or may produce a different output if there is a program named LS that
performs another function.

Linux / MCA 2 / DEEPANSHU


22756

Practical No 7

Aim: Write a shell script to delete all lines containing a specific word in one or more
file supplied as argument to it.

Software and Hardware Requirements: Ubuntu

Code: if [ $# -eq 0 ]

then

echo "enter sufficient files as arguments"

else

echo "enter word to delete "

read word

sed "/$word/d" $1

fi

Input /Output:

Linux / MCA 2 / DEEPANSHU


22756

Short Answer Questions: (5 to 10)

Question: How do you terminate an ongoing process?

Ans: There are two commands used to kill a process:

1. kill – Kill a process by ID.


2. killall – Kill a process by name.

Question: How do you insert comments in the command line prompt?

Ans: “REM” command and the “::” (Double colon) are used to add the single line
comments. REM is the short form of “Remarks”. It is the built-in command prompt utility of
Windows.

Question: What is command grouping and how does it work?

Ans: The GROUP command allows you to execute several commands on a record before
moving to the next record in the table, which can significantly reduce processing time. You
can use the LOOP command inside the GROUP command if you need to execute a series of
commands more than once against a record.

Question: How do you execute more than one command or program from a single
command line entry?

Ans: Using the Semicolon (;) Operator

For instance, if there are two commands: command A and command B, using the semicolon
operator in between them ensures both commands get executed sequentially regardless of the
output of the first command.

Linux / MCA 2 / DEEPANSHU


22756

Practical No 8

Aim: A shell script that accepts a list of filenames as its arguments counts and reports the
occurrence of each word that is present in the first argument file on other argument files.

Software and Hardware Requirements: Ubuntu

Code:
echo "enter file name.."
read file
if [ -f $file ]
then
echo "enter pattern you want to search"
read pattern
grep -c "$pattern" "$file"
else
echo "file not exist"
fi
Input/Output:

Linux / MCA 2 / DEEPANSHU


22756

Short Answer Questions: (5 to 10)


Question: Write a command that will display all .txt files, including its individual
permission.

Ans: ls -al *.txt

Question: What, if anything, is wrong with each of the following commands?

a) ls -l-s
Ans: There should be space between the 2 options.
b) cat file1, file2
Ans : do not use comma to separate arguments : cat file1 file2
c) ls - s Factdir
Ans : There should be no space between hyphen and option label : ls -s Factdir

Question: What is the command to calculate the size of a folder?

Ans: The du command displays the amount of file space used by the specified files or
directories. If the specified path is a directory, du summarizes disk usage of each subdirectory
in that directory.

Question: How can you find the status of a process?

Ans: ccommand to check the process status (ps command)

Item Description

PID Process ID

PPID Parent process ID

C CPU utilization of process

STIME Start time of process

Linux / MCA 2 / DEEPANSHU


22756

Practical No 9
Aim: Write a shell script to check

a. File Existence
b. File Permissions
c. Check if file has execute permission, if not then change permission of file and make it
executable.
d. Check whether the file is sorted or not, if not then sort the file.
e. Read another file and compare the permissions of both the files, and extract common
permissions if any

Software and Hardware Requirements: Ubuntu

Code:

echo "enter file name "


read file
if [ -f $file ]
then
echo "file permissions: "
ls -l $file | cut -c1-10
if [ `ls -l $file | cut -c4` -eq "x" ]
then
echo "file has execute permission"
else
echo "file has not execute permission"
chmod u+x $file
echo "permission granted : `ls -l $file | cut -c1-10` "

fi
if [ `sort -c $file` ]
then
echo " sorted "
else
echo "file sorted: "
sort $file
fi
echo " enter another file to compare "
read file2
per1=`ls -l $file | cut -c1-10`
per2=`ls -l $file2 | cut -c1-10`
if [ per1 -eq per2 ]
then

Linux / MCA 2 / DEEPANSHU


22756

echo "files have same permissions "


else
echo "file does not have same permissions"
fi
fi

Input/Output:

Linux / MCA 2 / DEEPANSHU


22756

Short Answer Questions:


Question: How can you check the memory status?
Ans: To open up Resource Monitor, press Windows Key + R and type resmon into the
search box. Resource Monitor will tell you exactly how much RAM is being used, what is
using it, and allow you to sort the list of apps using it by several different categories.

Question: How can you append one file to another in Linux?


Ans: You do this by using the append redirection symbol, ``>>''. To append one file to the
end of another, type cat, the file you want to append, then >>, then the file you want to
append to, and press <Enter>.

Question: Explain how you can find a file using Terminal?


Ans: use the find command with -name option followed by the file name that you want to
search. You can use the following option if you want to search for a specific file type: f –
regular file. d – directory.

Linux / MCA 2 / DEEPANSHU


22756

Practical No 10
Aim: Write a shell script that folds long lines into 40 columns. Thus any line that exceeds 40
characters must be broken after 40th; a\is to be appended as the indication of folding and the
processing is to be continued with the residue. The input is to be through a text file created
by the user

Software and Hardware Requirements: Ubuntu

Code: echo "Enter a file name";


read f
if [ -f $f ];
then
n=`wc -l $f | cut -d " " -f 1`;
i=1
while [ $i -le $n ];
do
line=`sed -n "$i p" $f`;
cc=`echo $line | wc -c | cut -d " " -f 1`
while [ $cc -ge 40 ];
do
ext=`echo $line | cut -c 41-`;
line=`echo $line | cut -c 1-40`
echo "$line \\"
line=$ext;
cc=`echo $ext | wc -c | cut -d " " -f 1`
done
echo "$line"
i=`expr $i + 1`
done
else
echo "File does not exists "
fi

Input /Output:

Linux / MCA 2 / DEEPANSHU


22756

Short Answer Questions:

Question: Explain how you can create a folder using Terminal?

Ans: Open the Terminal app and type the following command:

1. $ mkdir foo. To see directory listing use the ls command:


2. $ ls. $ ls -l. You can simultaneously create any number of folders/directories:
3. $ mkdir dir1 dir2 dir3 dir_4. Verify it:
4. $ ls -l.

Question: How can you run a Linux program in the background simultaneously when
you start your Linux Server?

Ans: Use bg to Send Running Commands to the Background

You can easily send such commands to the background by hitting the Ctrl + Z keys and then
using the bg command. Hitting Ctrl + Z stops the running process, and bg takes it to the
background

Question: What are filters?

Ans: A filter is a program that takes plain text (stored in a file or generated by another
program) as standard input, converts it to a meaningful format, and then returns it as standard
output.

Question: What is a typical syntax being followed when issuing commands in shell?

Ans: The typical syntax that is being followed when issuing the commands in shell
are

Command [-argumen t] [-argumen t ] [-argumen t] [ file ]


Arguments or the variables that are may be passed to a shell script.

This simply lists the arguments on the command line when running a shell script.

In shell script, $0 is the name of the command to run i.e usually the name of the
shell script file.

$1 is the first argument, $2 is the second argument, $3 is the third argument etc.

Args provides the access to raw command-line arguments.

Linux / MCA 2 / DEEPANSHU


22756

Practical No 11
Aim: Write a shell script to display the calendar for current month with current date replaced
by * or ** depending on whether the date has one digit or two digits.

Software and Hardware Requirements: Ubuntu

Code:

current_date=$(date +%d)
current_month=$(date +%m)
cal_output=$(cal)
if [ $current_date -lt 10 ]; then
current_date=`expr substr "$current_date" 2 2`
cal_output=$(echo "$cal_output" | sed "s/\<$current_date\>/\*/g")
else
cal_output=$(echo "$cal_output" | sed "s/\<$current_date\>/\\/g")
fi
echo "Calendar for current month"
echo "$cal_output"
Input/Output:

Linux / MCA 2 / DEEPANSHU


22756

Short Answer Questions: (5 to 10)

Question: Is there a way to erase all files in the current directory, including all its sub-
directories, using only one command?

Ans: To delete everything in a directory run: rm /path/to/dir/* To remove all sub-directories


and files: rm -r /path/to/dir/*

Question: What are some common shells and what are their indicators?

Ans:

Shell Indicators

C Shell csh

Bourne Again shell Bash

Enhanced C shell tcsh

Z Shell zsh

Question: What is command substitution?

Ans: Command substitution allows you to capture the output of any command as an
argument to another command.

When you place a command line within back quotes (``), the shell first runs the command
or commands and then replaces the entire expression, including the back quotes, with the
output. This feature is often used to give values to shell variables. For example, the
statement:
today=`date`
assigns the string representing the current date to the today variable. The following
assignment saves, in the files variable, the number of files in the current directory:

Question: What is a directory?


Ans: A command is an instruction given to our computer by us to do whatever we want. In Mac OS,
and Linux it is called terminal, whereas, in windows it is called command prompt. Commands are
always case sensitive.

Linux / MCA 2 / DEEPANSHU


22756

Practical No 12

Aim: Write a shell script that accepts command line arguments and print them in reverse
order.

Software and Hardware Requirements: Ubuntu

Code: echo $@ | tr ' ' '\n' | tac | tr '\n' ' '

echo '\n'

Input/Output:

Linux / MCA 2 / DEEPANSHU


22756

Short Answer Questions: (5 to 10)

Question: What is inode?

Ans: Inodes keep track of all the files on a Linux system. Except for the file name and the
actual content of the file, inodes save everything else. It's like a file-based data structure that
holds metadata about all of the files in the system.

Question: Describe file systems in UNIX

Ans: The Unix file system is a methodology for logically organizing and storing large
quantities of data such that the system is easy to manage. A file can be informally defined as
a collection of (typically related) data, which can be logically viewed as a stream of bytes
(i.e. characters).

Question: Differentiate relative path from an absolute path.

Ans: A relative path describes the location of a file relative to the current (working)
directory*. An absolute path describes the location from the root directory. When learning to
access data files through programming, we regularly use relative file paths.

Question : What are shell variables?

Ans: 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.

Question : What is the use of -l when listing a directory?

Ans: Although minor information leaks, directory listings allow the Web user to see most (if
not all) of the files in a directory, as well as any lower-level subdirectories.

Linux / MCA 2 / DEEPANSHU


22756

Practical No 13

Aim: Write a shell script to check whether the given string is palindrome.

Software and Hardware Requirements: Ubuntu

Code:

(i) without using string function


echo "Enter a string :"
read str
echo
len=`echo $str | wc -c`
len=`expr $len - 1`
i=1
j=`expr $len / 2`
while [ $i -le $j ]
do
k=`echo $str | cut -c $i`
l=`echo $str | cut -c $len`
if [ “$k” != “$l” ]
then
echo "String is not palindrome"
exit
fi
i=`expr $i + 1`
len=`expr $len - 1`
done
echo "String is palindrome"

Input/Output:

Linux / MCA 2 / DEEPANSHU


22756

(ii) with using string function


echo "Enter a string :"
read str
echo
revstr=`echo $str | rev`
if [ $revstr != $str ]
then
echo " The string is not a palindrome "
else
echo " The string is a palindrome "
fi

Input/Output:

Linux / MCA 2 / DEEPANSHU


22756

Short Answer Questions: (5 to 10)

Question: What is the use of the tee command?

Ans: The tee command, used with a pipe, reads standard input, then writes the output of a
program to standard output and simultaneously copies it into the specified file or files. Use
the tee command to view your output immediately and at the same time, store it for future
use.

Question: Differentiate cat command from more command.

Ans: As 'cat' command displays the file content. Same way 'more' command also displays the
content of a file. Only difference is that, in case of larger files, 'cat' command output will
scroll off your screen while 'more' command displays output one screenful at a time.

Question: What is pid?

Ans: In computing, the process identifier (a.k.a. process ID or PID) is a number used by most
operating system kernels—such as those of Unix, macOS and Windows—to uniquely
identify an active process.

Question: How does the system know where one command ends and another begins?

Ans: the newline character, which is generated by the ENTER or RETURN key, acts as the
signpost. However, the semicolon and the ampersand characters can also serve as command
terminators.

Question: What is the output of this command?

$who | sort –logfile > newfile

Ans: the output from the command “who” becomes the input to the “sort” command. At the
same time, “sort” opens logfile, arranges it together with the output from the command
“who”, and places the final sorted output to the file newfile

Linux / MCA 2 / DEEPANSHU


22756

Practical No 14
Aim: Write a shell script to calculate basic salary of Employee on the basis of following
conditions:
(a) if basic <10000, then hra=15% of basic and da=10% of basic
(b) otherwise, hra=25% of basic and da=15% of basic
Software and Hardware Requirements: Ubuntu
Code:

echo "enter basic"


read basic
if [ $basic -lt 10000 ]
then
hra=`expr $basic \* 15 / 100`
da=`expr $basic \* 10 / 100`
salary=`expr $basic + $hra + $da`
else
hra=`expr $basic \* 25 / 100`
da=`expr $basic \* 15 / 100`
salary=`expr $basic + $hra + $da`
fi
echo $salary

Input/Output

Linux / MCA 2 / DEEPANSHU


22756

Short Answer Questions: (5 to 10)

Question: What is wrong with this interactive shell script?


Ans: A shell gives us an interface to the Unix system. While using an operating system, we
indirectly interact with the shell. On Linux distribution systems, each time we use a terminal,
we interact with the shell. The job of the shell is to interpret or analyze the Unix commands
given by users. A shell accepts commands from the user and transforms them into a form that
is understandable to the kernel. In other words, it acts as a mediator between ta user and the
kernel unit of the operating system.

Question: What is Samba? Why is it used?


Ans: Samba is a suite of applications that implements the Server Message Block (SMB)
protocol. Many operating systems, including Microsoft Windows, use the SMB protocol for
client-server networking. Samba enables Linux / Unix machines to communicate with
Windows machines in a network. Samba is open source software.

Question: What are the basic commands for user management?


Ans: Linux/Unix user management commands
Command Description

sudo adduser username Adds a user

sudo passwd -l 'username' Disable a user

sudo userdel -r 'username' Delete a user

sudo usermod -a -G GROUPNAME USERNAME Add user a to a usergroup

Question: Is Linux Operating system virus free?


Ans: Linux, Unix and other Unix-like computer operating systems are generally regarded as
very well-protected against, but not immune to, computer viruses.

Question: Which command is used to uncompressed gzip files?


Ans: When compressing, gzip uses the .tgz extension if necessary instead of truncating a file
with a .tar extension. Unzip can currently decompress files created by gzip, zip, compress,
compress -H or pack. The detection of the input format is automatic.

Linux / MCA 2 / DEEPANSHU


22756

Question: How to exit from vi editors?


Ans: you’ve made mistakes along the way while editing and want to back out (abandon) all
non-saved changes, enter Command mode by pressing Esc and typing :q! This command
quits without saving any changes and exits Vi.

Question: How to delete information from a file in vi?


Ans: To delete one character, position the cursor over the character to be deleted and type x
. The x command also deletes the space the character occupied—when a letter is removed
from the middle of a word, the remaining letters will close up, leaving no gap.

Question: What do you mean by hard links in Linux?


Ans: A hard link always points a filename to data on a storage device. A soft link always
points a filename to another filename, which then points to information on a storage device.

Linux / MCA 2 / DEEPANSHU


22756

Practical No. 15

Aim: Write an interactive file handling shell program. Let it offer the user the choice of
listing copying, removing, renaming or linking files. Once the use has made a choice, have
the program ask the user for necessary information, such as the file name, new file name and
so on.

Software and Hardware Requirements: Ubuntu

Code:

echo " choose from these"


echo "1. Listing all files."
echo "2. Copying files."
echo "3. Removing files."
echo "4. Renaming files."
echo "5. Linking files."
choice='y'
while [ $choice='Y' -o $choice='y' ]
do
echo "Enter number to choose "
read n
case $n in
1 ) echo "The list of all file names."
ls ;;
2) echo "To copy one file to another :"
echo "Enter the filename to copy - "
read f1
echo "Enter the filename to be copied to -"
read f2
if [ -f $f1 ]
then
cp $f1 $f2
else
echo “file does not exist”
fi
;;
3) echo "Enter the file name to remove."
read file
if [ -f $file ]
then
rm -f $file
else
echo “file does not exist”
fi
;;
4 ) echo "To rename the file : "

Linux / MCA 2 / DEEPANSHU


22756

echo "Enter the file name to move - "


read file1
echo "Enter the new file name - "
read file2
if [ -f $file1 ]
then
mv $file1 $file2
else
echo “file does not exist”
fi
;;
5) echo "To link two files : "
echo "Enter the original filename - "
read ofile
echo "Enter the new filename to link a file - "
read lfile
if [ -f $file1 ]
then
ln $ofile $lfile
else
echo “file does not exist”
fi
;;
*)
echo "enter valid number "
esac
echo “do you want to continue”
read choice
case $choice in
'y' | 'Y')choice='y';;
*) exit;
esac
done

Linux / MCA 2 / DEEPANSHU


22756

Linux / MCA 2 / DEEPANSHU


22756

Question: What is top Command in Linux?

Ans: The top utility is a commonly used tool for displaying system-performance
information. It dynamically shows administrators which processes are consuming processor
and memory resources. Top is incredibly handy.

Question: What is netstat command in Linux?

Ans: The network statistics ( netstat ) command is a networking tool used for
troubleshooting and configuration, that can also serve as a monitoring tool for connections
over the network.

Question: Choose the odd one out.

▪ Vi
▪ Vim
▪ Cd
▪ nano

Question: What is lsof command in Linux?

Ans: The lsof command is an acronym for "list open files," but its potential isn't limited to
just that role. It's commonly said that in Linux, everything is a file.

Question: Explain about chmod command?

Ans: Linux chmod command is used to change the access permissions of files and
directories. It stands for change mode. It can not change the permission of symbolic links.

Linux / MCA 2 / DEEPANSHU

You might also like