Errors and Exceptions

commandline session

$gcc error.c
$cat error.c
#include

main()
{
int a=0;
int b=10;
printf("Hello Alln");
printf("%d",b/a);
}

$./a.out > outputa
Floating point exception
$cat outputa
$
$./a.out 2> outputa
Hello All
Floating point exception
$./a.out > outputa
Floating point exception
$./a.out 2> outputb
Hello All
Floating point exception
$./a.out &> outputc
Floating point exception
$cat outputa
$
$cat outputb
$cat outputc
$

An Error “indicates serious problems that a reasonable application should not try to catch.

An Exception “indicates conditions that a reasonable application might want to catch.

source : http://stackoverflow.com/questions/5813614/what-is-difference-between-errors-and-exceptions

pattern matching operators PART 1

commandline session

$cat pm.sh
#!/bin/bash

filename=${1##*/}

echo $filename
$sh pm.sh /bin/bash
bash
$vim pm.sh
$cat pm.sh
#!/bin/bash

filename=${1}

echo $filename
$sh pm.sh /bin/bash
/bin/bash
$vim pm.sh
$cat pm.sh
#!/bin/bash

filename=${#}

echo $filename
$sh pm.sh /bin/bash
1
$vim pm.sh
$cat pm.sh
#!/bin/bash

filename=${##}

echo $filename
$sh pm.sh /bin/bash
1
$vim pm.sh
$cat pm.sh
#!/bin/bash

filename=${*}

echo $filename
$sh pm.sh /bin/bash
/bin/bash
$vim pm.sh
$cat pm.sh
#!/bin/bash

filename=${/}

echo $filename
$sh pm.sh /bin/bash
pm.sh: 3: pm.sh: Bad substitution
$

Bash . number of script arguments

commandline session

$. var 1 2 3 4
4
$. var 1 2 3 4 3 3 3
7
$cat var
Name=Jeffrin
Age=36

echo $#
$. var
0
$. var .
1
$. var $
1
$. var ?
5
$. var ?
5
$. var 1 2
2
$. var ?
5
$echo $?
0
$. var *
768
$. var )
bash: syntax error near unexpected token `)'
$. var &
[1] 5679
$0

[1]+  Done                    . var
$. var ^
1
$. var %
1
$. var @
1
$. var !
1
$. var ~
1
$. var -
1
$. var _
1
$. var +
1
$. var =
1
$. var |
> ;
bash: syntax error near unexpected token `;'
$. var 
> .
1
$

$. var ?
~/ 0/ 1  c  h
$. var ?
5
$. var *
Display all 768 possibilities? (y or n)
$. var *
768
$

SOURCING .

commandline session

$cat var
Name=Jeffrin
Age=36
$echo $Name

$. var
$echo $Name
Jeffrin
$echo $Age
36
$./var
bash: ./var: Permission denied
$chmod 744 var
$./var
$echo $Age
36
$bash
$echo $Name

$./var
$echo $Name

$. var
$echo $Name
Jeffrin
$ls  -l var
-rwxr--r-- 1 jeffrin jeffrin 20 Apr 10 00:41 var
$

some useful /proc parameters

commandline session

root>cat /proc/sys/fs/file-max
198152
root>cat /proc/sys/net/ipv4/ip_forward
0
root>cat /proc/sys/dev/cdrom/autoeject
0
root>cat /proc/sys/dev/scsi/logging_level
0
root>cat /proc/sys/kernel/hostname
debian
root>cat /proc/sys/kernel/osrelease
3.2.0-4-amd64
root>cat /proc/sys/net/core/rmem_max
131071
root>cat /proc/sys/vm/laptop_mode
0
root>cat /proc/sys/vm/swappiness
60
root>

help Get Help for Shell Builtins

commandline session

$help source
source: source filename [arguments]
    Execute commands from a file in the current shell.

    Read and execute commands from FILENAME in the current shell.  The
    entries in $PATH are used to find the directory containing FILENAME.
    If any ARGUMENTS are supplied, they become the positional parameters
    when FILENAME is executed.

    Exit Status:
    Returns the status of the last command executed in FILENAME; fails if
    FILENAME cannot be read.
$echo $?
0
$help bash
bash: help: no help topics match `bash'.  Try `help help' or `man -k bash' or `info bash'.
$

TYPE COMMAND Indicate how a command name is interpreted

commandline session

$type ls
ls is aliased to `ls --color=auto'
$type cp
cp is /bin/cp
$type mv
mv is /bin/mv
$type happy
bash: type: happy: not found
$type gnome-session
gnome-session is /usr/bin/gnome-session
$type rm
rm is /bin/rm
$type rm -r
rm is /bin/rm
bash: type: -r: not found
$type 'rm -r'
bash: type: rm -r: not found
$type "rm -r "
bash: type: rm -r : not found
$type type
type is a shell builtin
$type bash
bash is /bin/bash
$type if
if is a shell keyword
$type $
bash: type: $: not found
$type fi
fi is a shell keyword
$type ./arduino-1.0.1+dfsg/
app/       build/     core/      debian/    hardware/  libraries/ .pc/
$type ./arduino-1.0.1+dfsg/
bash: type: ./arduino-1.0.1+dfsg/: not found
$

Screen shot modified to bring to the above form

nl [ -i ] line number increment at each line

commandline session

$cat comma.pl
use strict;
use warnings;

for (qw/ 179550 45960 890458 -12345678 1000000000000/) {
  (my $n = $_) =~ s/(d+?)(?=(d{3})+b)/$1,/g;
  print "$nn";
}

$nl comma.pl
     1	use strict;
     2	use warnings;

     3	for (qw/ 179550 45960 890458 -12345678 1000000000000/) {
     4	  (my $n = $_) =~ s/(d+?)(?=(d{3})+b)/$1,/g;
     5	  print "$nn";
     6	}

$nl -i2 comma.pl
     1	use strict;
     3	use warnings;

     5	for (qw/ 179550 45960 890458 -12345678 1000000000000/) {
     7	  (my $n = $_) =~ s/(d+?)(?=(d{3})+b)/$1,/g;
     9	  print "$nn";
    11	}

$nl -i3 comma.pl
     1	use strict;
     4	use warnings;

     7	for (qw/ 179550 45960 890458 -12345678 1000000000000/) {
    10	  (my $n = $_) =~ s/(d+?)(?=(d{3})+b)/$1,/g;
    13	  print "$nn";
    16	}

$

PERL @ (at sign) and ARRAY

commandline session

$cat array.pl
#!/usr/bin/perl
use strict;
use warnings;


my @colors = ("red","green","blue");
print "@colors";
print "n";
$perl array.pl
red green blue
$vim array.pl
$cat array.pl
#!/usr/bin/perl
use strict;
use warnings;


my @colors = ("red","green","blue");
print "@colors";
print "n";
print $color[0];
print "n";

$perl array.pl
Global symbol "@color" requires explicit package name at array.pl line 9.
Execution of array.pl aborted due to compilation errors.
$vim array.pl
$cat array.pl
#!/usr/bin/perl
use strict;
use warnings;


my @colors = ("red","green","blue");
print "@colors";
print "n";
print $colors[0];
print "n";

$perl array.pl
red green blue
red
$

Perl TESTING for gcc compiler option related

commandline session

$cat testgcc.pl
#!/usr/bin/perl -w

use Test::More tests => 2 ;

my $result;

system "gcc unsigned.c";
$result = `./a.out`;
#print $result;
ok ($result lt 0);


system "gcc -funsigned-char unsigned.c";
$result = `./a.out`;
#print $result;
ok ($result gt 0);

$cat unsigned.c
#include

int main(void)
{
  char c = -10;
  printf("%d",c);
  return 0;
}
$perl testgcc.pl
1..2
ok 1
ok 2
$