Posts

Showing posts from November, 2014

Redirect both stdout and stderr to dev null using > /dev/null 2>&1 for linux commands and scripts

/ tmp ( ) $ cat out_err_redirect_test . sh echo message to stdout echo message to stderr > & 2 / tmp ( ) $ bash out_err_redirect_test . sh message to stdout message to stderr # Message to stdout is redirected to dev null / tmp ( ) $ bash out_err_redirect_test . sh > / dev / null message to stderr # Message to stderr is redirected to dev null / tmp ( ) $ bash out_err_redirect_test . sh 2 > / dev / null message to stdout # Message to stderr and stdout is redirected to dev null / tmp ( ) $ bash out_err_redirect_test . sh > / dev / null 2 > / dev / null # Messaged to stdout is redirected to dev null , and stderr to configured stdout ( implies dev null ) / tmp ( ) $ bash out_err_redirect_test . sh > / dev / null 2 > & 1 # Message to stderr is redirected to stdout ( screen ) and stdout to dev null / tmp ( ) $ bash out_err_redirect_test . sh 2 > & 1 > / dev / null message to stderr

Tuples are expanded as multiple arguments when passed to print statement after %

Below the tuple (1, 2, 3) is getting expanded as 3 arguments and the format representation is expecting to have 3 %s or %r to be printed, > > > print " %r " % ( 1 , 2 , 3 ) Traceback ( most recent call last ) : File " <stdin> " , line 1 , in < module > TypeError: not all arguments converted during string formatting > > > print " %s " % ( 1 , 2 , 3 ) Traceback ( most recent call last ) : File " <stdin> " , line 1 , in < module > TypeError: not all arguments converted during string formatting > > > print " %s " % str ( ( 1 , 2 , 3 ) ) ( 1 , 2 , 3 )

Loop over an array with index and element in the array

Perl looping of an array with index as well as the item stored in variables, /tmp$ cat loop_with_index.pl  my @arr=(1,2,3,4); while (my ($i, $x) = each(@arr)) {     print "$i: $x\n"; } /tmp$ perl loop_with_index.pl  0: 1 1: 2 2: 3 3: 4