You are here:
Wiki-SL
>
TWikiBar Web
>
TWikiBarTalk004
(16 Feb 2007,
JulioNeves
)
(raw view)
E
dit
A
ttach
---+!! Pub Talk Part IV<!--pula duas linhas--> %TOC% ------ 'My fellow!!! How have you been, mr. bin? Have you done the exercise I asked you to?' 'I surely did! When programming is the topic, if you don't practice, you don't learn. You've asked me a simple script that tells whether a user is logged in or not. I have made the following script:' %TERMINAL_INI% $ cat logado%OUT_INI% #!/bin/bash # searches whether a user is logged in or not if who | grep $1 then echo $1 is logged else echo $1 can't be seen anywhere around fi%OUT_FIM% %TERMINAL_FIM% 'Easy boy!! You look quite excited, but let's just ask for some beer first. John, two beers, please! And please, pour mine with no foam...' 'OK! Now that we've had our drinks, let's take a look:' %TERMINAL_INI% $ logado jneves%OUT_INI% jneves pts/0 Oct 18 12:02 (10.2.4.144) jneves is logged%OUT_FIM% %TERMINAL_FIM% 'Well, it really works! I used my login as parameter and it told me I was logged in. Nevertheless, it told me something I didn't want to: a line of the who command. In order to avoid it, it's just necessary to send that line to the black hole called /dev/null. Check it out:' %TERMINAL_INI% $ cat logado%OUT_INI% #!/bin/bash # searches whether a user is logged in or not (version 2) if who | grep $1 > /dev/null then echo $1 is logged else echo $1 can't be seen anywhere around fi%OUT_FIM% %TERMINAL_FIM% 'Now, let's test:' %TERMINAL_INI% $ logado jneves%OUT_INI% jneves is logged $ logado chico chico can't be seen anywhere around%OUT_FIM% %TERMINAL_FIM% %ATTENTION_INI% Remember the trick: most of the commands have a standard output and an error output (=grep= is one of the few exceptions because it shows no error message when it doesn't find a string) and we should pay attention when it is necessary to send them to the black hole. %ATTENTION_FIM% 'But let's change the subject. The last time we met, I was showing you some conditional commands and, when I was thirsty as hell, you asked me how one can test conditions. Let's see the test command, then.' ---++ The test Command 'Well, we are all acquainted to the use of =if= testing conditions (which are always _greater than_, _less then_, _greater or equal_, _less or equal_, _equal_ and _not equal_). When using Shell to test conditions, we can use the =test= command, but it is a lot more powerful than we can imagine. Let me first show you the main options to test files in a disc:' <center> %TABLE{ databg="#ffffff" headerrows="1" }% | *Options of the test command to files* || | *Option* | *True if:* | | =-e file= | =file= does exist | | =-s file= | =file= does exist and is bigger than zero | | =-f file= | =file= does exist and is a regular file | | =-d file= | =file= does exist and is a directory | | =-r file= | =file= does exist with reading grant | | =-w file= | =file= does exist with writing grant | | =-x file= | =file= does exist with execution grant | </center> 'Now, the main testing options for character chains:' <center> %TABLE{ databg="#ffffff" headerrows="1" }% | *Options of the test command for character strings* || | *Option* | *True if:* | | =-e string= | =string= size is zero | | =-n string= | =string= size is bigger than zero | | =string= | the string =string= is bigger than zero | | =c1 = c2= | strings =c1= & =c2= are identical | </center> 'Thinking it's over? So sorry!!! Now the part you are not acquainted to, comparisons with numbers. Check out the table below:' <center> %TABLE{ databg="#ffffff" headerrows="1" }% | *Options of the test command for numbers* || | *Option* | *True if* | | =n1 -eq n2= | =n1= equals =n2= | | =n1 -ne n2= | =n1= and =n2= are different | | =n1 -gt n2= | =n1= is greater than =n2= | | =n1 -ge n2= | =n1= is greater or equals than =n2= | | =n1 -lt n1= | =n1= is less than =n2= | | =n1 -le n2= | =n1= is less or equals than =n2= | </center> 'Furthermore, consider the following operators' <center> %TABLE{ databg="#ffffff" headerrows="1" }% | *Operators* || | *Operator* | *Purpose* | | Parenthesis =( )= | Grouping | | Exclamation =!= | Denying | | =-a= | logical _AND_ | | =-o= | logical _OR_ | </center> 'Wow! As you've seen, there is a lot of stuff here, and as I told you, our =if= is much more powerful than others's. Let's see some examples of how it works, first, testing the existence of a directory:' Example: <verbatim> if test -d lmb then cd lmb else mkdir lmb cd lmb fi </verbatim> 'That example tests if there is a =lmb= directory, or else, it creates such directory. I know you will probably question this logic saying the script is not optimized. I know, but I wanted you to understand it the way it is, so that we can use an exclamation point =(!)= to deny the test command. Check it:' <verbatim> if test ! -d lmb then mkdir lmb fi cd lmb </verbatim> 'This way, the =lmb= directory would be created if (and only if) it did not exist. This denial comes from the exclamation point =(!)= that preceeds the option =-d=. At the end of the execution of this piece of script, you would certainly be in the =lmb= directory. Let's see other two examples to check the difference between numbers and chains.' <verbatim> str1=1 str2=01 if test $str1 = $str2 then echo The variables are equal. else echo The variables are not equal. fi </verbatim> 'Running the piece of software above, the answer would be:' <verbatim> The variables are not equal. </verbatim> 'Now, let's change it in order to have a numerical comparison:' <verbatim> str1=1 str2=01 if test $str1 -eq $str2 then echo The variables are equal. else echo The variables are not equal. fi </verbatim> 'And let's run it again:' <verbatim> The variables are equal. </verbatim> 'As you have seen above, we've had two different results because the string =01= is quite different from the chain string =1=, though they are the same when considered numerically, since the number =1= is equivalent to the number =01=.' Examples: 'In order to show the use of conectors =-o= (_OR_) and =-a= (_AND_), check this example made at the prompt:' %TERMINAL_INI% $ Familia=felinae $ Genero=cat $ if test $Familia = canidea -a $Genero = lobo -o $Familia = felino -a $Genero = lion > then > echo Be aware > else > echo Can wave it > fi%OUT_INI% Can wave it%OUT_FIM% %TERMINAL_FIM% %TIP_INI% The angle brackets =(>)= in the begining of the internal lines of the =if=, are the continuation prompts (that are defined as =$PS2=) and when our friend Shell identifies that a command will have a continuation in the following line, it automatically places it until the end of the command. %TIP_FIM% 'Let's change the example to check if it still works:' %TERMINAL_INI% $ Familia=felino $ Genero=cat $ if test $Familia = felino -o $Familia = canideo -a $Genero = lion -o $Genero = lobo > then > echo be aware > else > echo Can wave it > fi%OUT_INI% Be aware%OUT_FIM% %TERMINAL_FIM% 'The operation has, obviously, generated an error. That has happened because the option =-a= preceeds the option =-o=. This way, what was firstly evaluated was the expression:' <verbatim> $Familia = canideo -a $Genero = lion </verbatim> 'That expression was evaluated as false, so that the answer was:' <verbatim> $Familia = felino -o FALSE -o $Genero = lobo </verbatim> 'Solved, it would be:' <verbatim> TRUE -o FALSE -o FALSE </verbatim> 'Since all the conectors are =-o=, the final expression has resulted as =TRUE= (considering that, when in a series of logical expressions connected by logic _OR_, only one of those expressions must be true to the result become true) and the =then= was wrongly executed. To make it work properly (again), let's try the following procedure:' %TERMINAL_INI% $ if test \($Familia = felino -o $Familia = canideo\) -a \($Genero = lion -o $Genero = lobo\) > then > echo Be aware > else > echo Can wave it > fi%OUT_INI% Can wave it%OUT_FIM% %TERMINAL_FIM% 'This way, using the parentheses, the expressions are grouped with the connector =-o=, which priorizes the execution and results in:' <verbatim> TRUE -a FALSE </verbatim> 'The result of expressions connected by the operator =-a= is true when all the expressions that it connects are true (which is not the case above). This way, the result was =FALSE= and the =else= was correctly executed.' 'If we decide to read a CD with songs of different singers, we could be temptated to use an if with a connector =-a=, but it is better to bare in mind that bash provides us many resources, and it could be done more easily with a single =grep= command, as in the following example:' %TERMINAL_INI% $ grep Musician1 musics | grep Musician2 %TERMINAL_FIM% 'Similarly, if we pick a CD with songs of an =Musician1= and of an =Musician2=, it is not necessary to use an =if= with the connector =-o=. The =egrep= (or =grep -E=, which is more proper for that situation) can also solve this problem. Check it out:' %TERMINAL_INI% $ egrep (Musician1|Musician2) musics %TERMINAL_FIM% 'Or (specifically for that case) =grep= itself could help us out:' %TERMINAL_INI% $ grep Musician[12] musics %TERMINAL_FIM% 'Above, a regular expression was used. The vertical bar =(|)= works as a logical _OR_ and the parentheses are used to limit that _OR_. On the next =grep=, on the other hand, the word =Artista= must be followed by one of the values of the list between square brackets (=[]=), that means, =1= or =2=.' 'OK! I accept it when you tell me that shell's if is much more powerful than other's. But let me tell you one thing: that syntax =if test...= is quite creepy... han?' 'Yeah, I think you're right. I don't like it either... no one does, I guess. I think that's why Shell has another syntax to substitute the =test= command.' Examples 'In order to do so, we'll use that example to switch directories. It was like that:' <verbatim> if test ! -d lmb then mkdir lmb fi cd lmb </verbatim> 'Using a new syntax, it will be:' <verbatim> if [ ! -d lmb ] then mkdir lmb fi cd lmb </verbatim> 'That means that the =test= command can be replaced by a pair of square brackets (=[]=), separated by blank spaces between the arguments, which would considerably increase the legibility of the command, since if would present a syntax similar to the one of other languages and that is why the =test= command will be used henceforth.' ---++ Please help me to finish or to correct this translation (from Portuguese) Send me a e-mail <a href="mailto:julio.neves@gmail.com?Subject=Julio I wanna help the Pub Talk translation">julio.neves@gmail.com</a> -- Main.JulioNeves - 30 Sep 2006
E
dit
|
A
ttach
|
P
rint version
|
H
istory
: r4
<
r3
<
r2
<
r1
|
B
acklinks
|
V
iew topic
|
M
ore topic actions
Topic revision: r4 - 16 Feb 2007 - 13:16:21 -
JulioNeves
TWikiBar
Página Inicial
Últimas alterações
Índice
Procurar
Estatísticas de Uso
Aviso de Atualização
Configurações Gerais
Projeto Gráfico
Mapa do Site
Quem Somos
Registre-se
?
Regras de Formatação
Biblioteca Gráfica
?
Carinhas Gráficas
Webs Wiki-SL
Amadeu
Anapolivre
ArquivoLivre
Arte
BahiaSocial
BeaBa
BibliotecaLivre
Blogs
BrasilDigital
BrasilELivre
BSM
Ccsa
CESL
CoberturaWiki
Cooperativas
Curriculo
DarvinMarosin
DiaD
Dinamicoop
Economia
EconomiaSolidaria
EducacaoLivre
Ekaaty
Emacsbr
ENSL
Fatos
Festival3
Festival4
Flisol
Fmpb
Formatos
Foswikibr
FSM2005
GNOMEBR
GTTemario2004
GTWeb
Guialivre
HDC
Incubus
InkscapeBrasil
Jogos
KdeBR
KSP
LGM
LinuxStokDoc
Livros
Main
Mentores
MHHOB
MinuanoDigital
MoradiaECidadania
OlhosDagua
Olimpo
OLPC
OOPTQ
Papers
PCLivre
PentahoBrasil
Pessoas
Portal
Prefeituras
PSLAL
PSLBA
PSLBancarios
PSLBrasil
PSLGO
PSLMA
PSLMG
PSLMIP
PSLMT
PSLMulheres
PSLPI
PubFisl10
PubFisl7
PubFisl8
PubFisl9
QuilomboDoSopapo
RadioSL
RedeMesh
RedePopular
RobotWars
Sandbox
Saudelivre
Scribus
Sementes
Shakya
SLRJ
SoftwareLivreIrece
SoftwareLivreVS
SoLiSC
SuporteLivre
System
Telecentros
TeseSA
TextoLivre
TV
TWikiBar
TWikiPtbr
UNELivre
UNIMIX
VilaTorres
WebNordeste
WTRD2004
Este Menu
?skin=free
English
Español
Português brasileiro
Copyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Wiki-SL?
Send feedback