Unix Short 1
Unix Short 1
1.`ls` Command:
In Unix-like operating systems, the `ls` command is used to list files
and directories. When used with the `-i` option, `ls` displays the
inode number of each file or directory. The inode is a data structure
on a filesystem that stores information about a file or directory,
excluding its name and actual data.
Example:
ls -i
Output:
1234567 file1.txt
7654321 file2.txt
In this example, the `-i` option is used with `ls` to display the inode
numbers of the files (`file1.txt` and `file2.txt`).
For Bash:
echo "alias ls='ls -l --color'" >> ~/.bashrc
source ~/.bashrc
For Zsh:
echo "alias ls='ls -l --color'" >> ~/.zshrc
source ~/.zshrc
After adding the alias, you can simply use the `ls` command, and it
will include the `-l --color` options by default.
5.IS THERE ANY METHOD TO ERASE ALL FILES IN
THE CURRENT DIRECTORY, ALONG WITH ITS ALL
SUBDIRECTORIES, BY USING ONLY ONE
COMMAND ?
Yes, there is a command that allows you to remove all files in the
current directory and its subdirectories. The command you're looking
for is `rm` (remove) combined with the `-r` (recursive) option.
However, please exercise caution when using this command, as it will
permanently delete files, and there's no easy way to recover them.
if [ condition ]; then
echo "Condition is true. Exiting the script."
exit 1 # Exit with status 1 (indicating an error)
fi
Replace "filename" with the name of your file. If the file is large, it
will be displayed in its entirety.
2.`more` Command:
The `more` command displays the content of a file one screen at a
time. You can scroll through the content using the spacebar or the
Enter key:
Example-
more filename
11.WRITE THE SYNTAX OF UNTILL LOOP IN SHELL
PROGRAMMING.
In shell scripting, the `until` loop is used to repeatedly execute a
block of code as long as a specified condition evaluates to false. The
syntax of the `until` loop in shell programming is as follows:
Syntax-
until [ condition ]
do
# Commands to be executed as long as the condition is false
Done
Example-
#!/bin/bash
counter=0
until [ $counter -ge 5 ]
do
echo "Counter: $counter"
((counter++))
done
echo "Loop finished."
12.WRITE THE SYNTAX OF MKDIR IN SHELL
PROGRAMMING.
The `mkdir` command in shell programming is used to create
directories (folders). The syntax of the `mkdir` command is quite
straightforward:
Syntax-
mkdir [options] directory_name
Example:
To create a single directory named "my_directory," you would use:
mkdir my_directory
Example:
if [ "$variable" -eq 10 ]; then
echo "The variable is equal to 10."
fi
2. `test` Command:
Example:
if test "$variable" -eq 10; then
echo "The variable is equal to 10."
fi
Comparison:
if [ condition1 ]; then
# Code block to execute if condition1 is true
elif [ condition2 ]; then
# Code block to execute if condition2 is true
else
# Code block to execute if none of the conditions is true
fi
In the `if-elif-else` structure:
- It allows you to evaluate multiple conditions sequentially.
- If `condition1` is true, the code block inside the first `if` is executed,
and the `elif` and `else` blocks are skipped.
- If `condition1` is false but `condition2` is true, the code block inside
the first `elif` is executed, and the `else` block is skipped.
- If none of the conditions is true, the code block inside the `else` is
executed.
if [ condition ]; then
# Code to execute if the condition is true
else
# Code to execute if the condition is false
fi
Example:
Here's a simple example that checks whether a variable is greater
than 10:
#!/bin/bash
number=15
if [ "$number" -gt 10 ]; then
echo "The number is greater than 10."
else
echo "The number is not greater than 10."
fi
This command searches for the specified pattern in the given file
and prints the lines containing the pattern.
2.Search for a Pattern in Multiple Files:
grep "pattern" file1.txt file2.txt
This searches for the pattern in multiple files and displays the lines
containing the pattern along with the corresponding filenames.
1. Variable Expansion:
- When the dollar sign precedes a variable name, it is used for
variable expansion. The shell replaces `$variable` with the value
stored in the variable.
Example-
greeting="Hello"
echo $greeting
2. Special Variables:
- Certain variables have predefined meanings and are referred to
using the dollar sign. For example, `$0` represents the name of the
script or shell, and `$1`, `$2`, etc., represent command-line
arguments.
Example-
echo "Script name: $0"
echo "First argument: $1"
19.WHAT ARE POSITIONAL PARAMETER IN SHELL
SCRIPT ?
In shell scripting, positional parameters refer to the command-line
arguments or parameters that are passed to a shell script or a shell
function when it is executed. These parameters are assigned to
special variables, and their values can be accessed within the script.
The positional parameters are represented by variables like `$1`, `$2`,
`$3`, and so on.
Example-
counter=0
#include <stdio.h>
#include <unistd.h>
int main() {
pid_t child_pid = fork();
if (child_pid == 0) {
printf("This is the child process (PID: %d)\n", getpid());
}
else if (child_pid > 0) {
printf("This is the parent process (PID: %d)\n", getpid());
}
else {
perror("fork");
return 1;
}
return 0;
}
1. Creation:
- Parent Process: The process that creates another process is called
the parent process. The parent process typically uses a system call
like `fork()` to create a new process.
- Child Process: The process that is created by another process is
called the child process. The child process is an exact copy of the
parent process at the time of the `fork()` system call.
2. Process ID (PID):
- Parent Process: It has its own unique Process ID (PID), which is
assigned by the operating system when the process is created.
- Child Process: It also has its own unique PID, different from the
parent's PID. The child process inherits most attributes, including the
PID, from its parent.
3. Parent-Child Relationship:
- Parent Process: The parent process may continue to execute
independently of the child process or may choose to wait for the
child to complete its execution.
- Child Process: The child process is created as a copy of the parent,
and it has a reference to its parent's PID. It can run independently or
perform specific tasks requested by the parent.
2. Run:
- Definition: A process is in the "run" state when it is actively being
executed by the CPU. In this state, the instructions of the process are
being executed, and it is utilizing the CPU resources.
- Context: When a process is in the run state, it is actively
performing its tasks, and the CPU is dedicated to executing its
instructions.
- Transition: A process transitions from the ready state to the run
state when the scheduler allocates CPU time to it. The process may
later transition back to the ready state when it voluntarily yields the
CPU, is preempted by a higher-priority process, or completes its
execution.
1. Process ID (PID):
- Description: A unique numerical identifier assigned to each
process by the operating system.
- Purpose: Used to distinguish one process from another. PIDs are
assigned sequentially as processes are created.
5. Group ID (GID):
- Description: Identifies the primary group associated with the
process.
- Purpose: Helps determine the group-level permissions and access
rights for the process.
27.WHAT IS KERNEL ?
The kernel is a critical component of an operating system (OS) that
acts as an intermediary between software applications and the
computer's hardware. It is responsible for managing system
resources, providing essential services, and facilitating
communication between hardware and software components. The
kernel is loaded into the computer's memory during the boot process
and remains in memory as long as the system is running.
Key functions of the kernel include:
1. Process Management
2. Memory Management
3. File System Management
4. Device Management
5. Security and Access Control
Multiuser
A multiuser operating system enables two or more users to
run programs simultaneously. Networks, servers,
mainframes, and supercomputers allow hundreds to thousands
of users to connect at the same time, and thus are multiuser.
Multitasking
A multiprocessing operating system supports two or more
processors running programs at the same time also known
as Multitasking. Multiprocessing involves the coordinated
processing of programs by more than one processor.
Multiprocessing increases a computer’s processing speed.
2. Hierarchy:
- Directories can be organized in a hierarchical structure, forming a
tree-like arrangement. This hierarchy helps in organizing and
categorizing files based on their relationships.
3. Path:
- The location or address of a directory within the file system is
represented by a path. A path includes the names of all the
directories leading to the target directory, separated by slashes (e.g.,
`/home/user/documents`).
#!/bin/bash
echo "Enter your name:"
read username
echo "Enter your age:"
read age
echo "Hello, $username! You are $age years old."
1. Pattern Matching:
- The `case` structure allows for pattern matching against different
values or expressions. Patterns can include simple literals, wildcards,
and regular expressions, providing versatility in handling various
cases.
2. Simplified Syntax:
- It simplifies the syntax and structure when dealing with multiple
conditions. This can make the code more readable and maintainable
compared to a series of nested `if-elif-else` statements.
3. Logical Flow:
- The `case` structure provides a clear and logical flow for handling
different cases. It enhances code organization and makes it easier to
follow the execution path.
Here's an example using `expr` along with `index` to find the position
of a character in a string:
#!/bin/bash
string="Hello, World!"
char="o"
position=$(expr index "$string" "$char")
echo "Position of '$char' in '$string': $position"
#!/bin/bash
while true
do
echo "This is an infinite loop. Press Ctrl+C to break out."
done
#!/bin/bash
for ((i=1; i<=10; i++))
do
echo "This is iteration number $i"
done