Knowee
Questions
Features
Study Tools

You're developing a Bash script that needs to create a temporary directory, perform some operations within it, and then delete the temporary directory and its contents when done. Which command sequence ensures the secure creation and deletion of the temporary directory?1.0 Markstmp_dir="/tmp/mytempdir"mkdir $tmp_dirrm -r $tmp_dirtmp_dir=$(mktemp -d)rm -r $tmp_dirtmp_dir="/tmp/mytempdir"trap "rm -r $tmp_dir" EXITmkdir $tmp_dirtmp_dir=$(mktemp -d)trap "rm -r $tmp_dir" EXITtmp_dir="/tmp/mytempdir"mkdir $tmp_dirrm -rf $tmp_dir

Question

You're developing a Bash script that needs to create a temporary directory, perform some operations within it, and then delete the temporary directory and its contents when done. Which command sequence ensures the secure creation and deletion of the temporary directory?1.0 Markstmp_dir="/tmp/mytempdir"mkdir tmpdirrmrtmp_dirrm -r tmp_dirtmp_dir=(mktempd)rmr(mktemp -d)rm -r tmp_dirtmp_dir="/tmp/mytempdir"trap "rm -r tmpdir"EXITmkdirtmp_dir" EXITmkdir tmp_dirtmp_dir=(mktempd)trap"rmr(mktemp -d)trap "rm -r tmp_dir" EXITtmp_dir="/tmp/mytempdir"mkdir tmpdirrmrftmp_dirrm -rf tmp_dir

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The correct command sequence for securely creating and deleting a temporary directory in a Bash script is:

tmp_dir=$(mktemp -d)
trap "rm -r $tmp_dir" EXIT

Here's a step-by-step explanation:

  1. tmp_dir=$(mktemp -d): This command creates a new temporary directory. The mktemp -d command creates a unique temporary directory and returns its name. The $(...) syntax is a command substitution, meaning it runs the command inside the parentheses and replaces the command with its output. So this line is creating a new temporary directory and storing its name in the tmp_dir variable.

  2. trap "rm -r $tmp_dir" EXIT: The trap command allows you to specify commands that will be executed when a certain signal is received. In this case, the signal is EXIT, which is sent when the script exits for any reason. The command rm -r $tmp_dir will delete the temporary directory and its contents. So this line is setting up a trap that will delete the temporary directory whenever the script exits.

This problem has been solved

Similar Questions

What command is used to remove a directory?

What command is used to remove the directory in Linux?(1 Point)rdrdirrmdirremove

Which command is suitable in deleting directory that contains files in it?2.0 Marks#rm –r directory#rm directory#remove directory#delete directory

Identify the command which is used to remove a directory0.5 Marksrm -frmdeleterm -R

Explain the following Linux commands:(1) mkdir(2) touch(3) cat(4) rm

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.