unix detail 2nd internals
unix detail 2nd internals
The grep command is used to search for patterns in files or input streams. It prints matching
lines based on the provided pattern.
Syntax:
Examples:
Basic search:
grep "error" logfile.txt
Common options:
Option Description
-i Case-insensitive search
-v Invert match (exclude pattern)
-n Show line numbers of matches
-r Recursively search directories
-c Count the number of matching lines
-l Show filenames with matches
--color Highlight matches in output
-E Use extended regular expressions
Wildcards are special characters used in shell commands to match filenames or patterns.
Common Shell Wildcards:
Example:
ls *.sh
3. Explain if and while control statements in shell scripts with suitable programs.
ifstatement:
The if statement evaluates a condition and executes code based on the result.
Syntax:
if [ condition ]; then
commands
fi
Example:
#!/bin/bash
echo "Enter a number:"
read num
if [ $num -gt 10 ]; then
echo "The number is greater than 10."
else
echo "The number is 10 or less."
fi
while statement:
The while loop executes a block of code repeatedly while a condition is true.
Syntax:
while [ condition ]; do
commands
done
Example:
#!/bin/bash
count=1
while [ $count -le 5 ]; do
echo "Count: $count"
count=$((count + 1))
done
4. Explain for and while control statements in shell scripts with suitable
programs.
for loop:
The for loop iterates over a list of items.
Syntax:
Example:
#!/bin/bash
for file in *.txt; do
echo "Processing $file"
done
5. Explain the following with their prototypes: open(), close(), create(), read(),
and write().
Diagram:
+------------------+
| Command-Line Arg | <-- High Memory
+------------------+
| Stack | (grows downward)
+------------------+
| Heap | (grows upward)
+------------------+
| BSS Segment | (uninitialized data)
+------------------+
| Data Segment | (initialized data)
+------------------+
| Text Segment | (program code)
+------------------+
7. What are daemon processes? Mention and explain coding rules of daemon
processes.
Daemon Process:
A daemon process runs in the background, typically for performing system tasks like logging or
scheduling.
Characteristics:
Coding Rules:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
if (fork() > 0) exit(0); // Parent exits
setsid(); // Create new session
chdir("/"); // Change working directory
while (1) {
// Daemon task here
sleep(1);
}
}
8. What is error logging? With a neat block diagram, discuss error logging
facility for a daemon process.
Error Logging:
Error logging records errors or system events to a file or logging facility.
Block Diagram:
+-------------------+ +-----------------+
| Daemon Process | | syslog() |
+-------------------+ --> +-----------------+ --> Log File
Example:
#include <syslog.h>
int main() {
openlog("mydaemon", LOG_PID, LOG_DAEMON);
syslog(LOG_ERR, "An error occurred!");
closelog();
return 0;
}
Here are the page numbers in the PDF where the information is covered (based on my search):
The grep command in UNIX is used to search for specific patterns in files or streams. It scans
the input line by line and outputs lines that match the pattern.
Syntax:
grep [OPTIONS] PATTERN [FILE...]
Common Options:
Option Description
Examples:
1. Basic Search:
2. grep "error" logfile.txt
3. Case-Insensitive Search:
4. grep -i "warning" logfile.txt
5. Recursive Search:
6. grep -r "TODO" /home/user/code
7. Count Matches:
8. grep -c "error" logfile.txt
Wildcards are special characters in UNIX shells used to match filenames or patterns. They
simplify file selection in commands.
Common Shell Wildcards:
Wildcard Description Example
Examples:
3. Explain if and while control statements in shell scripts with suitable programs.
if Statement:
The if statement allows conditional execution of commands based on the result of a test.
Syntax:
if [ condition ]; then
commands
elif [ condition ]; then
commands
else
commands
fi
Example:
#!/bin/bash
read -p "Enter a number: " num
if [ $num -gt 10 ]; then
echo "The number is greater than 10."
else
echo "The number is 10 or less."
fi
while Statement:
The while loop executes commands repeatedly as long as a condition evaluates to true.
Syntax:
while [ condition ]; do
commands
done
Example:
#!/bin/bash
count=1
while [ $count -le 5 ]; do
echo "Count: $count"
count=$((count + 1))
done
Page Reference: Pages 157–160 in the PDF.
4. Explain for and while control statements in shell scripts with suitable
programs.
for Loop:
Syntax:
Example:
#!/bin/bash
for file in *.txt; do
echo "Processing $file"
done
while Loop:
5. Explain the following with their prototypes: open(), close(), create(), read(),
and write().
Prototypes:
Function Prototype Description
open() int open(const char *pathname, int flags, ...); Opens a file.
creat() int creat(const char *pathname, mode_t mode); Creates a new file.
read() ssize_t read(int fd, void *buf, size_t count); Reads data from a file.
Diagram:
+------------------+
| Command-Line Arg |
+------------------+ <-- High Memory
| Stack |
+------------------+
| Heap |
+------------------+
| BSS Segment |
+------------------+
| Data Segment |
+------------------+
| Text Segment |
+------------------+
Page Reference: Pages 203–205 in the PDF.
7. What are daemon processes? Mention and explain coding rules of daemon
processes.
Definition:
A daemon process runs in the background, detached from any terminal, and typically performs
system-level tasks like logging or scheduling.
Characteristics:
Coding Rules:
8. What is error logging? With a neat block diagram, discuss error logging
facility for a daemon process.
Definition:
Error logging is a mechanism for recording system errors or application messages for debugging
or monitoring purposes.
Steps in Error Logging for Daemons:
Block Diagram:
+-------------------+ +-----------------+
| Daemon Process | | syslog() |
+-------------------+ --> +-----------------+ --> Log File
Example:
#include <syslog.h>
int main() {
openlog("mydaemon", LOG_PID, LOG_DAEMON);
syslog(LOG_ERR, "An error occurred!");
closelog();
return 0;
}
Page Reference: Pages 469–473 in the PDF.