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:
#!/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
There is no answer for the question 5.
Question 5 is Print only even-numbered arguments.