0% found this document useful (0 votes)
10 views4 pages

Lab Assignment 9 10 Sols

Uploaded by

Rishi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

Lab Assignment 9 10 Sols

Uploaded by

Rishi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CMSC 216 Lab Assignment 9 and 10 Fall 2024

Shell Commands

Prerequisite
Before you answer the questions, run the following command:
cp -r ~/216public/labs/week16lab1 ~/216.
Then, cd ~/216/week16lab1.

Questions
Examine the provided shell commands, considering each one separately. Write (1) the output produced and (2) the exit
status value (use echo $?) returned by each command. For commands that include |, <, >, or (), briefly explain the
process of command execution. If a command fails, explain why it failed.

1. cal -3 | wc -l

Solution
Output: 8
Status: 0
Explanation: standard output from cal -3 is piped to wc -l.

2. wc -l files/loremipsum.txt

Solution

Output: 8 files/loremipsum.txt
Status: 0

3. true && echo "hello" > data.txt

Solution
Output: none
Status: 0
Explanation: true exits with 0 always (i.e., success). So, echo is run. It prints "hello", which is redirected to
data.txt

4. echo "hello" > data.txt | wc -l

Solution
Output: Ambiguous output redirect.
Status: 1
Explanation: echo prints "hello" to stdout and the stdout is redirected to data.txt. At the same time the
output should be piped to "wc -l". So, ambiguous output redirect.

5. seq 10 | shuf > data2.txt

Solution
Output: none
Status: 0
Explanation: seq 10 prints 10 numbers to stdout, which is piped to shuf (shuffled) and then redirected to
data2.txt.

1
6. cat files/numbers.txt | sort

Solution
Output:
1
10
2
3
5

Status: 0
Explanation: "cat files/numbers.txt" prints the contents of the file numbers.txt to stdout, which is piped to
sort. The sorted output is displayed to terminal

7. cal 2024 | grep -o 29 | wc -l

Solution
Output: 12
Status: 0
Explanation: cal 2024 prints the calendar for 2024 to stdout, which is piped to grep -o 29. The output is 12
lines of 29. The output is piped to wc -l, so it prints 12.

8. true && cat loremipsum.txt | grep -o 29 | wc -l

Solution
Output:
cat: loremipsum.txt: No such file or directory
0

Status: 1
Explanation: true exits with 0 always (success), so shell attempts to run cat. It cannot find loremipsum.txt
in the current directory and prints the error message (printed to stderr). The stdout will not have contents
but is anyway piped to "grep -o 29", which fails to find "29" in the input and the blank stdout is in turn
piped to "wc -l", which prints 0.

9. seq 10 | shuf | sort | false

Solution
Output: none
Status: 1
Explanation: seq 10 prints 10 numbers to stdout, which is piped to shuf (shuffled) and then piped to sort.
The sorted output is piped to false, which does not print anything, always fails, and exits with 1.

2
10. seq 10 | shuf | sort && false

Solution
Output:

1
10
2
3
4
5
6
7
8
9

Status: 1
Explanation: seq 10 prints 10 numbers to stdout, which is piped to shuf (shuffled) and then piped to sort.
The sorted output is displayed to terminal. Then, it executes false, which always fails and exits with 1.

11. ( cd files && fmt -w 80 numbers.txt ) | grep 5


Was the directory changed to files? If not, why?

Solution
Output: 1 5 3 10 2
Status: 0
Explanation: In a subshell, change directory to the files directory and run fmt. It prints the formatted
contents of the file numbers.txt to stdout, which is piped to grep 5. Because the line contains 5, the line is
printed to the terminal.

12. seq 10 | shuf < files/numbers.txt

Solution
Output: Ambiguous input redirect.
Status: 1
Explanation: seq prints 10 lines (1 through 10) to stdout, which is piped to shuf as stdin. shuf also takes the
file (numbers.txt) as the input as well. So, ambiguous input redirect.

13. seq 2 && shuf < files/numbers.txt | grep 3

Solution
Output:
1
2
3

Status: 0
Explanation: seq prints 1 and 2 to stdout. Then, shuf takes input from the file, shuffles, and prints the
output to stdout, which is piped to grep 3, which prints the line with 3.

3
Process Control, Unix I/O, and Pipes
Complete LabAssignment_9_10_dist.c (available under the ~/216public/labs/week16lab1 directory). You only need to
give the else clause. Don’t forget to hand-write the code. We won’t accept typed answers.

Solution

int main(void)
{
char value[MAX_STR_LEN + 1];

int pipe_fd[2];
pid_t child_pid;

pipe(pipe_fd);
child_pid = fork();
if (child_pid) {
int fd, k;
close(pipe_fd[0]);

fd = open("numbers.txt", O_RDONLY);
dup2(fd, STDIN_FILENO);
close(fd);
while (fgets(value, MAX_STR_LEN + 1, stdin)) {
sscanf(value, "%d", &k);
write(pipe_fd[1], &k, sizeof(int));
}
close(pipe_fd[1]);

wait(NULL);
} else {
int fd, sum = 0, k;
close(pipe_fd[1]);

do {
read(pipe_fd[0], &k, sizeof(int));
printf("%d\n", k);
if (k >= 0) {
sum += k;
}
} while (k >= 0);
close(pipe_fd[0]);

sprintf(value, "Sum: %d\n", sum);

fd = open("output.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);


write(fd, value, strlen(value));
close(fd);

exit(0);
}

return 0;
}

You might also like