edited by
852 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.

edited by

1 Answer

1 votes
1 votes

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.

Related questions

0 votes
0 votes
2 answers
1
Pooja Khatri asked Jul 13, 2018
1,458 views
Which UNIX/Linux command is used to make all files and sub-directions in the directory "progs" executable by all users?chmod - R a + x progschmod - R 222 progschmod - X a...
0 votes
0 votes
0 answers
2
makhdoom ghaya asked Aug 30, 2016
692 views
Which command allows you to view your file $24$ lines at a time ?MoreCatPgNone of the above
3 votes
3 votes
1 answer
3
go_editor asked Jul 22, 2016
4,160 views
Which of the following is not a part of an expert system shell?Knowledge baseInterference engineExplanation facilityNone of the above
3 votes
3 votes
1 answer
4
Anup patel asked Oct 13, 2017
816 views