We can iterate over each line from standard input by using while and read
commands.
while read -r line; do
# Process each line here
done
Given /etc/passwd file as standard input (&0),
extract the usernames and convert them to lowercase.
script.sh shell script file.&0).&1).root:x:0:0:root:/root:/bin/bash
user:x:1000:1000:User:/home/user:/bin/bash
Sayan:x:1001:1001:Sayan:/home/sayan:/bin/bash
root
user
sayan
/etc/passwd.:).## Motivation We can iterate over each line from standard input by using `while` and `read` commands. ```bash while read -r line; do # Process each line here done ``` ## Problem Statement Given `/etc/passwd` file as standard input (`&0`), extract the usernames and convert them to lowercase. ## Instructions - Write your solution in `script.sh` shell script file. - The script should read the input from standard input (`&0`). - The output should be printed to standard output (`&1`). - No files will be provided, so you need to read from standard input. ## Sample Input ```text root:x:0:0:root:/root:/bin/bash user:x:1000:1000:User:/home/user:/bin/bash Sayan:x:1001:1001:Sayan:/home/sayan:/bin/bash ``` ## Sample Output ```text root user sayan ``` ## Constraints - The input will always be in the format of `/etc/passwd`. - Usernames are the first field in each line, separated by a colon (`:`). ## Hints - Try using variable manipulation solely to solve this question. ## Motivation ### Folding A line of text can be folded into multiple lines in two broad ways: - **Fixed-width folding**: where each line has a fixed number of characters. This can be done using the `fold` command in Linux. - **Variable-width folding**: where the line is folded based on the content, such as breaking at spaces or punctuation. This can be done using the `grep -o` command in Linux. ```bash > cat data 123456789 > fold -w 3 data 123 456 789 ``` ```bash > grep -o '[0-9]\+' <<< "1,23,456" 1 23 456 ``` ### Unfolding Unfolding is the reverse of folding, where multiple lines are combined into a single line. This can be done using the `paste` command in Linux. ```bash > cat data 1 2 3 > paste -sd'*' < data 1*2*3 ``` Read more about them in `man fold`, `man grep`, and `man paste`. ## Problem Statement You are given a $n$ dimensional vector in one line. Find the L1 norm of the vector, which is the sum of the absolute values of its components. ## Instructions - Write your solution in `script.sh` shell script file. - The script should read the input from standard input (`&0`). - The output should be printed to standard output (`&1`). ## Sample Input ```text [1,-25,4] ``` ## Sample Output ```text 30 ``` ## Constraints - The expressions will always be well-formed. - The vector can have any number of elements. - The components of the vector can be positive or negative integers. ## Motivation ### Fibonacci Numbers Fibonacci numbers are a sequence of numbers where each number is the sum of the two preceding ones, usually starting with 1 and 1. The sequence commonly starts as follows: 1, 1, 2, 3, 5, 8, 13, 21, and so on. $$ F_n = F_{n-1} + F_{n-2} $$ ## Problem Statement You are given a positive integer `n`. Your task is to print a triangle of `n` rows consisting of the first $n \times (n + 1) / 2$ fibonacci numbers. ## Instructions - Write your solution in `script.sh` shell script file. - The script should read the integer `n` from standard input. - The output should be formatted as a triangle, with each row containing the appropriate number of fibonacci numbers. ## Sample Input ```text 4 ``` ## Sample Output ```text 1 1 2 3 5 8 13 21 34 55 ``` ## Constraints - `n` is a positive integer. - The output should be formatted with each number separated by a space. - The last number in each row should not have a trailing space.