Jun 11, 2011

bash script to check if a file or a folder exist

This is a cool , I always wanted to know how to check if a file exist or not ( could submited by monk on this link http://nixcraft.com/shell-scripting/93-script-how-check-if-file-exists.html )

files="/dev/MAKEDEV /etc/makedev.d /usr/sbin/mksock"
# $i will hold single file at a time in following loop
for i in $files
do
if [ ! -f $i -o ! -d $i ]; then
exit 1
fi
done


As I was writing shell code I ll add also some links to a bash scripting manual and another for reguraler exprssions , I have a think head but i have a tendency to complety forget who the regex work , you have to admit that cobol people gave us the worst heritage ever
For the shell scripting you can you can start from here : http://www.freeos.com/guides/lsst/
For the reg ex ( mainly sed and grep ) check this link : http://www.bsd.org/regexintro.html, for sed http://www.grymoire.com/Unix/Sed.html

Map and synchronize a local directory and distant shared directory

Map a drive

To Map a distant shared folder or even a local one using Dos command prompt

Open a command prompt and lauch this command

For a local directory



subst Q: "C:\Documents and Settings\hb\"



For a shared directory on a different machine



subst z: "\\Shared Server\hb\My Documents"


Synchronize two folder

To do sync to folder , we will you a great command, that microsoft incorporated as an external command and it's the "xcopy" , I am pretty sure that microsoft bought the company that developped this tool ; as they did with the excel .
For more info on xcopy or the list of external command visit this site that i recommand to visit from time to time http://www.computerhope.com

now back to how to sync the folder ; you create a batch file let's call it sync.bat
and add those two line and xcopy will do every thing for you


xcopy "c:\localfolder" "q:\remotefolder" /S /Y /D
xcopy "q:\remotefolder""c:\localfolder" /S /Y /D



Run the batch file and tadah its a miracle ; the data of the two folder will be merged and they and folder will become an image for one another.

Scheduele automatic sync

Instead of manually running the sync.bat , we can ask the scheduler to execute it every day by using the at command for more information on the at command and some tweaks check this site : http://ss64.com/nt/at.html


at 00:10 /every:M,T,S sync.bat


but due to some user previleges problem as the local system runinng the scheduele don't have the rights to access a remote directory
use the graphical interface to add a task on the win scheduler

Jun 10, 2011

A clean MakeFile for a C++ project using ILOG Cplex + Concert Library

Most of people that want to create IBM - ILOG Cplex project using C++ on linux, they take a file in the example directory of Cplex and copy paste it in the same example directory and then add a new entry in the Makefile as they are adding a new example.


But if you what to create a clean project an a separate directory you have to create your own Make File here is an example Make file that I did , it will automatically scan the (Source file directory )

Here is the architecture of the normal C++ in my sense ( it would be better to have header files in a separate directory, but I started the project using Visual studio on windows)


Project Directory
|__ MakeFile
|__Exe
|__src (dir)
| |__cpp(dir)
| |__ CPP Files + H Files
|__obj(dir)
|__data(dir)
|__ *.dat ; *.xml ;*.mps Files ( The instances of the problem that you want to test)



Of course you can add separate include directory but I put my Header files with the sources (sorry again for that it’s partially the VS fault ) but in the Makefile its already build to have a separate header Files .


You need to change the EXDIR and point it to where you want build the executables and run them.

If you are using 64 bit machine or itanium you can change the SYSTEM for x86_sles10_4.1, you can find this value in the make file in the cplex example direcory.

And also you need to change the ILOGSTUDIODIR to where you have installed ILOG.

NB: Dont forget to change the envirnment variable and add the path for the ILM.

You should update your ./bash_rc file and and something similare to : (watch out for the architecture 32 or 64 and change the library path accordinly
export ILOG_HOME=/home/balbaki/ILOG/CPLEX_Studio_AcademicResearch122

export LD_LIBRARY_PATH=$ILOG_HOME/cplex/bin/x86-64_sles10_4.1/:$LD_LIBRARY_PATH

export ILOG_LICENSE_FILE=/home/balbaki/ILOG/ILM/access.ilm


This make file was build for the IBM -ILOG Cplex v 12.2 but i think it should work on the previous versions as well

#===========================MakeFile==============================
# For Cplex + Concert Projects
# by Hassan Baalbaki www.hassanbaalbaki.com
#================================================================


SYSTEM = x86_sles10_4.1
LIBFORMAT = static_pic

EXDIR = /home/hb/dev/GlobalPlanner/MIP

#------------------------------------------------------------
#
# When you adapt this makefile to compile your CPLEX programs
# please copy this makefile and set CPLEXDIR and CONCERTDIR to
# the directories where CPLEX and CONCERT are installed.
#
#------------------------------------------------------------
ILOGSTUDIODIR =/home/hb/ILOG/CPLEX_Studio_AcademicResearch122
CPLEXDIR = $(ILOGSTUDIODIR)/cplex
CONCERTDIR = $(ILOGSTUDIODIR)/concert




# ---------------------------------------------------------------------
# Compiler selection
# ---------------------------------------------------------------------

CCC = g++
CC = gcc
JAVAC = javac

# ---------------------------------------------------------------------
# Compiler options
# ---------------------------------------------------------------------

CCOPT = -m32 -O -fPIC -fexceptions -DNDEBUG -DIL_STD


# ---------------------------------------------------------------------
# Link options and libraries
# ---------------------------------------------------------------------

CPLEXBINDIR = $(CPLEXDIR)/bin/$(BINDIST)
CPLEXLIBDIR = $(CPLEXDIR)/lib/$(SYSTEM)/$(LIBFORMAT)
CONCERTLIBDIR = $(CONCERTDIR)/lib/$(SYSTEM)/$(LIBFORMAT)

CCLNFLAGS = -L$(CPLEXLIBDIR) -lilocplex -lcplex -L$(CONCERTLIBDIR) -lconcert -m32 -lm -pthread



all:${DIRS}
make all_cpp

execute: all
make execute_cpp


CONCERTINCDIR = $(CONCERTDIR)/include
CPLEXINCDIR = $(CPLEXDIR)/include

#EXINC = $(EXDIR)/include
EXINC = $(EXDIR)/src/cpp
EXDATA = $(EXDIR)/data
EXSRCCPP = $(EXDIR)/src/cpp


CFLAGS = $(COPT) -I$(CPLEXINCDIR)
CCFLAGS = $(CCOPT) -I$(CPLEXINCDIR) -I$(CONCERTINCDIR)


#------------------------------------------------------------
#
#Build and Execute directory
#
#------------------------------------------------------------



INC_PATH = $(EXSRCCPP)
SRC_PATH = $(EXSRCCPP)
OBJ_PATH = ${EXDIR}/obj



SOURCES = ${wildcard ${SRC_PATH}/*.cpp}
OBJECTS = ${patsubst ${SRC_PATH}/%.cpp, ${OBJ_PATH}/%.o, ${SOURCES}}


INCLUDES = -I${INC_PATH}

DIRS = ${OBJ_PATH}


#------------------------------------------------------------
# make all : to compile all.
# make execute : to compile and execute some instances
#------------------------------------------------------------



CPP_EX = globalplanner




all_cpp: $(CPP_EX)


execute_cpp: $(CPP_EX)
./globalplanner $(EXDATA)/small.dat 3




# ------------------------------------------------------------

clean :
/bin/rm -rf *.o *~ $(OBJ_PATH)/*.o
/bin/rm -rf ${SRC_PATH}/*~
/bin/rm -rf $(CPP_EX)
/bin/rm -rf *.mps *.ord *.sos *.lp *.sav *.net *.msg *.log *.clp

# ------------------------------------------------------------
#
# The Cpp and *.o Files
#


all: ${DIRS} globalplanner

${OBJ_PATH}:
mkdir -p $@


${OBJ_PATH}/%.o : ${SRC_PATH}/%.cpp
$(CCC) ${INCLUDES} -c $(CCFLAGS) $< -o $@

globalplanner: ${OBJECTS}
$(CCC) $(CCFLAGS) $^ -o globalplanner $(CCLNFLAGS)


# Local Variables:
# mode: makefile
# End:


#======================== END OF MakeFile =========================

The easiest way to run an executable that take as entry files that are on another directory



This tutorial is for shell - unfamiliar people; it unable them to run; on the command prompt a program that take as an input a file that is on different directory. (If you know how to use cd .. ) this tutorial isn't for you. But it could accelerate the process if you don't want to type a long command each type if you have multiple sub directory between the executable and data file.




Let’s say we have this architecture for a program called hello and it takes as input the data1.dat that is in a sub directory



C:\>
|__hello (dir)
|__hello.exe
|__data(dir)
|__data1.dat

First you create in you data folder a shortcut to the executable:
1- Go to data Folder and right click
2- Choose to new -> Shortcut

3- Browse to the location of your executable (hello world)


4- Finish the process
5- Right click on the newly create link and go to properties
6- Empty the field Run In. (it is c:\hello\) delete it.

Create a cmd shortcut
One last thing create also cmd shortcut in the data directory if you don't want to type cd each time

1- Go to data Folder and right click
2- Choose to new -> Shortcut


3- Type cmd in the path and then click on next then finish




4- Also you should Right click the link and empy the field Run




To run the program :

Click on the (cmd) link that you created and the command prompt should pop up , Type the name of the link to you executable then the name of your data input file .
In this case it should be
C:\hello\data> hello.exe.lnk data1.dat
NB : I know that in this case we can also time ..\hello.exe data1.dat but this example was written to help the people that are unfamiliar with shell and the data is in sub -sub-sub directory

May 25, 2011

How to install Netbeans IDE , with c++ compilors for the SOC-RAD project

Installation On windows

Before installing the IDE - ( Net beans ) , and the project itself , you should

  1. Install a c++ compiler and debugger and make
  2. Check if java JRE is installed on your machine and installed it otherwise.
  3. Install Netbeans from www.netbeans.org

Or 2’- Install the bundle netbeans + jdk from oracle site : http://www.oracle.com/technetwork/java/javase/downloads/jdk-netbeans-jsp-142931.html

4-Download the source files and create a project.


Part I : Install the gcc and gdb and make:

First I recommend MinGW instead of Cgwin .

**To check if it’s installed

  1. type cmd in the Run command
  2. In the Command prompt opened type the following command “g++- v “ if you don’t get an error that means it’s installed you don’t need to install the c++ compiler
  3. In the Command prompt opened type the following command “make –v” if you don’t get an error that means it’s installed you don’t need to install the MSYS.

If you want to install it the hard way / manually follow this link : http://www.mingw.org/wiki/InstallationHOWTOforMinGW

If not do the simple way and use the installer :

http://sourceforge.net/projects/mingw/files/Automated%20MinGW%20Installer/mingw-get-inst/

1. Download the latest version

2. Choose the default settings till you arrive the check list

3. You should check from the chck list C++ compiler ( g++ ) and ) and the developer kit that includes MSYS ( make)

4. Update : the Path

4.1. Right-click on "My Computer" and select "Properties".

4.2. Click Advanced -> Environment Variables. (AvancĂ© -> variables d’environnement)

4.3. In the box entitled "System Variables" scroll down to the line that says "PATH" and double-click the entry.

4.4. You will be presented with a dialog box with two text boxes, the bottom text box allows you to edit the PATH variable. It is very important that you do not delete the existing values in the PATH string, this will cause all sorts of problems for you!

4.5. Scroll to the end of the string and at the end add ;C:\MinGW\bin;C:\MinGW\MSYS\1.0\bin; (if you kept to the recommended default installation directory). Don't forget the semicolon; this separates the entries in the PATH.

4.6. press OK -> OK -> OK and you are done.

5. After the installation open and new cmd and check if g++ and make are installed ( see above the ** section)

Part 2 : JDK / JRE (Check the option part 2’)

First JRE , stands for java run time environment , and if you are using the new terminology of oraclem it’s called “ java 6 “

To check if it’s installed

  1. type cmd in the Run command
  2. In the Command prompt opened type java- version

You should get the version of your java runtime,

if not go to ( http://www.java.com/en/download/help/index.xml or http://www.java.com/en/download/manual.jsp#win :) and install it ( I recommend also to install the JDK / SDK ( stands for java development kit : ) , if you are planning to develop java programs , you ll only loose some disk storage space.-

Part 3: Install Netbeans

Download the netbeans from the following link

http://netbeans.org/downloads/index.html

Of course choose either the full version or the C++ version.

Part 2’ : Install the the bundles JDK+netbeans :

Oracle now, on their site they offer a bundle of jdk + netbeans ( but in this version usually you should install the C++ plugings )

  1. Go to the following link and download the bundle at : http://www.oracle.com/technetwork/java/javase/downloads/jdk-netbeans-jsp-142931.html
  2. Once installed , got the tools menu and then go to plugins
  3. If C/C++ does not figure in the installed tab , go the available plugins tab and check the C/C++ plugins and then install .

Part 4: Create and install the project

Jan 5, 2007

How to format compaq / hp laptop with a recovery partition.

I bought a new compaq laptop presario V6000 its really wonderful you should see the display
it's the clearest and the brightest screen i ever seen.BUT
it got a french windows media center with a azerty keyboard. Ok i can't do anything for the key board but i should be able to format the laptop after all
But now you can't coz ( hp . compaq ) added a Recorvery partition for fast recorvery
and this partition will not let you format the hard disk , in fact when boot up from the windows bootable cd the installation will freeze and tell you that their not disk on the system!!!

and i could find on the internet how to get rid of the partition so i send an email to the french
customer departement of HP and didn't get anything from them.I called the nb that came with the laptop of the french technical support to find out that they deleted this option
and they if went to the US hp site and god bless america they have online technical support
and ge have me the answer on how to remove the recovery partition.

Click Start, click All Programs, select System Recovery, and click PC Recovery. When the recovery options are presented, select the option to perform the PC Recovery and click Next. The PC will restart.When the Welcome to PC Recovery message displays, click OK to continue. On the System Recovery panel, click Advanced Options. On the Advanced Options panel, select the option to Delete Recovery Partition, and click Next

http://h10025.www1.hp.com/ewfrf/wc/genericDocument?docname=c00608578&cc=us&dlc=en&lc=en&jumpid=reg_R1002_USEN#N1528

I will do it and tell you the result.Whish me luck