September 8, 2024

System Programming Q & A

System Programming
Share :

System Programming Question and Answer


Sorry for inconvenience

Click Here for Q&A :
https://jobdekhoguru.com/system-programming-qa/

(Suggest: Find the question by search page and keep refreshing the page for updated content)

Q. In a Linux system you want to open an existing file a.txt in vi editor. Write appropriate vi commands to fulfill the following requiments
1) a.txt should be opened with cursor at the last line of the file
Answer.
To open an existing file a.txt in vi editor with the cursor at the last line of the file, you can use the following command:
vi + $ a.txt
This will open the file a.txt in vi editor and place the cursor at the last line of the file.
2) Delete the last line and paste it as the first line of the same file.
Answer.
To delete the last line and paste it as the first line of the same file, you can follow these steps:

  1. Move the cursor to the last line of the file by pressing G.
  2. Press dd to delete the last line.
  3. Move the cursor to the first line of the file by pressing gg.
  4. Press P to paste the deleted line at the beginning of the file.

3) Write the line 1 through 15 to a new file a txt.bak.
Answer.
To write the first 15 lines of the file to a new file a.txt.bak, you can use the following command:
1,15w a.txt.bak
This will write the first 15 lines of the file to a new file named a.txt.bak.

4) Exit vi editor without saving any changes.
Answer.
To exit vi editor without saving any changes, you can use the following command:
:q!
This will quit vi editor without saving any changes.


Q. A) If you execute a Shell Script called WASE at command prompt by saying
$sh WASE WIMS EXAM
What will be the values of $0, $$, $#, $”, $@, $?, ?
Answer.

  • The value of $0 will be the name of the shell script which is `WASE`.
  • The value of `$$ will be the
  • process ID of the current shell.
  • The value of `$#` will be 2, which is the number of arguments passed to the script (`WIMS` and `EXAM`).
  • The value of `$” (or `$IFS`) will be the default value of the internal
  • The value of `$” (or `$IFS) will be the default value of the internal field separator, which is a space character by default.
  • The value of $@ will be the list of arguments passed to the script, which are ‘WIMS and EXAM`.
  • The value of `$?` will depend on the execution status of the `ssh` command. If the command executed successfully, it will be 0. Otherwise, it will be a non-zero value indicating the error code. The value of `?` will also be affected by the execution of the `WASE script if it contains any commands that exit with a non- zero status

Q. Discuss the purpose of shift command with an example.
Answer.
The ‘shift’ command in shell scripting is used to shift the positional parameters to the left. The positional parameters are the command-line arguments passed to the script or function.

Each positional parameter is represented by a dollar sign followed by a number, starting from `$0`, which represents the name of the script or function, and `$1`, `$2`, `$3`, and so on, which represent the first, second, and third argument, respectively.

The ‘shift’ command allows you to access the positional parameters beyond `$9 by shifting the parameters to the left, so that the value of `$1` becomes `$0`, the value of $2` becomes `$1`, and so on. The value of `$0` is not changed by the ‘shift’ command.

Here is an example of the `shift` command:

#!/bin/bash

echo "Script name: $0"
echo "Argument 1: $1"
echo "Argument 2: $2" echo "Argument 3: $3"

shift

echo "Shifted..."
echo "Script name: $0"
echo "Argument 1: $1"
echo "Argument 2: $2"

Suppose you save this script as `shift-example.sh’ and execute it with the command `./shift-example.sh a b c’. The output will be:

Script name: ./shift-example.sh
Argument 1: a
Argument 2: b
Argument 3: c
Shifted...
Script name: ./shift-example.sh
Argument 1: b
Argument 2: c

As you can see, the ‘shift’ command shifted the positional parameters to the left, so that `$1`became `b` and `$2` became `c`. The value of `$0` did not change. This allows you to access the you use the ‘shift command again, positional parameters beyond `$9`. If the value of $1` will become `c` and `$2’ will become empty.

 

B) i) What is an infinite loop? Explain with any one example?
Answer.

An infinite loop is a type of programming loop that repeats indefinitely and does not terminate unless the program is interrupted or terminated externally. In an infinite loop, the loop condition is always true, and the loop body keeps executing repeatedly, causing the program to get stuck in an endless loop. This can cause the program to consume system resources, crash or become unresponsive.
Here’s an example of an infinite loop in Python:

while True:
print("This is an infinite loop")

2 / 2

An infinite loop is a type of programming loop that repeats indefinitely and does not terminate unless the program is interrupted or terminated externally. In an infinite loop, the loop condition is always true, and the loop body keeps executing repeatedly, causing the program to get stuck in an endless loop. This can cause the program to consume system resources, crash or become unresponsive.

Here’s an example of an infinite loop in Python:

while True:
print("This is an infinite loop")

In this example, the `while` loop condition is set to `True`, which means the loop will never terminate on its own. The loop body simply prints a message to the console repeatedly, causing the program to get stuck in an infinite loop.

To avoid infinite loops, it’s important to ensure that the loop condition will eventually become false at some point, or to include a conditional statement that will cause the loop to terminate when a specific condition is met.

ii) Write a shell script which takes filename as a command line argument and replaces all string1 present in that file with string2 and save its output in output.txt file.
Answer.
Here’s a shell script that takes a filename as a command line argument, replaces all occurrences of string1 in the file with string2, and saves the output in a file named output.txt:

#!/bin/bash

if [ $# -eq 0 ]; then
echo "Usage: $0 filename"
exit 1
fi

filename=$1

if [ ! -f $filename ]; then
echo "$filename not found"
exit 1
fi

echo "Enter string1 to replace:"
read string1

echo "Enter string2 to replace with:"
read string2

sed "s/$string1/$string2/g" $filename > output.txt

echo "Replacement complete. Check output.txt for the result."

Here’s how this script works:

  1. It first checks whether a filename is provided as a command line argument. If not, it displays an error message and exits the script.
  2. It then checks whether the specified file exists or not. If the file does not exist, it displays an error message and exits the script.
  3. The user is prompted to enter string1 and string2, which are the strings to be replaced and the string to replace them with, respectively.
  4. The sed command is used to perform the replacement. The `s` option tells `sed to search and replace, and the `g option tells it to replace all occurrences of string1 in the file. The output of the `sed` command is redirected to a file named output.txt.
  5. Finally, a message is displayed to indicate that the replacement is complete and the user is prompted to check the output file for the result.

 


For More Updates Join Our Channels :