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

HELP!!!! Using Putty (UNIX) !!! Commands to study to answer this question: Read

ID: 3801379 • Letter: H

Question

HELP!!!! Using Putty (UNIX) !!!

Commands to study to answer this question: Read about test, shift, while loop, expr,

        displaying the value of the two-character variable $1 using echo command

1) Exit from script, if is currently enabled; if it is not enabled, skip this item (1)

2) Using the vi editor, create a shell script file that displays the first 12 command line arguments. You must use a while loop, expr and shift shell commands in the script to do this question.

3) Issue script command to capture the output of the items below

4) Add execute permission to the file

5) Display the attributes of the script file

6) Display the content of the script file

7) Run the script file by typing the name of the script file and specifying 14 or more arguments

The following are helpful hints in creating the shell script file for this question:

The expr command can be used to perform arithmetic operations. For example, the value of a user defined variable, count can be incremented by 1 (that is, add 1 to count) by using the following expr command.

                        count=`expr $count + 1`

NOTE: (1) there should be no space before and after = when storing a value into a variable, (2) Reverse quote (`) is used and not forward quote (‘) in the above command.

Explanation / Answer

I have done the below scripting for BASH shell . You can know your shell by typing "echo $0" on your command line.

You can check a condition and make a decision on whether to continue or not in a script using "test" command. We are going to use it for your first requirement. Before running the script please execute the following command on the terminal.

export should_exit=1

The script checks the variable and if it is equal to "1" then it exits otherwise it proceeds. To enable execution

of the script execute following command before running the script.

export should_exit=0

Let the script name be "script.sh"

#script command logs whatever commands gets executed on the command line and its output to a file.
# -c option is used with the "script" command to provide the command name.
# -a option is used with the "script" command to append the output in the log file.


#To make a file executable we use the chmod command but it doesn't print anything if everything goes fine.

script -c "chmod +x script.sh" ./log.txt

#Display attributes of the script file

script -c "ls -l script.sh" -a ./log.txt

#Display contents of the script

script -c "cat script.sh" -a ./log.txt

You can now check the "log.txt" file to see the output of each of the above executed command.

Following is the script.

#!/bin/bash

#Test the script should exit or not
if test $should_exit = 1 ; then

  echo "I am exiting . Bye";
   exit 0;
fi


#Print the first 12 command line arguments
arg=1;
while [ $arg -le 12 ]
do
echo "Argument $arg : $1";
shift;
arg=`expr $arg + 1`
done

TO execute the script with 14 arguments : (Remember to export variable should_exit before running the script)

export should_exit=1

script -c "./script.sh 101 102 103 104 105 106 107 108 109 110 111 112 113 114" -a log.txt

OUTPUT :

I am exiting . Bye

export should_exit=0

script -c "./script.sh 101 102 103 104 105 106 107 108 109 110 111 112 113 114" -a log.txt

OUTPUT :

Argument 1 : 101
Argument 2 : 102
Argument 3 : 103
Argument 4 : 104
Argument 5 : 105
Argument 6 : 106
Argument 7 : 107
Argument 8 : 108
Argument 9 : 109
Argument 10 : 110
Argument 11 : 111
Argument 12 : 112