top of page
book of spells.webp

Start your journey towards mastering the CLI with our FREE Command Line Book of Spells.

By entering your email address you agree to receive emails from Command Line Wizardry. We'll respect your privacy and you can unsubscribe at any time.

Cybersecurity Ops with bash - Chapter 2 Solutions

Writer: Cybersecurity Ops with bashCybersecurity Ops with bash

Below are selected solutions for the Chapter 2 workshop questions from Cybersecurity Ops with bash.


Question 2


Modify the osdetect.sh script to use a function. Put the if/then/else logic inside the function and then call it from the script. Don’t have the function itself produce any output. Make the output come from the main part of the script.


Answer


Note that this is just one of many possible solutions:



 

Question 3


Set the permissions on the osdetect.sh script to be executable (see man chmod) so that you can run the script without using bash as the first word on the command line. How do you now invoke the script?


Answer


The chmod command can be used to give a script execute permissions. A value of 755 will give all users the ability to execute the script.


Once the script has execute permissions is can be executed directly using ./ rather than using the bash.




 

Question 4


Write a script called argcnt.sh that tells how many arguments are supplied to the script.

a. Modify your script to have it also echo each argument, one per line.

b. Modify your script further to label each argument like this:


$ bash argcnt.sh this is a "real live" test

there are 5 arguments

arg1: this

arg2: is

arg3: a

arg4: real live

arg5: test


Answer


Note that this is just one of many possible solutions:



 
 
 

5 Comments


Sergio O
Sergio O
Oct 27, 2022

#!/bin/bash

#Print only even-numbered arguments


echo there are $# arguments

i=0

for ARG

do

let i++

if [ $(( i % 2 )) -eq 0 ]

then

echo "arg$i: $ARG"

fi

done


Like
wearier_birdie0y
Apr 08, 2023
Replying to

Hi. What is happening in this piece of code?


$(( i % 2 ))

Like

Hello World
Hello World
Jul 07, 2022

There is no answer for the question 5.

Question 5 is Print only even-numbered arguments.

Like
bottom of page