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

task6 Write a bash script accepting three arguments: (stdout file), (stderr file

ID: 3745177 • Letter: T

Question

task6 Write a bash script accepting three arguments: (stdout file), (stderr file), and (cwd), followed by an arbitrary number of other ar- guments specifying a subcommand and its arguments. The script will run the specified subcommand with all the specified arguments, redirecting the subcommand's stdout to (stdout file) and the subcommand's stderr to stderr file), and setting the subcommand's current working directory to (cwd) For instance, if the script is invoked in the following way: /script.sh /tmp/stdout /tmp/stderr /bin ls -1 -a the command 1s will be run with the arguments -1 and -a. When run, ls's current working directory must be /bin, the stdout output must be saved in (stdout file), and the stderr output must be saved in (stderr file) The script must not change its own current working directory. The script should print to stdout the erit code of the executed subcommand. Nothing else should be print to stdout or stderr To refer to all the arguments specified by the user after the argument cud), I suggest using the bash array variable "$Q: (n))" (substituting (n) with a proper number). I suggest using a subshell not to change the script (cwd) https://en.wikipedia.org/wiki/Unix_time https://www.tldp.org/LDP/abs/html/subshells.html

Explanation / Answer

Hi,

First things first, here is the script you requested for:

#!/bin/bash

out_stream=$1

err_stream=$2

curr_dir=$3

subcommand=${@:(4)}

(

cd $curr_dir && $subcommand

)> $out_stream 2> $err_stream

echo $?

The code is pretty much self explainatory but here are a few things that might come handy :

To access a command line variable inside a script, simply access it using $n, where n is the index of variable. In the first part of code, we assign each command line argument to a variable for readability.

We store in stream as out_stream, error stream file as err_stream and the the directory to be used as curr_dir.

To refer the rest of the subcommand, we use ${@:(4)} to skip the first three arguments and start from fourth. That is, we store each argument after 3rd index as a single string in variable subcommand.

Subshell starts a new process inside the shell program. It is enclosed in the parentheses. In the subshell we simply change to the expected directory and execute the required command.

As required in the task, we use '$?' to print the exit code of last executed command which in our case is the subshell.

To redirect the stream (output or error) after any command or subshell, we supply the redirection as:

Command/subshell >(output_stream) 2>(error_stream)

Executing the script:

Make sure to change the permissions of the script before executing.

$ chmod 755 script.sh

$ ./script.sh <stdout_file_path> <std_err_file_path> <target_dir> <rest of the command(s)>

Eg:

$ ./script.sh out.txt err.txt ~/ ls -alrt