Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a bash script accepting an arbitrary number of arguments specifying a subc

ID: 3745179 • Letter: W

Question

Write a bash script accepting an arbitrary number of arguments specifying a subcommand and its arguments (similar to what is required for task6). The script will run the specified subcommand with all the specified arguments, monitoring its execution using strace. For instance, if the script is invoked in the following way /script.sh 1s -1 -a it will execute strace in the following way: strace 1s -1-a The script must redirect the stderr ouput from strace and the executed subcommand properly. Specifically, the script must not print the mentioned stderr output, but process it as specified below When the executed subcommand finishes, the script has to print to stdout a list of al the system calls executed by the subcommand (in no particular order, but with no repetitions For instance, if the script is invoked in the following way . /script, sh uname -n The output should be: x86.64 accesS arch-prcti brk close execve exit-group fstat nmap mprotect munmap open read uname rite I suggest redirecting stderr to a temporary file. By running the subcom mand with strace, stderr will contain all the executed syscalls (and their parameters) You can then process the content of this temporary file to extract just the syscall names, by splitting every line, using the character " as delimiter To do so, I suggest using avk. Once you have a list of the executed syscalls you can use sort and uniq to remove repetitions.

Explanation / Answer

#Initializing a counter to get the command line arguments
j=0

for i in "$@"
do
# Incrementing the counter for checking
j=$((j+1))
if (($j == 1))
then
# The First command line argument is the subcommand
cmd=$i
else
# Appending the arguments for the subcommand
cmd=$cmd" "$i
fi
done

# Executing the command strace and redirecting it to Log_output.txt using tee
strace $cmd 2>&1|tee Log_output.txt

# Reading the Log_output.txt lines one by one and getting the first part using separator as '(' in the awk command
while read line; do
awk -F "(" '{print $1}' line
done <Log_output.txt

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote