in Others edited by
851 views
0 votes
0 votes

The following source code corresponding to a 'bash' shell script 'sl.sh' in UNIX system:

for i in $*
do
       cat $i
done

What will be the output if 'sl.sh' is executed from the console as follows:
$sh sl.sh *

(A) Displays contents of file labeled as ‘*’
(B) Displays list of all the files in present working directory.
(C) Displays contents of all the files in present working directory.
(D) Displays an error message.

in Others edited by
851 views

1 Answer

1 vote
1 vote

Answer (C)

Script in a unix (or linux) system is executed as 

sh <script_name> <input_arguments>

So, when a script is executed as 

$sh sl.sh *
Then sl.sh means the name of the script to execute and * means the input arguments.

When * is written in shell, it means the list of all files in the current directory. So, the argument list to the shell script being executed is the list of all files executed.

$* in the (bash) shell script file means the list of all input arguments.

So, the for loop is iterating over the list of all input arguments.

for i in $*

and each argument of current iteration is held in variable 'i'. Since the input arguments is a list of files of current directory, so 'i' holds the name of a file which is in current directory.

cat <filename> command displays the content of the file. So

cat $i

above would display the contents of a file whose name is held in 'i'.

This will happen for each file name which is provided in the input argument list. Remember, input argument list is $*, which means the list of all files in current directory.

So, the for loop would iterate over the list of files in the current directory and for each file, it would display the contents due to use of cat command.

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true