Monday, November 19, 2012

Using the Perl chomp() function


Using the Perl chomp() function

Introduction

The chomp() function will remove (usually) any newline character from the end of a string. The reason we say usually is that it actually removes any character that matches the current value of $/ (the input record separator), and $/ defaults to a newline.
For more information on the $/ variable, try perldoc perlvar and see the entry for $/.

Example 1. Chomping a string

Most often you will use chomp() when reading data from a file or from a user. When reading user input from the standard input stream (STDIN) for instance, you get a newline character with each line of data. chomp() is really useful in this case because you do not need to write a regular expression and you do not need to worry about it removing needed characters.
When running the example below, using the enter key on a line by itself will exit the program.
  #!/usr/bin/perl
  use strict;
  use warnings;

  while (my $text = ) {
    chomp($text);
    print "You entered '$text'\n";
    last if ($text eq '');
  }
Example usage and output of this program is:
  a word
  You entered 'a word'
  some text
  You entered 'some text'

  You entered ''

Example 2. Chomping an array

If you chomp an array, it will remove a newline from the end of every element in the array:
  #!/usr/bin/perl
  use strict;
  use warnings;

  my @array = ("bob\n", "jill", "fred\n");

  print "Before chomp:\n";
  print "@array\n";

  chomp(@array);

  print "After chomp:\n";
  print "@array\n";
This program produces the following output:
  Before chomp:
  bob
   jill fred

  After chomp:
  bob jill fred
As you can see, the newlines have been removed from "bob" and "fred", but no characters have been removed from "jill".

Example 3. Chomping a hash

If you pass a hash into chomp(), it will remove newlines from every value (not key) of the hash:
  #!/usr/bin/perl
  use strict;
  use warnings;

  my %hash = (
    'first' => "one\n",
    'second' => "two\n",
    'third' => "three\n",
  );

  chomp(%hash);

  foreach my $k (keys %hash) {
    print "$k: $hash{$k} ";
  }

  print "\n";

  exit 0;
This program outputs:
  first: one second: two third: three

Wednesday, November 14, 2012

ReturnCode in unix


Shell Command Exit Status

The return value of a command is its exit status, or 128 + N if the command is terminated by signal N. Exit status is used to check the result (success/failure) of the execution of the command. If the exit status is zero, then the command is success. If the command is failed the exit status will be non-zero.

Exit Value Exit Status
0 (Zero) Success
Non-zero Failure
2 Incorrect usage
127 Command Not found
126 Not an executable
$? Shell Variable

The shell variable name $? is a special built-in variable which has the exit status of the last command executed.

After the shell function execution, $? returns the exit status of the last command executed in a function.
After the shell script execution, $? returns the exit status of the last command executed in the script.
Sample Shell Script that Explains Shell-Command Exit Status

The following exitstatus.sh shell-script shows examples of various shell-command exit status.

$ cat exitstatus.sh
#! /bin/bash

echo -e "Successful execution"
echo -e "====================="
echo "hello world"
# Exit status returns 0, because the above command is a success.
echo "Exit status" $?

echo -e "Incorrect usage"
echo -e "====================="
ls --option
# Incorrect usage, so exit status will be 2.
echo "Exit status" $?

echo -e "Command Not found"
echo -e "====================="
bashscript
# Exit status returns 127, because bashscript command not found
echo "Exit status" $?

echo -e "Command is not an executable"
echo -e "============================="
ls -l execution.sh
./execution.sh
# Exit status returns 126, because its not an executable.
echo "Exit status" $?
Now, execute the above exitstatus.sh to see the various exit statues given by the sample shell script.

$ bash exitstatus.sh
Successful execution
=====================
hello world
Exit status 0
Incorrect usage
=====================
ls: unrecognized option `--option'
Try `ls --help' for more information.
Exit status 2
Command Not found
=====================
exitstaus.sh: line 15: bashscript: command not found
Exit status 127
Command is not an executable
=============================
-rw-r--r-- 1 root root 659 Mar  9 13:36 execution.sh
exitstatus.sh: line 21: ./execution.sh: Permission denied
Exit status 126