Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts

Tuesday, August 18, 2015

Mac OS X ipython notebook install

IPython is extended development environment for python with more usable shell, architecture for parallel and multiuser computing, and web-based environment (Ipython notebooks) for modern interactive web-based shell.

Unfortunately, python related staff on Mac requires more effort than on Linux.

Way to install Ipython Notebooks on mac is there:

1) Download anaconda package manager from there:
http://continuum.io/downloads

you need:

Mac OS X — 64-bit
Python 2.7
Graphical Installer 

2) Install anaconda by double click on downloaded package and following instructions in installer,

3) Start new bash shell (Mac OS X Terminal)

4) in terminal (confirm where required):
# for  updating conda
conda update conda
# for creating ipython environment
conda create --name ipython_learn python=2.7 ipython ipyhon-notebook
# list environments, here should be ipython_learn

conda env list 
# for activating ipython_learn environment
source activate ipython_learn
# for starting ipython notebooks environment
ipython notebook

Ok, there your ipython environment started.
Upload or just use your notebooks there.




Saturday, May 16, 2009

chmod list of dirs (or files)

I have list of dirs (generated by some find statement) that we want to grant rights to execute (go inside dir).

the problem with

# cat list | xargs chmod 
intuitive one-line script is that it is not working.

Directories names in list contain spaces and ' " symbols and we have errors in directories processing doing that way.

Added slashes to get working script to grant rights to the files:

# cat list | sed 's/[\ '"'"'"]/\\&/g' | xargs chmod a+x

Wednesday, May 21, 2008

Move local CVS repository to RCS

The following is the way to convert existing CVS repository to RCS.
Actually no conversion required;
It works for me; no warranty that it would work for somebody else;


# from /home/rtg/
mkdir for_rcs
cd for_rcs
# CVS_REPO/cvs:/ - is repository created with cervesia
cp -r /home/rtg/CVS_REPO/cvs\:/* .
find ./ -name CVS | xargs rm -rf
for i in `find ./ -type d` ; do cd $i ; mkdir RCS ; mv *,v RCS/ ; cd /home/rtg/for_rcs/ ; done

Ok, at this point I have RCS repository in for_rcs folder.
:)

Monday, May 19, 2008

rcs + rsync for keeping your files on several machines

I have two GNU/Linux laptops (Xubuntu) and one GNU/Linux desktop (CentOS).

Need to synchronize them to keep the integrity of the information on the machines.

Tried git, but found it too strong too keep my little docs and code snippets.
That way decided to use well known and stable GNU tools rcs and rsync.

man rcs 
man rsync
are prerequisites ;

Why rcs? Because repository could be synced with code/docs, and it is GNU.
Why rsync? It is GNU solution for syncing files.

Two basic session snippets for usage:

init files for directory:

cd fldr_for_sync
mkdir RCS
# --
rcs -i file # add new file under rcs
# -- it is the same with --
touch another_file; ci anoter_file


basic rsync dir with subdirs; it is not overwrites old files in dest dir;
permissions and links kept;

rsync -avz path_to_fldr_src path_to_dest


I found that rcs + sync can substitute git for such task and even make the task more clear and robust, due to the long history and high stability of the rcs and rsync tools.

Saturday, March 15, 2008

Logic operators: Lazyness C++, Perl, Java, Javascript, ksh

Examples demonstrating lazy evaluation for || and && in different languages presented.

Consider following c++ code:


Fun1() || Fun2() || Fun3();
Fun1() && Fun2() && Fun3();


In c++, they are executed, only if the result of overall execution depends from execution of function that was not yet executed. '&&' , '||' and ',' are only c++ operators, thats order is direct from left-to right.

In examples:

Java1.6:
/**
* @author rtg
*
*/
public final class main {
public static boolean fun1(){System.out.println("fun1"); return true;}
public static boolean fun2(){System.out.println("fun2"); return false;}
public static boolean fun3(){System.out.println("fun3"); return true;}
/**
* @param args
*/
public static void main (String[] args) {
boolean res;
System.out.println("|| test:");
res = fun1() || fun2() || fun3();
System.out.printf("res:%b\n",res);
System.out.println("&& test:");
res = fun1() && fun2() && fun3();
System.out.printf("res:%b",res);
}
}


Results are the same as for

Javascript in firefox 2 is lazy too:
example

Perl's operators are lazy too:

$ cat lazy_or.pl
#!/usr/bin/perl

sub fun1 {print "fun1\n"; return 1;};
sub fun2 {print "fun2\n"; return 0;};
sub fun3 {print "fun3\n"; return 1;};

sub main
{
print "or:\n";
print "res=" . (fun1() or fun2() or fun3()) . "\n";
print "and:\n";
print "res=" . (fun1() and fun2() and fun3()) . "\n";
print "&&:\n";
print "res=" . (fun1() && fun2() && fun3()) . "\n";
print "||:\n";
print "res=" . (fun1() || fun2() || fun3()) . "\n";
}

main();

rtg@kubic-roman:~$ perl lazy_or.pl
or:
fun1
res=1
and:
fun1
fun2
res=0
&&:
fun1
fun2
res=0
||:
fun1
res=1
rtg@kubic-roman:~$ perl -v

This is perl, v5.8.8 built for i486-linux-gnu-thread-multi


ksh example at the end is different in results from perl c++ and javascript.
After ksh testing I understand, why Java not accepts int's as booleans for the operator.

ksh interprets 0 as true and 1 as false, so it is the place were you have to be patient,
when swith to:

$ cat lazy.ksh
#!/usr/bin/ksh

fun1() {
echo "fun1"
return 1;
}

fun2() {
echo "fun2"
return 0;
}

fun3() {
echo "fun3"
return 1;
}

status(){
echo $?
}

fun1 || fun2 || fun3
status
fun1 && fun2 && fun3
status
rtg@kubic-roman:~$ ./lazy.ksh
fun1
fun2
0
fun1
1


Thats strange, that a lot of programmers in presented languages where very confident about non-laziness of the operators.
However, it is common for all presented languages, and it is no conceptual difference in these languages at the point described.

C++ example:
#include 

int fun1(){std::cout << "fun1\n"; return 1;}
int fun2(){std::cout << "fun2\n"; return 0;}
int fun3(){std::cout << "fun3\n"; return 1;}

int main()
{
int test_val;
std::cout << "\n || test:\n";
test_val = fun1() || fun2() || fun3();
std::cout << "result is:" << test_val << std::endl;
std::cout << "\n && test:\n";
test_val = fun1() && fun2() && fun3();
std::cout << "result is:" << test_val << std::endl;
}
result of code execution is:

|| test:
fun1
result is:1

&& test:
fun1
fun2
result is:0

Thursday, March 13, 2008

GNU/Linux battery check state

To conclude from forums:


$ sudo acpi -V
Battery 1: charging, 98%, 00:07:11 until charged
Thermal 1: active[3], 50.0 degrees C
Thermal 2: ok, 46.0 degrees C
Thermal 3: ok, 42.0 degrees C
Thermal 4: ok, 34.0 degrees C
Thermal 5: ok, 50.0 degrees C
AC Adapter 1: on-line
$ cat /proc/acpi/battery/[BatteryID]/state
present: yes
capacity state: ok
charging state: charging
present rate: 626 mA
remaining capacity: 4299 mAh
present voltage: 12531 mV
$ cat /proc/acpi/battery/[BatteryID]/info
present: yes
design capacity: 4374 mAh
last full capacity: 4374 mAh
battery technology: rechargeable
design voltage: 10800 mV
design capacity warning: 219 mAh
design capacity low: 44 mAh
capacity granularity 1: 100 mAh
capacity granularity 2: 100 mAh
model number: Primary
serial number: 03743 2007/08/23
battery type: LIon
OEM info: Hewlett-Packard

Saturday, February 23, 2008

grep -A -B

This options of grep used for looking through lines surrounding text that is searched.

-A< lines After >
-B< lines Before >

Coloured example:


$ grep -A2 -B1 deflate algorithm.txt

1. Compression algorithm (deflate)

The deflation algorithm used by gzip (also zip and zlib) is a variation of

--
size (except that the compressed data for one block must fit in

available memory). A block is terminated when deflate() determines that
it would be useful to start another block with fresh trees. (This is
somewhat similar to the behavior of LZW-based _compress_.)

--
truncated at a certain length, determined by a runtime option (level
parameter of deflateInit). So deflate() does not always find the longest
possible match but generally finds a match which is long enough.

deflate() also defers the selection of matches with a lazy evaluation
mechanism. After a match of length N has been found, deflate() searches for
a longer match at the next input byte. If a longer match is found, the
previous match is truncated to a length of one (thus producing a single

--
The lazy match evaluation is also subject to a runtime parameter. If
the current match is long enough, deflate() reduces the search for a longer
match, thus speeding up the whole process. If compression ratio is more
important than speed, deflate() attempts a complete second search even if
the first match is already long enough.

--
more time filling in duplicate symbol entries than you do actually decoding.
At least for deflate's output that generates new trees every several 10's of
kbytes. You can imagine that filling in a 2^15 entry table for a 15-bit code
would take too long if you're only decoding several thousand symbols. At the

Thursday, February 21, 2008

Compare strings and numbers in shell

Comparison operators and syntaxes of comparisons are different for different languages. For example 'eq' keyword in perl used in comparisons of strings, and in shell '-eq' used for comparisons of numbers.

The more interesting thing, that even for bash ( most often used as interactive shell) and ksh (used for scripts), results of comparisons would be different for absolutly the same expression.

Example is:


$ ksh
$ set -o vi #for more convinient command-line editing
$ if [ "b " -eq " b" ] ; then echo "hurray" ; else echo "dfdfd" ; fi
hurray
$ bash
rtg@kubic-roman:~$ if [ "b " -eq " b" ] ; then echo "hurray" ;
else echo "dfdfd" ; fi
bash: [: b numerical expression expected
dfdfd


Incorrect comparison used; compared strings as numbers. Got two different tests results for .ksh and bash, and bash reported problem, but .ksh not.

Now fix expression, and use "=" operator as specified.

~$ ksh
$ set -o vi
$ if [ "b " = " b" ] ; then echo "equal" ; else echo "non equal" ; fi
non equal
$ bash
rtg@kubic-roman:~$ if [ "b " = " b" ] ; then echo "equal" ; else echo "non equal" ; fi
non equal


It is correct, and same for both expressions.

What will be, if numerical comparison used for string containing numbers?
It is more simple to display it in script:

$ ksh
$ if [ "7" -eq ' 7 ' ] ; then echo "equal" ; else echo "non equal" ; fi
equal
$ if [ "7" == ' 7 ' ] ; then echo "equal" ; else echo "non equal" ; fi
non equal

the same results are in the bash.

Consclusion:


If move from another language, especially statically typed like C:

- be patient with scripts (especially comparisons)

- if a script does not report any error, it still can be buggy; C program probably won't even compile for cases processed by shell script with hidden error.

- If some command in script failed, and even produced error message, script continues execution if else not specified in the script. Some of the errors of this type could be avoided by always checking result of command execution after each command in script.

- Test cases are needed, especially when migrate/upgrade to new toolset/system, even if application code is unchanged.

Why this post have been written:


Switching from one programming language to the another is popular.
It is because modern systems are heterogeneous.

The problem: often the same entity in contest of one programming language, present something complete different in contest of another language.

It is simple to use only one language. You can be sure in such a case, that when you are coding automatically, you are using exact syntax from the language used, but not from some another language.

It is complex, when use a lot of languages.

When switching from one language to another, looking through short reference card of language you switched from, and that of language you switched in, can be useful. It can help to place yourself in right contest, and get rid of previous contest.

Perl and Shell scripts, that are often similar in construction, have different variable declaration, but the same definition (shell does not use $ when defining variable), tests are different, function definitions i.t.c.