Linux Questions Practice
Linux Questions Practice
Procedure/Algorithm/Steps/Syntax:
Procedure/Algorithm/Steps/Syntax:
Option Description
-a Displays all processes on a terminal, with the exception of group
leaders.
-c Displays scheduler data.
-d Displays all processes with the exception of session leaders.
-e Displays all processes.
-f Displays a full listing.
-glist Displays data for the list of group leader IDs.
-j Displays the process group ID and session ID.
-l Displays a long listing
-plist Displays data for the list of process IDs.
-slist Displays data for the list of session leader IDs.
-tlist Displays data for the list of terminals.
-ulist Displays data for the list of usernames.
Procedure/Algorithm/Steps/Syntax:
vi is a visual text editor.
vi is actually the command which launches the visual mode of ex. As ex gained
popularity, he noticed that most users were exclusively using its visual mode, so he
created a link to ex which immediately starts in visual mode and simply named it vi.
The name stuck, and today vi is the most popular text editor among Linux users.
Code:
vi [option]
Sample Input:
vi myfile.txt
Sample Output:
Edits the file myfile.txt
EXPERIMENT NO: 5
Procedure/Algorithm/Steps/Syntax:
The current date is displayed using the date command.
The number of users currently login is displayed using who|wc –l command.
The current month’s calendar is displayed using cal command.
Code:
echo “Current date is”; date
echo “Number of users”; who|wc –l
echo “Monthly calendar”; cal
Save it as file1.sh
Sample Input:
chmod 754 file1.sh
./file1.sh
Sample Output:
Current date is <today’s date>
Number of users 20
Monthly calendar
<Current month’s calendar>
b. Write a shell script to display the process name and its pid.
Procedure/Algorithm/Steps/Syntax:
The process id (PID) is displayed using $$.
The process name is displayed using $0.
Code:
echo “Current process’ name: $0”
echo “Current process’ id: $$”
Save it as file1.sh
Sample Input:
chmod 754 file1.sh
./file1.sh
Sample Output:
Current process’ name: ./file1.sh
Current process’ id: 1245 (Or any other number)
EXPERIMENT NO: 6
Procedure/Algorithm/Steps/Syntax:
The % operator is used to find the remainder.
The if [ condition ] then statement else statement is the syntax of if-else construct.
fi is used to end the if statement.
Code:
Save it as file1.sh
Sample Input:
chmod 754 file1.sh
./file1.sh
Sample Output:
Enter a number
10
10 is even
b. Write a shell script to input the name of a file as command line argument and
display whether it is a file, a directory or anything else.
Procedure/Algorithm/Steps/Syntax:
The –f option checks for a file and the –d option checks for a directory.
The first command line argument is stored in the variable $1, the second in $2, and so
on.
fi is used to end the if statement.
Code:
if [ -f $1 ]
then
echo “It is a file”
else if [ -d $1 ]
then
echo “It is a directory”
else
echo “Invalid filename”
fi
fi
Sample Input:
chmod 754 file2.sh
./file2.sh file1.sh
(Note: Here file1.sh is the command line argument.)
Sample Output:
It is a file
c. Write a shell script to input the marks of a student in 3 subjects and find his
grade.
Procedure/Algorithm/Steps/Syntax:
Any expression can be calculated in shell programming using expr. The options for
equal to is –eq, for greater than equal to is –ge, for less than equal to is –le, and so on.
Code:
echo “Enter marks in three subjects”
read m1
read m2
read m3
sum=`expr $m1+$m2+$m3`
per=`expr $sum/3`
if [ $per –ge 60 ]
then
echo “First division”
else if [ $per –ge 50 ]
then
echo “Second Division”
else if [ $per –ge 40 ]
then
echo “Third Division”
else
echo “Fail”
fi
fi
fi
Sample Input:
chmod 754 file1.sh
./file1.sh
Sample Output:
Enter marks in three subjects
98
97
90
First division
EXPERIMENT NO: 7
a. Write a shell script to input two strings from the user and determine whether
they are same or not.
Procedure/Algorithm/Steps/Syntax:
Two strings can be compared using “=” sign
Code:
echo “Enter string1”
read s1
echo “Enter string2”
read s2
if [ s1 = s2 ]
then
echo “Strings are same”
else
echo “Strings do not match”
fi
Sample Input:
chmod 754 file1.sh
./file1.sh
Sample Output:
Enter string1
University
Enter string2
University
Strings are same
b. Write a shell script to input a string from the user and determine its length.
Procedure/Algorithm/Steps/Syntax:
The length of a string can be determined using #.
When we write
len=$ {#s1}
the length of the string s1 gets stored in the len variable.
Code:
echo “Enter string”
read s1
len=${#s1}
echo “Length of the string is $len”
Save the file as file1.sh
Sample Input:
chmod 754 file1.sh
./file1.sh
Sample Output:
Enter string
GLA University
Length of the string is 14
c. Write a shell script to input two strings from the user and find the
occurrences of string2 in string 1.
Procedure/Algorithm/Steps/Syntax:
The patterns can be matched using grep command. Similarly, we can match the
strings within each other using grep command.
The –o option is for matching and the –w option is for a complete word only.
Code:
echo “Enter string1”
read s1
echo “Enter string2”
read s2
grep –o –w “$s2” <<< “$s1” | wc -l
Sample Input:
chmod 754 file1.sh
./file1.sh
Sample Output:
Enter string1
Jitendra is ten years old
Enter string2
ten
EXPERIMENT NO: 8
Procedure/Algorithm/Steps/Syntax:
The entered name is a file or not can be checked by the option –f. The grep command
helps us to find the number of lines, characters and words in the file.
Code:
if [ -f $1 ]
then
w=`cat $1 | wc –w`
c=`cat $1 | wc –c`
l=`cat $1 | wc –l`
echo “Number of words=$w”
echo “Number of lines=$l”
echo “Number of characters=$c”
else
echo “Invalid filename”
fi
Sample Input:
Sample Output:
Number of words=25
Number of lines=2
Number of characters=243
b. Write a shell script to display a list of directories within the current directory and how
much space they consume, sorted from the largest to the smallest.
Procedure/Algorithm/Steps/Syntax:
The du command is used to find the disk usage. The sort command is used for
sorting. The –n option with sort command is for numeric sort. The –r option with
sort command is used to sort in reverse order.
Code:
Sample Input:
Sample Output:
List of directories sorted from largest to smallest.
EXPERIMENT NO: 9
Procedure/Algorithm/Steps/Syntax:
The frequently used two loops in unix shell scripts are while loop and for loop.
Code:
Sample Input:
Sample Output:
Enter the value of n:
5
5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50
Code:
ch=1
while [ $ch -eq 1 ]
do
echo "Enter a Timer Limit"
read n
for (( i=n; i>0; i--)); do
sleep 1 &
printf "Timer $i \r"
wait
done
echo "Time Up!"
echo "Want a new Timer? Press 1 to continue 0 to exit!"
read ch
done
Sample Input:
Sample Output:
Enter the timer limit
5
(Timer runs for 5 seconds)
Time Up
Want a new Timer? Press 1 to continue 0 to exit!
0
EXPERIMENT NO: 10
Procedure/Algorithm/Steps/Syntax:
IPC stands for Inter-process Communication. This technique allows the processes to
communicate with each another.
We can request the kernel to allocate the space which can be used to communicate
between processes. The process can also communicate by having a file accessible to
both the processes. Processes can open, and read/write the file, which requires lot of
I/O operation that consumes time.
There are various IPC’s which allows a process to communicate with another process,
either in the same computer or different computer in the same network.
1. Pipes – Provides a way for processes to communicate with each another by
exchanging messages. Named pipes provide a way for processes running on
different computer systems to communicate over the network.
2. Shared Memory – Processes can exchange values in the shared memory. One
process will create a portion of memory which other process can access.
3. Message Queue – It is a structured and ordered list of memory segments
where processes store or retrieve data.
4. Semaphores – Provides a synchronizing mechanism for processes that are
accessing the same resource. No data is passed with a semaphore; it simply
coordinates access to shared resources.
Code:
ipcs –a
ipcs –q
ipcs –s
ipcs –m
ipcs -q -i 32768
Sample Input:
1. ipcs –a
2. ipcs –q
3. ipcs –s
4. ipcs –m
5. ipcs -q -i 32768
Sample Output:
1. ipcs command with -a option lists all the IPC facilities which has read access
for the current process. It provides details about message queue, semaphore
and shared memory.
2. ipcs with option -q, lists only message queues for which the current process
has read access.
3. ipcs -s option is used to list the accessible semaphores.
4. ipcs -m option with ipcs command lists the shared memories.
5. ipcs -i option provides detailed information about an ipc facility. Option -i
with -q provides information about a particular message queue. Option -i with
-s provides semaphore details. Option -i with -m provides details about a
shared memory.