Perl and Shell Script
Q. Can you explain the exit status of shell and commands under Linux / UNIX operating system?
Ans. All UNIX and Linux command has a several parameters or variables that can be use to find out the exit status of command. Please note that these parameters or variables may only be referenced assignment to them is not allowed. You can use $? to find out the exit status of command. $? always expands to the status of the most recently executed foreground command or pipeline.
Q.What’s the conditional statement in shell scripting?
Ans: if {condition} then … fi
Q.How do you do number comparison in shell scripts? -
Ans: -eq, -ne, -lt, -le, -gt, -ge
Q.How do you test for file properties in shell scripts?
Ans: -s filename tells you if the file is not empty,
-f filename tells you whether the argument is a file, and not a directory,
-d filename tests if the argument is a directory, and not a file,
-w filename tests for writeability,
-r filename tests for readability,
-x filename tests for executability
Q. .Nested condititional statement?
Ans: if COMMANDS ; then
COMMANDS;
[ elif COMMANDS; then COMMANDS ; ]...
[ else COMMANDS; ]
fi
Q. File operator Comparison ?
Ans: Operator Returns true if:
-d file file exists and is a directory
-e file file exists
-f file file exists and is a regular file
-r file file exists and you have read permission on it
-s file file exists and is more than 0 bytes in size
-w file You have write permission on file
-x file You have execute permission on file, or search permission if it is a directory
-O file You are the owner of file
-G file You are a member of the group that owns the file
file1 -nt file2 file1 is newer than file2
file1 -ot file2 file1 is older than file2
Remember that many of these commands can be used interactively, at the command prompt, as well as in scripts. For example, here is a command line that will show the disk space used by every subdirectory of the current directory:
for n in * ; do if [ -d $n ]; then du -hs $n ; fi; done
you can also perform string comparisons, as shown in Table 2.
String operators:
Operator Returns true if:
str1 = str2 str1 matches str2
str1 != str2 str1 does not match str2
str1 < str2 str1 is less than str2
str1 > str2 str1 is greater than str2
-n str1 str1 is non-null (i.e. is not an empty string)
-z str1 str1 is null (i.e. has length zero)
And finally, you can make comparisons between integer variables, as shown in Table 3. You'll see a little later how to perform arithmetic.
Integer operators
Operator Returns true if:
var1 -lt var2 var1 is less than var2
var1 -le var2 var1 is less than or equal to var2
var1 -eq var2 var1 is equal to var2
var1 -ne var2 var1 is not equal to var2
var1 -gt var2 var1 is greater than var2
var1 -ge var2 var1 is greater than or equal to var2
The for Statement
Q.How do you do Boolean logic operators in shell scripting?
Ans: - ! tests for logical not,
-a tests for logical and, and
-o tests for logical or.
Q.How do you find out the number of arguments passed to the shell script?
Ans: - $#
Q.How do you write a for loop in shell?
Ans: for {variable name} in {list} do {statement} done
Q.How do you write a while loop in shell?
Ans: while {condition} do {statement} done
Q.What is source command ?
Ans:The can be used to load any functions file into the current shell script or a command prompt.It read and execute commands from given FILENAME and return.The pathnames in $PATH are used to find the directory containing FILENAME. If any ARGUMENTS are supplied, they become the positional parameters when FILENAME is executed.The syntax is as follows:
source functions.sh
Q.What is PERL ?
Ans: Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. ...
Q. What is a hash?
Ans: Hashes are complex lists with both a key and a value part for each element of the list. We define a hash using the percent symbol (%).
Hashes are complex list data, like arrays except they link a key to a value. To define a hash, we use the percent (%) symbol before the name.
Q.What is array?
Ans:Arrays contain a list of scalar data (single elements). A list can hold an unlimited number of elements. In Perl, arrays are defined with the at (@) symbol.
Q. What is scalar variable?
Ans: Scalar variables are simple variables containing only one element--a string, a number, or a reference. Strings may contain any symbol, letter, or number. Numbers may contain exponents, integers, or decimal values.
Q. What does the command ‘use strict’ do and why should you use it?
Ans: "Use strict" is one of the pragma used for checking the variable scope and syntax. If you use this pragma means, you need to specify the scope (my, local, our) for the variable and use the exact usage of operators.
Q. What do the symbols $ @ and % mean when prefixing a variable?
Ans: $ indicate scalar data type
@ indicates an array and
% indicates an hash or an associative array.
Q.Explain the difference between "my" and "local" variable scope declarations. ?
Ans: Both of them are used to declare local variables.The variables declared with "my" can live only within the block it was defined and cannot get its visibility inherited functions called within that block, but one defined with "local" can live within the block and have its visibility in the functions called within that block.
Q. How do you find the length of an array?
Ans: $@array
Q. Difference between for & foreach, exec & system?
Ans: Goto command prompt & type:
perldoc -f function name
perldoc -q keywords
Q. Perl uses single or double quotes to surround a zero or more characters. Are the single(' ') or double quotes (" ") identical?
Ans: They are not identical. There are several differences between using single quotes and double quotes for strings.
1. The double-quoted string will perform variable interpolation on its contents. That is, any variable references inside the quotes will be replaced by the actual values.
2. The single-quoted string will print just like it is. It doesn't care the dollar signs.
3. The double-quoted string can contain the escape characters like newline, tab, carraige return, etc.
4. The single-quoted string can contain the escape sequences, like single quote, backward slash, etc.
Q. Difference between for & foreach,exec & system?
Ans: - Technically, there's no difference between for and foreach other than some style issues.
- For and foreach both are for looping the code but if you want to use array in place of scalar for loop, you have to use foreach keyword
Both Perl's exec() function and system() function execute a system shell command. The big difference is that system() creates a fork process and waits to see if the command succeeds or fails - returning a value. exec() does not return anything, it simply executes the command. Neither of these commands should be used to capture the output of a system call. If your goal is to capture output, you should use the backtick operator:
Q. What is meant by 'chomp'? Where do we require this ?
Ans: chomp is used to eliminate the new line character.
Q. What is CPAN? What are the modules coming under this?
Ans: CPAN ic Comprehencive Perl Archive Network. Its a repository contains thousands of Perl modules, source and documentation, and all under GNU/GPL or similar licence. you can go to www.cpan.org for more details. Some Linux distributions provide a tool named "cpan" with wich you can install packages directly from CP
Q. what does this “'$_' ? “ mean :
Ans: '$_' ? .Its an Default variable in Perl, where the input from the user will be taken into this variable if the variable is not defined by the user.
Q. How does a? grep? Function performs?
Ans: Grep returns the number of lines the expression is true. Grep returns a sublist of a list for which a specific criterion is true. This function often involves pattern matching. It modifies the elements
Q. What is Perl one-liner?
Ans: There are two ways a Perl script can be run:
--from a command line, called one-liner, that means you type and execute immediately on the command line. You'll need the -e option to start like "C: %gt perl -e "print "Hello";". One-liner doesn't mean one Perl statement. One-liner may contain many statements in one line.
--from a script file, called Perl
Q. Is there any way to add two arrays together?
Ans: Of course you can add two arrays together by using push function. The push function adds a value or values to the end of an array. The push function pushes the values of list onto the end of the array. Length of an array can be increased by the length of list.
my @planets = qw(mercury venus earth mars);
my @outer_planets = qw(jupiter saturn uranus neptune pluto);
push (@planets, @outer_planets);
Q. How to use the command shift and unshift?
Ans: Shift array function shifts off the first value of the array and returns it, thereby shortening the array by one element and moving everything from one place to the left. If you donĂ¢€™t specify an array to shift, shift uses @ ARGV, the array of command line arguments passed to the script or the array named @-.
unshift() - adds an element to the beginning of an array.
@myNames = ('Larry', 'Curly', 'Moe');
$oneName = shift(@myNames);
shift() and pop() remove from an array - unshift() and push() add.
shift() and unshift() work on the beginning of an array, push() and pop() on the end. In other words, shift() and unshift() do the same thing to the left end of an array that push() and pop() do to the right end. Hence; to take from the left and put on the right,
Q. What are the different types of perl operators?
Ans: There are four different types of perl operators they are
(i) Unary operator like the not operator
(ii) Binary operator like the addition operator
(iii) Tertiary operator like the conditional operator
(iv) List operator like the print operator
Q. What is subroutine?
Ans: perl allows you to write your own user-defined function which is called subroutine. Subroutine allows you to reuse a chunk of code in program over and over again.
Q. What is push and pop?
Ans: Push() add an item to end of array.
Pop() removes item from end of array.
Perl modules
A Perl module is a self-contained piece of Perl code that can be used by a Perl program or by other Perl modules. It is conceptually similar to a C link library, or a C++ class.
Modular programming is a software design technique that increases the extent to which software is composed of separate, interchangeable components, called modules by breaking down program functions into modules, each of which accomplishes one function and contains everything necessary to accomplish this.[1] Conceptually, modules represent a separation of concerns, and improve maintainability by enforcing logical boundaries between components. Modules are typically incorporated into the program through interfaces.[2] A module interface expresses the elements that are provided and required by the module. The elements defined in the interface are detectable by other modules.
Comments
Post a Comment