0
0
Bash Scriptingscripting~20 mins

Temporary files (mktemp) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Temporary File Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this mktemp command?
Consider the following command run in a bash shell:

mktemp /tmp/tempfile.XXXXXX

What does this command output?
Bash Scripting
mktemp /tmp/tempfile.XXXXXX
AThe literal string '/tmp/tempfile.XXXXXX' without changes
BA unique temporary filename starting with /tmp/tempfile. followed by 6 random characters
CA directory named tempfile.XXXXXX created in /tmp
DAn error message because mktemp requires no arguments
Attempts:
2 left
💡 Hint
mktemp replaces the XXXXXX with random characters to create a unique filename.
💻 Command Output
intermediate
2:00remaining
What error does this mktemp command produce?
What error message will this command produce?

mktemp /tmp/tempfile

Note: The template does not contain any 'X' characters.
Bash Scripting
mktemp /tmp/tempfile
Amktemp: too few X's in template '/tmp/tempfile'
BNo error, creates a file named /tmp/tempfile
Cmktemp: cannot create temporary file
Dmktemp: invalid option -- 't'
Attempts:
2 left
💡 Hint
mktemp requires at least 3 'X' characters in the template to replace.
🔧 Debug
advanced
2:00remaining
Why does this mktemp command fail to create a file?
Given this command:

mktemp -d /tmp/mydir.XXXXXX

It fails with an error 'cannot create directory'. What is the most likely reason?
Bash Scripting
mktemp -d /tmp/mydir.XXXXXX
AThe -d option is invalid and causes failure
BThe template must not contain any X characters when using -d
CThe /tmp directory does not exist or is not writable
Dmktemp cannot create directories, only files
Attempts:
2 left
💡 Hint
Check if the directory where you want to create the temp directory exists and is writable.
🚀 Application
advanced
2:00remaining
Which script snippet safely creates and cleans up a temporary file?
You want to create a temporary file, write 'Hello' into it, then delete it after use. Which snippet does this correctly?
Atmpfile=$(mktemp) && echo 'Hello' > "$tmpfile" && cat "$tmpfile" && rm "$tmpfile"
Btmpfile=mktemp && echo 'Hello' > $tmpfile && cat $tmpfile && rm $tmpfile
Cmktemp > tmpfile && echo 'Hello' > tmpfile && cat tmpfile && rm tmpfile
Dtmpfile=$(mktemp) && echo 'Hello' > tmpfile && cat tmpfile && rm tmpfile
Attempts:
2 left
💡 Hint
Remember to capture the output of mktemp in a variable and quote variables when used.
🧠 Conceptual
expert
2:00remaining
Why is mktemp preferred over manual temp file naming?
Why should scripts use mktemp to create temporary files instead of manually naming files like /tmp/tempfile.txt?
AManual naming automatically cleans up files after use
BManual naming is faster and more secure than mktemp
Cmktemp creates files with fixed names for easier debugging
Dmktemp guarantees unique filenames and avoids race conditions
Attempts:
2 left
💡 Hint
Think about what happens if two scripts use the same temp filename.