exam questions

Exam LX0-103 All Questions

View all questions & answers for the LX0-103 exam

Exam LX0-103 topic 3 question 49 discussion

Actual exam question from CompTIA's LX0-103
Question #: 49
Topic #: 3
[All LX0-103 Questions]

When running the command -
sed -e "s/a/b/" /tmp/file >/tmp/file
while /tmp/file contains data, why is /tmp/file empty afterwards?

  • A. The file order is incorrect. The destination file must be mentioned before the command to ensure redirection.
  • B. The command sed did not match anything in that file therefore the output is empty.
  • C. When the shell establishes the redirection it overwrites the target file before the redirected command starts and opens it for reading.
  • D. Redirection for shell commands do not work using the > character. It only works using the | character instead.
Show Suggested Answer Hide Answer
Suggested Answer: C 🗳️

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
linux_admin
2 years, 2 months ago
Here's why: The command sed -e "s/a/b/" /tmp/file is used to replace all occurrences of the character a with the character b in the file /tmp/file. The > symbol is used for redirection to write the output to a file, in this case, to overwrite the original file /tmp/file. When the shell processes the command, it first opens the file /tmp/file for writing, and then executes the sed command. Since the output file and the input file are the same (/tmp/file), the shell will overwrite the contents of the file before the sed command can read it. As a result, the sed command will not find any occurrences of the character a in the input file, and will output nothing to the output file. Therefore, the file /tmp/file will end up being empty after the command completes.
upvoted 1 times
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...
exam
Someone Bought Contributor Access for:
SY0-701
London, 1 minute ago