System Programming 2024 Q&A


System Programming 2024 Question and Answers





Suggestion :  keep refreshing the page for updated content & Search Questions Using Find )



Q1.  (A) Assume you are currently in the /home/BITS directory. Outline the procedures to create the directories dirt and dir2 using the absolute path, also issue a command to create an empty file namely welcome.txt in the dir2 directory and set the permission to 755. Make one soft link to file welcome.txt in the same directory and set the permission to 755. Make one soft link to file welcome.txt in the same directory.


Answer: 

  • Create the directories "dirt" and "dir2" using absolute paths:
mkdir /home/BITS/dirt
mkdir /home/BITS/dir2

  • Create an empty file named "welcome.txt" in the "dir2" directory and set permissions to 755:
touch /home/BITS/dir2/welcome.txt
chmod 755 /home/BITS/dir2/welcome.txt

  • Create a soft link to the file "welcome.txt" in the same directory and set permissions to 755:
ln -s /home/BITS/dir2/welcome.txt /home/BITS/welcome_softlink
chmod 755 /home/BITS/welcome_softlink


This series of commands will achieve the desired outcome. It creates the directories "dirt" and "dir2", then creates an empty file "welcome.txt" in the "dir2" directory with permissions set to 755. Finally, it creates a soft link to "welcome.txt" in the same directory (/home/BITS) and sets the permissions of the soft link to 755.



Q1. (B) Suggest commands to the files in the current  working directory for the following cases

Grant only read and write permission to owner, only read permission to group and only execute permission to others for the files with the names STAR1, STAR2. Use octal and values and meta characters.

Delete all the files whose names are ending with the character sequence 'bits' forcibly

Answer: 

To achieve the permissions described for the files STAR1 and STAR2 and delete files ending with the character sequence 'bits', you can use the following commands:

  • Grant permissions to STAR1 and STAR2:
chmod 614 STAR1 STAR2


  • Delete files ending with 'bits':

rm -f *bits




Q1. (C) What is the use of pipe symbol (1) in unix system?

Answer: 

In Unix-like operating systems, the pipe symbol | is a powerful operator used to connect the output of one command to the input of another. It facilitates the creation of pipelines, allowing multiple commands to be chained together, with each command processing data and passing it along to the next command in the pipeline.

Here's how it works:
  • Connects Commands: It connects the standard output (stdout) of one command to the standard input (stdin) of another.
  • Sequential Processing: Commands connected by pipes are executed sequentially, with each command processing the data it receives from the previous command.
  • Example Usage: command1 | command2 executes command1 and feeds its output as input to command2.
  • Data Processing: It's commonly used for filtering, sorting, transforming, and aggregating data, enabling the creation of complex data processing pipelines.
Example:

command1 | command2


Overall, the pipe symbol (|) is a fundamental component of Unix-like systems, providing a flexible and efficient mechanism for connecting commands and building powerful data processing pipelines.





Q2. File1 and File2 contents are as shown below.

File1 Content

SL.NO|ACC No.|NAME|BALANCE

1|123|ABC|2000

2|124|XYZ|3000

3|125|PQR|4000


File2 Content

SL.NO|ACC No.|NAME|BALANCE

1|123|ABC|2000

2|124|YYY|3000

3|125|PPP|4000

4|126|LMO|5000

a) Suggest command to find the first difference between these two files.

b) Suggest command to find the lines unique to File1 and lines common to both the files.

c)Suggest  the command used to get the suggestion to convert the file1 to file 2

d)Do line count and character count in the file1.

e)Write one simple shell script which contains one simple comment statement, one printing statement one read statement to read input and finally one statemet to print the input retrieved



Answer:


a) To find the first difference between the two files, you can use the diff command along with the --changed-group-format option to display only the lines that differ between the files:

diff --changed-group-format='%<' --unchanged-group-format='' File1 File2 | head -n 1


This command compares the contents of File1 and File2 and outputs only the first difference it finds.


b) To find the lines unique to File1 and the lines common to both files, you can use the comm command:

comm -3 <(sort File1) <(sort File2)

This command sorts the contents of both files and then compares them, displaying lines unique to File1 (lines only in the first column), lines unique to File2 (lines only in the second column), and lines common to both files (lines in both the first and second columns). The -3 option suppresses the lines that are common to both files.

c. To suggest a command to convert the content of File1 to match the content of File2, you can use the diff command to find the differences between the two files and then use awk to generate sed commands for making the necessary modifications. Here's the command:

diff -u File1 File2 | grep '^+' | sed 's/^+//' | awk -F '|' '{print "sed -i '\''s/"$0"/"$0"/g'\'' File1"}'


d. To perform line count and character count in File1, you can use the wc command with appropriate options:

# Line count
wc -l File1

# Character count
wc -m File1


e. Here's a simple shell script that contains a comment, a printing statement, a read statement to read input, and finally, a statement to print the input retrieved:

#!/bin/bash

# This is a simple shell script
echo "Enter your name:"
read name
echo "Hello, $name!"






Q3. (A) In the middle of editing a file xipro.txt in vi editor you want to import the output of who command.


Answer: 

To import the output of the who command into the vi editor while editing a file (xipro.txt), you can follow these steps:

  1. Enter command mode in vi editor: Press Esc key to ensure you're in command mode in vi editor.
  2. Invoke shell command: Type :! followed by the command you want to execute. In this case, you want to execute the who command. So, type:   ':!who' Then press Enter.
  3. View output: The output of the who command will be displayed in your terminal window, below the vi editor interface.
  4. Return to vi editor: To return to vi editor, press any key to dismiss the output of the who command and return to vi editor.
  5. Continue editing: You'll now be back in vi editor. You can continue editing your file (xipro.txt) as needed.

Q3. (B). What would be the equivalent linux command for the following command? cat<file1 >file2


Answer:

The command cat<file1 >file2 reads the contents of file1 and writes them to file2. It's essentially a way to copy the contents of file1 to file2.

The equivalent Linux command using standard command-line utilities would be:

cat file1 > file2

Q3. (C) What is the use of the following linux/unix command? ls | tee /dev/tty | grep '^config' | wc -l


Answer: 

The command you provided is a pipeline of multiple commands in Linux/Unix:

  1. ls: Lists the files and directories in the current directory.
  2. tee /dev/tty: Copies the output of the ls command to both standard output and /dev/tty, which represents the current terminal device.
  3. grep '^config': Filters the lines that start with the string 'config'.
  4. wc -l: Counts the number of lines.
So, the overall effect of the command is to list the files and directories in the current directory, display the output on the terminal, filter out lines that start with 'config', and then count the number of remaining lines. This can be useful for finding out how many files or directories in the current directory have names starting with 'config'.

Q3.(D)  Answer : ls -Art | tail -n > master_list


Q3. (E) Answer : The command reads the contents of the specified file, sorts them alphabeticatically, and then displays them page by page using more




Q4. a. Assume you have purchased some items in the super market and its bill contains product number, product name, qty, amount along with the total sum. But you wish to have only the product names, qty and amount of each item in the alphabetical order in a separate file. (create a file for the items using the information provided and give one line command to get the desired output)


b. List the various Input haode commands in the vi editor along with examples


c. What is the difference between $# and $ when we use this in shell scripts.


Answers:


a. To extract only the product names, quantities, and amounts of each item from the bill, sorted alphabetically, you can use a combination of commands like grep, awk, sort, and cut. Assuming the bill is stored in a file named bill.txt, here's a one-liner command to achieve this:

grep -v 'total sum' bill.txt | awk '{print $2, $3, $4}' | sort > items.txt


b. In the vi editor, there are various input mode commands that allow you to perform different editing actions. Here are some common input mode commands along with examples:

  • i: Switch to insert mode before the current cursor position.
    • Example: Press i, then start typing text at the current cursor position.
  • I: Switch to insert mode at the beginning of the current line.
    • Example: Press I, then start typing text at the beginning of the line.
  • a: Switch to insert mode after the current cursor position.
    • Example: Press a, then start typing text after the current cursor position.
  • A: Switch to insert mode at the end of the current line.
    • Example: Press A, then start typing text at the end of the line.
  • o: Open a new line below the current line and switch to insert mode.
    • Example: Press o, then start typing text on the new line.
  • O: Open a new line above the current line and switch to insert mode.
    • Example: Press O, then start typing text on the new line.
c. In shell scripts, $# and $ are different variables:

  • $#: Represents the number of arguments passed to a script or function. 
    • Example: If you run a script with ./script.sh arg1 arg2, then $# will be 2 inside the script because there are two arguments (arg1 and arg2).

  • $: Represents the value of a specific positional parameter.
    • Example: In the script mentioned above, $1 will be arg1 and $2 will be arg2. $$ represents the process ID of the current shell.





Q5. (A) List the three time stamps stored in inode of a file. Which single command that is used to list all the three time stamps?

Answer: 

The three timestamps stored in the inode of a file are:

  1. Access time (atime): This timestamp records the last time the file was accessed (read or executed).
  2. Modification time (mtime): This timestamp records the last time the file's content was modified.
  3. Change time (ctime): This timestamp records the last time the file's metadata (permissions, ownership, etc.) was changed.
To list all three timestamps of a file, you can use the stat command. The command is:

stat <file_name>

Replace <file_name> with the name of the file you want to examine. This command will display detailed information about the specified file, including the access time, modification time, and change time.


Q5. (B) file1.doc contains 1026 bytes and file2.doc contains 2046 bytes. Assuming block size of 1k, how many blocks are allotted for file1.doc file2.doc?


Answer:

Given that the block size is 1 kilobyte (1k), we need to calculate the number of blocks allotted for each file based on their respective sizes.

For file1.doc:
  • Size: 1026 bytes
  • Block size: 1 kilobyte (1k)
  • Number of blocks allotted: ceil(1026 bytes / 1024 bytes per block) = ceil(1026 / 1024) = 2 blocks
For file2.doc:
  • Size: 2046 bytes
  • Block size: 1 kilobyte (1k)
  • Number of blocks allotted: ceil(2046 bytes / 1024 bytes per block) = ceil(2046 / 1024) = 2 blocks
So, both file1.doc and file2.doc are allotted 2 blocks each.

Q5. (C): What information is stored in inode in Linux OS?

Answer: 

Inodes in Linux store crucial metadata about files and directories, essentially acting like an index card for each piece of data. They don't contain the actual file content but hold information that helps the system locate and manage the data. Here's a breakdown of what inodes typically store:

  • File ownership: This includes the User ID (UID) and Group ID (GID) associated with the file, determining access permissions.
  • File size: The total size of the file in bytes.
  • Permissions: Read, write, and execute permissions for owner, group, and others.
  • Timestamps: Information about the file's creation, last access, and last modification times.
  • File type: Whether it's a regular file, directory, symbolic link, etc.
  • Link counter: In the case of hard links, this keeps track of how many hard links point to the same file data.
  • Data block pointers: Inodes hold references (pointers) to the actual data blocks where the file content resides on the storage device. The number of pointers depends on the filesystem and maximum file size.

Q5. (D)Write the steps to create and execute shell script in Linux/or any Unix System.


Answer:

  • Choose a Text Editor: You can use any text editor of your choice, such as Vim, Nano, Emacs, etc., to write your shell script. For example, to create a script called myscript.sh, you can use the command: nano myscript.sh
  • Write the Script: In the text editor, write your shell commands. Here's a simple example of a shell script that prints "Hello, World!":
      • #!/bin/bash
        • # This is a simple shell script 
        • echo "Hello, World!"
      • Save the Script: Save the script in your desired directory.
      • Make the Script Executable: Before you can execute the script, you need to make it executable. You can do this using the chmod command: chmod +x myscript.sh
      • Execute the Script: To execute the script, simply type its name preceded by ./ (dot slash) if it's in the current directory: ./myscript.sh
      • View Output: After executing the script, you should see the output printed to the terminal.