Next Previous Contents

10. Stoixeia Programmatismou

10.1 Senaria floiou: kata polu anwtera twn arxeiwn .bat

An xrhsimopoiousate arxeia .BAT gia na kataskeyasete syntomeuseis polu megalwn entolwn sto DOS(egw to ekana syxna), twra sto Linux mporeite na eisagete katallhla alias (deite to parapanw paradeigma) sto profile h to .bash_profile. An omws ta arxeia .BAT sas htan pio poluploka, tote 8a trela8eite me thn glwssa senariwn (scripting language) poy sas parexei to kelyfos: einai to idio dynath oso kai h palia kalh QBasic, an oxi dynatoterh. Exei metablhtes, domes opws while, for, case, if... then... else, kai polles alles dynatothtes: einai mia kalh enallaktikh se mia «pragmatikh» glwssa programmatismou.

Gia na gracete ena senario - to antistoixo toy arxeioy .BAT sto DOS - arkei na gracete ena aplo arxeio ASCII poy na periexei tis entoles sas, na to swsete kai na to kanete ektelesimo me thn entolh chmod +x <scriptfile>. Gia na to ektelesete, gracte to onoma toy.

Mia proeidopoihsh: o basikos syntakths keimenoy toy systhmatos einai o vi, kai apo thn empeiria moy synagw oti oi perissoteroi xrhstes dyskoleuontai polu kata th xrhsh toy. Den 8a ejhghsw th xrhsh toy: diabaste to biblio toy Matt Welsh h cajte gia enan odhgo sto Diadiktyo. 8a pw omws oti:

Enas kalos syntakths keimenoy gia arxarioys einai o joe poy jekina plhktrologwntas jstar. Exete tis idies syntomeuseis plhktrwn opws stoys syntaktes toy DOSWin editor. O jed sto WordStar h se morfh IDE einai akomh kaluteros. An 8elete symboyleyteite thn enothta Pou 8a breite efarmoges gia na deite pou 8a breite aytous toys syntaktes.

H syggrafh senariwn sto bash einai ena ejairetika axanes 8ema kai xreiazetai ena biblio apo mono toy! Gi' ayto kai de 8a ypeisel8w perissotero. 8a sas dwsw omws ena paradeigma senarioy kelufoys (shell script), ek toy opoioy mporeite na ejagete merikous basikous kanones:


#!/bin/sh
# sample.sh
# I am a comment
# don't change the first line, it must be there
echo "This system is: `uname -a`" # use the output of the command
echo "My name is $0" # built-in variables
echo "You gave me the following $# parameters: "$*
echo "The first parameter is: "$1
echo -n "What's your name? " ; read your_name
echo notice the difference: "hi $your_name" # quoting with "
echo notice the difference: 'hi $your_name' # quoting with '
DIRS=0 ; FILES=0
for file in `ls .` ; do
  if [ -d ${file} ] ; then # if file is a directory
    DIRS=`expr $DIRS + 1`  # DIRS = DIRS + 1
  elif [ -f ${file} ] ; then
    FILES=`expr $FILES + 1`
  fi
  case ${file} in
    *.gif|*jpg) echo "${file}: graphic file" ;;
    *.txt|*.tex) echo "${file}: text file" ;;
    *.c|*.f|*.for) echo "${file}: source file" ;;
    *) echo "${file}: generic file" ;;
  esac
done
echo "there are ${DIRS} directories and ${FILES} files"
ls | grep "ZxY--!!!WKW"
if [ $? != 0 ] ; then # exit code of last command
  echo "ZxY--!!!WKW not found"
fi
echo "enough... type 'man bash' if you want more info."

10.2 H pio C-mantikh glwssa

Sto UNIX, glwssa toy systhmatos einai h C, eite mas aresei eite oxi. Par' ol' ayta einai dia8esimes kai alles glwsses, opws oi Java, FORTRAN, Pascal, Lisp, Basic, Perl, klp.

8ewrwntas ws dedomeno oti gnwrizete C, idou kapoies katey8ynthries grammes gia esas poy exete «xalasei» apo thn Turbo C++ h opoia allh ylopoihsh ths C sto DOS. Sto Linux, o metaglwttisths C legetai gcc kai den exei kamia apo tis fanfares poy kata kanona synodeuoyn tis DOS-adikes ylopoihseis: den yparxei IDE, boh8eia on-line, enswmatwmenh yphresia aposfalmatwshs (integrated debugger) klp. Einai aplws kai monon enas metaglwttisths gia th grammh entolwn, polu dynatos kai apotelesmatikos. Gia na metaglwttisete ton kwdika hello.c 8a plhktrologhsete:

$ gcc hello.c

to opoion 8a dhmioyrghsei ena ektelesimo arxeio onomati a.out. Gia na dwsete sto ektelesimo allo onoma, plhktrologeiste:

$ gcc -o hola hello.c

Gia na syndesete mia biblio8hkh me ena programma, pros8este to diakopth -l<onoma_biblio8hkhs>. Paradeigmatos xarin, gia na syndesete th ma8hmatikh biblio8hkh:

$ gcc -o mathprog mathprog.c -lm

(O diakopths -l<libname> anagkazei ton gcc na syndesei th biblio8hkh /usr/lib/lib<onoma_biblio8hkhs>.so· opote, o -lm syndeei to /usr/lib/libm.so).

Wraia mexri stigmhs. Otan, omws, to programma sas apoteleitai apo diafora phgaia arxeia, tote xreiazeste to programma make. As ypo8esoyme oti exete gracei enan expression parser. To phgaio arxeio toy einai to parser.c kai ayto periexei (#includes) duo arxeia header, ta parser.h kai xy.h. 8elete twra na xrhsimopoihsete tis roytines toy parser.c se ena allo programma, estw to calc.c, to opoio sth synexeia periexei (#includes) to parser.h. Mplejimo! Ti prepei na kanete gia na metaglwttisete to calc.c;

8a prepei na gracete to legomeno Makefile, to opoio deixnei ston metaglwttisth tis ejarthseis metaju twn phgaiwn arxeiwn kai twn arxeiwn object. Sto paradeigma mas:


# This is Makefile, used to compile calc.c
# Press the <TAB> key where indicated!

calc: calc.o parser.o
<TAB>gcc -o calc calc.o parser.o -lm
# calc depends on two object files: calc.o and parser.o

calc.o: calc.c parser.h
<TAB>gcc -c calc.c
# calc.o depends on two source files

parser.o:  parser.c parser.h xy.h
<TAB>gcc -c parser.c
# parser.o depends on three source files

# end of Makefile.

Swste ayto to arxeio ws Makefile kai plhktrologeiste make gia na metaglwttisete to programma sas· enallaktika, swste to ws calc.mak, plhktrologeiste make -f calc.mak, kai bebaiws RMP! Mporeite na breite kapoia boh8eia sxetika me tis synarthseis ths C poy kaluptontai apo tis selides egxeiridioy (man pages), tomeas 3· gia paradeigma,

$ man 3 printf

Gia na aposfalmatwsete ta programmata sas (debug) xrhsimopoieiste to gdb. Gia na ma8ete pws na to xrhsimopoieite: info gdb.

Yparxoyn polles dia8esimes biblio8hkes· anamesa stis prwtes poy 8a 8elhsete na xrhsimopoihsete einai oi ncurses (efe se textmode), kai svgalib (grafika konsolas). An eiste arketa gennaioi na asxolh8eite me ton programmatismo sto X11 (den einai toso duskolo), idou kapoies biblio8hkes poy dieykolunoyn th syggrafh programmatwn se X11. Deite sto http://www.xnet.com/~blatura/linapp6.html, exontes yp' och oti to Gtk arxise na ginetai to protypo sto Linux.

Polloi syntaktes keimenoy droyn ws IDE, gia paradeigma oi emacs kai jed, exoyn th dynatothta gia fwtismo suntajhs (syntax highlighting), aytomath paragrafopoihsh (indent), kai alla. Enallaktika, katebaste to paketo rhide apo ton topo ftp://sunsite.unc.edu:/pub/Linux/devel/debuggers/. Einai enas klwnos toy Borland IDE kai pisteuw 8a sas aresei.


Next Previous Contents