Most Frequently asked Interview Questions of shell(2024)
Question: What is the difference between hard links and symbolic links?
Answer:
In Unix-like operating systems, both hard links and symbolic links (also known as symlinks) are used to create references to files. However, they differ in how they operate and are managed by the file system. Here’s a detailed breakdown of their differences:
1. Definition and Concept:
-
Hard Link: A hard link is essentially another name for an existing file on the file system. It points directly to the inode (index node) of a file, which is the underlying data structure storing the actual file data. Both the original file and the hard link are indistinguishable; they both point to the same inode.
-
Symbolic Link (Symlink): A symbolic link is a special type of file that contains a reference (or path) to another file or directory. It acts as a shortcut, pointing to the location of the target file or directory, rather than directly referencing the inode.
2. How They Work:
- Hard Link:
- When you create a hard link, the file system simply creates a new directory entry pointing to the same inode as the original file. Both files (the original and the hard link) are treated as equal references to the same data.
- If the original file is deleted, the data is not lost as long as there is at least one hard link still referencing the inode.
- Symbolic Link:
- A symbolic link is like a pointer or a path to another file. It does not point directly to the file’s inode but rather contains the path of the target file.
- If the target file is moved or deleted, the symbolic link becomes broken and will point to a non-existent file, leading to a “dangling” link.
3. File System and Linking Behavior:
-
Hard Link:
- A hard link can only be created within the same file system, as it directly references the inode.
- You cannot create a hard link to a directory (except for the special
.
and..
entries) to prevent the creation of circular references.
-
Symbolic Link:
- A symbolic link can span across different file systems, as it only stores a path (which can reference files or directories on different file systems).
- Symbolic links can point to directories, unlike hard links.
4. Link Count:
-
Hard Link:
- A hard link increases the link count of a file. When the link count drops to zero (i.e., when all hard links are deleted), the data in the file is deleted as well.
- Multiple hard links to a file mean that the file’s content is still accessible even if one link is removed.
-
Symbolic Link:
- A symbolic link does not affect the link count of the target file. If you delete the symbolic link, it does not affect the original file.
- The symlink’s existence is independent of the target file’s link count.
5. Use Cases:
-
Hard Link:
- Hard links are generally used for making exact copies of files or ensuring that a file’s data remains available even if its name is removed.
- They are not commonly used by most users, as symlinks provide more flexibility.
-
Symbolic Link:
- Symbolic links are widely used in many scenarios, such as creating shortcuts, linking configuration files, or creating references across different directories or file systems.
- They are easier to manage and understand for most users.
6. Commands:
-
Hard Link: To create a hard link, use the
ln
command:ln existing_file hard_link_name
-
Symbolic Link: To create a symbolic link, use the
ln -s
command:ln -s target_file symlink_name
7. Advantages and Disadvantages:
-
Hard Link:
- Advantages:
- Hard links are a true, low-level copy of the original file.
- No risk of a broken link if the target file is deleted.
- Disadvantages:
- Cannot link to directories (except for
.
and..
). - Limited to the same file system.
- Hard to track, as there’s no distinction between the original file and the hard link.
- Cannot link to directories (except for
- Advantages:
-
Symbolic Link:
- Advantages:
- Flexible: can point to files or directories across file systems.
- Easier to create and manage.
- Can link to directories.
- Disadvantages:
- If the target file is deleted or moved, the symbolic link becomes broken (dangling symlink).
- Less efficient than hard links because they require extra metadata for the path.
- Advantages:
8. Example:
Creating a Hard Link:
$ echo "Hello, World!" > file.txt
$ ln file.txt hardlink.txt
Now, both file.txt
and hardlink.txt
point to the same inode. Deleting one will not affect the other:
$ rm file.txt
$ cat hardlink.txt
Hello, World!
Creating a Symbolic Link:
$ echo "Hello, World!" > file.txt
$ ln -s file.txt symlink.txt
Now, symlink.txt
points to file.txt
. If you delete file.txt
, the symbolic link becomes broken:
$ rm file.txt
$ cat symlink.txt
cat: symlink.txt: No such file or directory
Summary of Differences:
Feature | Hard Link | Symbolic Link |
---|---|---|
Points to | Directly to the inode (data of the file) | The file path (not the inode) |
File System | Must be on the same file system | Can span across file systems |
Target | Cannot link to directories (except . and .. ) | Can link to files and directories |
Effect on Original File | Increases link count (file data exists as long as there’s one link) | No effect on link count |
Link Type | True duplicate file reference | A pointer (shortcut) to the original file |
Handling Deleted Files | If the original file is deleted, the data remains accessible through the hard link | If the target file is deleted or moved, the symlink becomes broken (dangling) |
Command | ln | ln -s |
Common Usage | Making exact copies of files | Creating shortcuts, linking files across directories or file systems |
In conclusion, hard links are more like multiple names for the same file, while symbolic links are like shortcuts that can point to files or directories anywhere in the system.
Question: How can you read a file line by line in a Shell script?
Answer:
To read a file line by line in a Shell script, you can use a while
loop combined with the read
command. The read
command reads each line from the file, and the loop continues until all lines have been processed.
Here is a basic structure for reading a file line by line in a Shell script:
#!/bin/bash
# Specify the file you want to read
filename="yourfile.txt"
# Check if the file exists
if [[ ! -f "$filename" ]]; then
echo "File not found!"
exit 1
fi
# Read the file line by line
while IFS= read -r line; do
# Process each line (you can replace this echo with other logic)
echo "$line"
done < "$filename"
Explanation:
-
File Check: The script first checks if the file exists using the
-f
option. If the file is not found, the script exits. -
Reading the File:
while IFS= read -r line
:IFS=
: This prevents leading/trailing whitespaces from being trimmed from each line. By default, the Internal Field Separator (IFS
) is set to whitespace, so setting it to an empty string ensures that the entire line is read exactly as it appears.-r
: This preventsread
from interpreting backslashes as escape characters, ensuring the line is read as-is.line
: The variable that stores the contents of each line.
-
File Input Redirection:
< "$filename"
: This redirects the content of the file to thewhile
loop for line-by-line processing.
-
Processing Each Line: Inside the
while
loop, the variableline
holds each line from the file. You can replace theecho "$line"
statement with any other logic you need to process each line.
Example:
Consider a file file.txt
with the following content:
Hello, World!
Welcome to Shell scripting.
This is line 3.
Running the script will produce:
Hello, World!
Welcome to Shell scripting.
This is line 3.
Alternative Method (Using cat
):
Another approach to reading a file line by line is by using the cat
command along with a pipe:
#!/bin/bash
filename="yourfile.txt"
# Read the file using cat and pipe
cat "$filename" | while IFS= read -r line; do
echo "$line"
done
Note: The first method (using file input redirection <
) is generally preferred because it avoids creating an additional process for cat
. This method can be slower, especially for large files.
Summary:
- The
read
command is used inside awhile
loop to read a file line by line. - You can customize the processing of each line inside the loop.
- The approach can be extended to handle file contents in different ways, such as processing data or manipulating file entries.
Question: What are environment variables and how do you set them in Shell?
Answer:
Environment Variables:
Environment variables are dynamic values that affect the behavior of processes or programs in a shell or operating system. They store system-wide settings or user-specific configurations that can be used by the shell or any running processes. Environment variables typically store information such as system paths, user preferences, and configuration settings.
Common environment variables include:
PATH
: Specifies directories where executable programs are located.HOME
: Represents the home directory of the current user.USER
: The name of the current logged-in user.SHELL
: The path to the current shell (e.g.,/bin/bash
).EDITOR
: The default text editor to use (e.g.,vim
ornano
).
Environment variables are typically inherited by child processes, which allows programs or scripts to access them.
Setting Environment Variables:
There are two main ways to set environment variables in a Shell: temporarily for the current session, and permanently across sessions.
1. Setting Environment Variables Temporarily:
To set an environment variable temporarily in a shell, you can use the export
command. The variable will be available to the current shell session and any child processes, but it will be lost once the session ends.
export VAR_NAME="value"
Example:
export MY_VAR="Hello World"
echo $MY_VAR # Output: Hello World
In this example:
MY_VAR
is an environment variable."Hello World"
is the value assigned toMY_VAR
.echo $MY_VAR
outputs the value ofMY_VAR
to the terminal.
2. Setting Environment Variables Permanently:
To set environment variables permanently so they persist across sessions, you need to add them to configuration files like .bashrc
, .bash_profile
, .profile
, or .zshrc
, depending on the shell you are using.
For Bash Shell:
-
Open the
.bashrc
or.bash_profile
file in your home directory:nano ~/.bashrc
-
Add the export statement for the environment variable:
export MY_VAR="PermanentValue"
-
Save the file and exit the editor (
Ctrl + O
,Enter
,Ctrl + X
innano
). -
Apply the changes by sourcing the file:
source ~/.bashrc
After this, the environment variable will be available in every new shell session.
3. Viewing Environment Variables:
To view the value of an environment variable, you can use the echo
command:
echo $VAR_NAME
To list all environment variables, use the printenv
or env
command:
printenv
This will display a list of all environment variables and their values for the current session.
4. Unsetting Environment Variables:
To remove an environment variable from the current session, use the unset
command:
unset VAR_NAME
Example:
unset MY_VAR
echo $MY_VAR # Output: (empty, as MY_VAR is unset)
Summary of Key Points:
- Environment variables store system settings or user configurations.
- You can set environment variables temporarily using the
export
command. - To set environment variables permanently, you add them to configuration files like
.bashrc
,.bash_profile
, or.zshrc
. - Use
echo $VAR_NAME
to view an environment variable’s value andunset VAR_NAME
to remove it.
Question: How would you schedule a job to run at a specific time using cron?
Answer:
In Unix-like operating systems, cron is a job scheduler that allows you to run commands or scripts automatically at specified intervals or times. You can use cron to schedule recurring tasks such as backups, log rotation, or system maintenance.
Steps to Schedule a Job Using Cron:
-
Open the crontab file: To schedule jobs using cron, you need to edit your crontab (cron table) file. Each user has their own crontab file. To open the crontab file for editing, use the following command:
crontab -e
-
Cron Syntax: A cron job entry follows a specific syntax, which includes the timing and the command to execute. The format for a cron job is:
* * * * * command_to_run ┬ ┬ ┬ ┬ ┬ │ │ │ │ │ │ │ │ │ └─ Day of the week (0 - 7) (Sunday is 0 or 7) │ │ │ └──── Month (1 - 12) │ │ └────── Day of the month (1 - 31) │ └──────── Hour (0 - 23) └────────── Minute (0 - 59)
- Minute: The minute (0-59) when the command should run.
- Hour: The hour (0-23) when the command should run.
- Day of the month: The day of the month (1-31) when the command should run.
- Month: The month (1-12) when the command should run.
- Day of the week: The day of the week (0-7, where 0 and 7 represent Sunday) when the command should run.
If you want the cron job to run at a specific time, you replace the
*
in the corresponding fields with the desired value.
Examples:
-
Run a script at 3:00 PM every day:
0 15 * * * /path/to/script.sh
0
represents the minute (0th minute of the hour).15
represents the hour (3 PM).*
for the day of the month, month, and day of the week means the script will run every day.
-
Run a command every Monday at 5:30 AM:
30 5 * * 1 /path/to/command
30
represents the 30th minute.5
represents 5 AM.1
represents Monday (days of the week are represented as 0-6, with 1 being Monday).
-
Run a command on the 1st of every month at midnight:
0 0 1 * * /path/to/command
0
represents the 0th minute (midnight).0
represents the 0th hour (midnight).1
represents the first day of the month.*
for the month and day of the week means the command will run on the 1st of each month.
-
Run a job every 10 minutes:
*/10 * * * * /path/to/script.sh
*/10
means every 10 minutes.- The other fields (
*
) mean it will run every hour, day, and month.
-
Run a job at 2:30 AM every Sunday:
30 2 * * 0 /path/to/script.sh
30
represents 30 minutes past the hour.2
represents 2 AM.0
represents Sunday (could also use7
).
Saving and Exiting:
Once you’ve added your cron job, save and exit the editor:
- If you’re using
nano
, pressCtrl + O
to save, thenEnter
to confirm, andCtrl + X
to exit. - If you’re using
vim
, pressEsc
, type:wq
, and pressEnter
.
Viewing Cron Jobs:
To view the list of scheduled cron jobs for the current user, run:
crontab -l
This will display the contents of your crontab file.
Removing Cron Jobs:
To remove a specific cron job, simply open the crontab file again using crontab -e
, delete the line containing the job, and save the file. Alternatively, to remove all jobs for the current user, run:
crontab -r
Cron Job Logging:
By default, cron jobs are logged to the system’s log file, usually located at /var/log/syslog
or /var/log/cron
depending on your distribution. You can use the following command to check cron logs:
tail -f /var/log/syslog # On Debian/Ubuntu-based systems
tail -f /var/log/cron # On Red Hat/CentOS-based systems
Summary:
- Use the
crontab -e
command to edit your cron jobs. - Cron job syntax:
minute hour day month day_of_week command_to_run
. - The
*
wildcard means “every” in that field. - To view your jobs, use
crontab -l
; to remove them, usecrontab -r
.
With these steps, you can schedule recurring tasks or automate scripts to run at specific times using cron.
Read More
If you can’t get enough from this article, Aihirely has plenty more related information, such as shell interview questions, shell interview experiences, and details about various shell job positions. Click here to check it out.
Tags
- Shell scripting
- Bash
- Sh
- Shell commands
- File manipulation
- Grep
- Awk
- Sed
- Find command
- Ps command
- Chmod
- Stdout
- Stderr
- Error handling
- File redirection
- For loop
- Line by line file reading
- Cron jobs
- Environment variables
- Symbolic links
- Hard links
- Script arguments
- Pipeline
- Shell scripting best practices
- Linux commands
- System administration
- Command line tools