Linux Shell ProgrammingHere we have a free and complete book about ShellThe thirst of the "free knowledge" is welcome. |
Comandos Shell Script |
||
Home | Articles | Buy the bookChangelogs |
Pub Talk 1Pub Talk 2Pub Talk 3Pub Talk 4Pub Talk 5 |
Pub Talk - Part IV'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:' $ cat logado
#!/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
'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:' $ logado jneves
jneves pts/0 Oct 18 12:02 (10.2.4.144)
jneves is logged
'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:' $ cat logado
#!/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
'Now, let's test:' $ logado jneves
jneves is logged
$ logado chico
chico can't be seen anywhere around
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.
'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
'Now, the main testing options for character chains:'
'Thinking it's over? So sorry!!! Now the part you are not acquainted to, comparisons with numbers. Check out the table below:'
'Furthermore, consider the following operators'
'Wow! As you've seen, there is a lot of stuff here, and as I told you, our Example: if test -d lmb then cd lmb else mkdir lmb cd lmb fi
'That example tests if there is a if test ! -d lmb then mkdir lmb fi cd lmb
'This way, the Let's see other two examples to check the difference between numbers and chains.' str1=1 str2=01 if test $str1 = $str2 then echo The variables are equal. else echo The variables are not equal. fi 'Running the piece of software above, the answer would be:' The variables are not equal. 'Now, let's change it in order to have a numerical comparison:' str1=1 str2=01 if test $str1 -eq $str2 then echo The variables are equal. else echo The variables are not equal. fi 'And let's run it again:' The variables are equal.
'As you have seen above, we've had two different results because the string Examples:
'In order to show the use of conectors $ 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
Can wave it
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.
'Let's change the example to check if it still works:' $ 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
Be aware
'The operation has, obviously, generated an error. That has happened because the option $Familia = canideo -a $Genero = lion 'That expression was evaluated as false, so that the answer was:' $Familia = felino -o FALSE -o $Genero = lobo 'Solved, it would be:' TRUE -o FALSE -o FALSE
'Since all the conectors are $ if test \($Familia = felino -o $Familia = canideo\) -a \($Genero = lion -o $Genero = lobo\)
> then
> echo Be aware
> else
> echo Can wave it
> fi
Can wave it
'This way, using the parentheses, the expressions are grouped with the connector TRUE -a FALSE
'The result of expressions connected by the operator
'If we decide to read a CD with songs of different singers, we could be temptated to use an if with a connector $ grep Musician1 musics | grep Musician2
'Similarly, if we pick a CD with songs of an $ egrep (Musician1|Musician2) musics
'Or (specifically for that case) $ grep Musician[12] musics
'Above, a regular expression was used. The vertical bar
'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
'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 Examples 'In order to do so, we'll use that example to switch directories. It was like that:' if test ! -d lmb then mkdir lmb fi cd lmb 'Using a new syntax, it will be:' if [ ! -d lmb ] then mkdir lmb fi cd lmb
'That means that the Please help me to finish or to correct this translation (from Portuguese)Send me a e-mail julio.neves@gmail.com -- JulioNeves - 30 Sep 2006 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Creative Commons license - Attribution and Non-Commercial (CC) 2009 By Visitors of Júlio Neves´s Pub. All content of this page may be used under the terms of the Creative Commons License: Atribuição-UsoNãoComercial-PermanênciaDaLicença. |