Q-* (bash)
1.Write a `script.sh` which will take input from standard input and write output
4 │ to standard output.
5 │
6 │ The input is the `/etc/passwd` file. Your script should read the input
7 │ line by line using `while` loop with `read` and perform the following:
8 │
9 │ - If the line is a comment, then skip it.
10 │ - If the line is not a comment, then print the username and home
directory
11 │ of the user.
12 │ - If the user has a description (comment) in the GECOS field,
13 │ then print the description instead of the username.
14 │ - If the home directory is of the form `/home/username`,
15 │ then print '/home/$USER' literally.
16 │ - If not printing the username,
17 │ then print the home directory as it is.
18 │
19 │ Your script should only print regular users, not system users. (Hint: UID
>= 1000)
20 │
21 │ Your script should delimit records using '---'
Ans:
NOTE: IFS=":" read -r username x uid gid comment home shell <<<
"$line":
Splits the line into fields using : as the delimiter (as used in /etc/passwd
files).
Assigns each field to a variable: username, x (typically a placeholder for
the password field), uid (user ID), gid (group ID), comment (user's real
name or description), home (home directory), and shell (login shell).
-lt means less then
if [[ -n $comment ]]; then:
Checks if the comment field is non-empty (-n checks for a non-zero length
string).
If the comment exists, it prints it
2. Write a `script.sh` which will take input from standard input and write output
6 │ to standard output.
7 │ You are given heart attack data. From the given data through standard
input,
8 │ print only the lines that have outliers in the heart rate field. The data
9 │ may or may not have a header row. The heart rate field is the 3rd field in
10 │ the data. The heart rate field is an integer. An outlier is a value that is
11 │ more than 1.5 times the interquartile range from the third quartile, or less
12 │ than 1.5 times the interquartile range from the first quartile.
13 │
14 │ **Header format**:
15 │
16 │ ```csv
17 │ Age,Gender,Heart rate,Systolic blood pressure,Diastolic blood
pressure,Blood sugar,CK-MB,Troponin,Result
18 │ ```
Ans:
Note:
if [[ $(bc <<< "$heartrate < $lower") -eq 1 || $(bc <<< "$heartrate >
$upper") -eq 1 ]] ; then:
This if statement checks if the heartrate is outside the range defined by
the variables lower and upper.
bc <<< "$heartrate < $lower":
o This uses bc, a command-line calculator, to perform the
comparison. The <<< operator is a here-string that passes the
string on the right to bc.
o If heartrate is less than lower, bc will output 1.
-eq 1:
o Checks if the result of the bc comparison is 1 (true).
||:
o The logical OR operator. The condition is true if either the heartrate
is less than lower or greater than upper.
If the condition is true (i.e., the heartrate is outside the defined range), the
script proceeds to the next step.
3. Write a `script.sh` which will take input from standard input and write output
4 │ to standard output.
5 │ You are given two files, file1 and file2 in the standard input.
6 │ The content of two files is separated by a line 'EOF'.
7 │ Print the following information:
8 │
9 │ - Lines that are common in both files.
10 │ - Lines that are only in file1.
11 │ - Lines that are only in file2.
12 │
13 │ Order of the output should be unchanged.
ANS:
NOTE:
grep -Fxf file1 file2:
Finds lines that are present in both file1 and file2. The -F option treats the
pattern as a fixed string, -x matches whole lines, and -f file1 reads
patterns from file1.
grep -Fvxf file2 file1:
Finds lines in file1 that are not in file2. The -v option inverts the match, so
only lines not matching any pattern are shown.
comm Command Output Columns:
When you compare two sorted files using comm, the output is divided into three
columns:
1. Column 1: Lines that are only in the first file.
2. Column 2: Lines that are only in the second file.
3. Column 3: Lines that are common to both files.
comm -12 file1 file2:
Suppresses: Columns 1 and 2.
Shows: Only the lines that are common to both files (Column 3).
comm -23 file1 file2:
Suppresses: Columns 2 and 3.
Shows: Only the lines that are unique to the first file (Column 1).
4.