0
0
Jenkinsdevops~20 mins

Shell steps (sh, bat) in Jenkins - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Shell Steps Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
What is the output of this Jenkins shell step?
Consider this Jenkins pipeline shell step using sh to list files. What will be the output if the workspace contains files file1.txt and file2.txt?
Jenkins
sh 'ls *.txt'
Afile1.txt file2.txt
BNo output, command fails
Cls: cannot access '*.txt': No such file or directory
D
file1.txt
file2.txt
Attempts:
2 left
💡 Hint
Think about how the shell expands wildcards and how Jenkins captures output.
Troubleshoot
intermediate
1:30remaining
Why does this Jenkins bat step fail on Windows agent?
This Jenkins pipeline step runs a Windows batch command but fails with 'command not found'. What is the likely cause?
Jenkins
bat 'echo Hello World'
AThe <code>echo</code> command is misspelled
BThe agent is Linux, so <code>bat</code> commands won't run
CThe Jenkinsfile syntax is incorrect for <code>bat</code>
DThe workspace path is invalid
Attempts:
2 left
💡 Hint
Consider the environment where the command runs.
Configuration
advanced
2:00remaining
How to capture output of a shell step in Jenkins pipeline?
You want to save the output of a shell command into a variable in a Jenkins pipeline scripted syntax. Which snippet correctly captures the output?
Adef result = sh(script: 'date')
Bdef result = bat(script: 'date', returnStdout: true)
Cdef result = sh(script: 'date', returnStdout: true).trim()
Ddef result = sh('date')
Attempts:
2 left
💡 Hint
Look for the option that returns command output as a string.
Best Practice
advanced
2:00remaining
Which approach is best for cross-platform shell steps in Jenkins pipeline?
You want to write a Jenkins pipeline that runs shell commands on both Linux and Windows agents. Which approach is best?
AUse <code>sh</code> step for Linux and <code>bat</code> step for Windows inside <code>if</code> condition checking OS
BUse only <code>sh</code> step for all agents
CUse only <code>bat</code> step for all agents
DWrite commands in Groovy without shell steps
Attempts:
2 left
💡 Hint
Think about agent OS compatibility.
🔀 Workflow
expert
2:30remaining
Order the steps to run a shell command, capture output, and archive a file in Jenkins pipeline
Arrange these Jenkins pipeline steps in the correct order to run a shell command, save its output, and archive a file named output.log:
A1,3,2,4
B3,1,2,4
C1,2,3,4
D2,1,3,4
Attempts:
2 left
💡 Hint
Think about creating the file before reading and archiving it.