help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Help With School Assignment, Learning Bash Scripts


From: nick
Subject: Re: [Help-bash] Help With School Assignment, Learning Bash Scripts
Date: Thu, 06 Nov 2014 06:50:46 -0500
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0

It worked. I do have one more part left for this assignment which I will ask 
about later.
Cheers Nick

On 14-11-05 10:46 PM, Eduardo A. Bustamante López wrote:
> Hey Nick :) You forgot to CC the list, I'll include a copy of your
> email:
> 
>> I asked my teacher and all he states is read the power points.
>> Furthermore I will paste all the steps below. If you have any 
>> other questions please let me known.
>> Nick 
>> 1.Start this step by just adding the correct "echo" piped into the correct
>> "grep", without redirecting the output, and make sure the script acts
>> appropriately, for example:
>>
>> add2 4 -3 12 9
>> 22
>>
>> add2 4 -3 twelve nine
>> twelve
>> nine
>> 2.Now add an "if" structure right after the "echo ... | grep ...", testing 
>> the
>> exit status of the "grep".  If the "grep" was successful, then the 
>> appropriate
>> message should be displayed, and the script terminated.  Make sure the script
>> acts appropriately, for example:
>>
>> add2 4 -3 12 9
>> 22
>>
>> add2 4 -3 twelve nine
>> twelve
>> Sorry, 'twelve' is not a number
>>
>> 3.Finally, redirect the output of the grep to "/dev/null".  Make sure the
>> script acts correctly, for example:
>>
>> add2 4 -3 12 9
>> 22
>>
>> add2 4 -3 twelve nine
>> Sorry, 'twelve' is not a number
> 
> Yeah, I know teachers like these! I'm sorry about that, and about my
> response :)
> 
> Ok, so you hve to do a script that adds numbers, and it should
> complain if these are not really numbers. Then, it asks you to use
> something that we consider "bad practice" (the, '"if" structure right
> after the ...' part, that's wrong, but, ok, we'll do it as the
> teacher says).
> 
> First step: 
> 
> address@hidden ~/t % ./step1 9 -2 4
> 11
> address@hidden ~/t % ./step1 9 -2 four
> four
> 7
> address@hidden ~/t % cat step1
> #!/bin/bash
> 
> sum=0
> for number in "$@"; do
>   echo "$number" | grep '[^0-9+-]'
> 
>   sum=$((sum + number))
> done
> 
> echo "$sum"
> 
> 
> 
> Then, teacher asks us to 'test' if grep was successful:
> 
> address@hidden ~/t % ./step2 9 -2 4
> 11
> address@hidden ~/t % ./step2 9 -2 four
> four
> Sorry, 'four' is not a number
> address@hidden ~/t % cat ./step2
> #!/bin/bash
> 
> sum=0
> for number in "$@"; do
>   echo "$number" | grep '[^0-9+-]'
>   if [ $? -eq 0 ]; then
>     echo "Sorry, '$number' is not a number"
>     exit 1
>   fi
> 
>   sum=$((sum + number))
> done
> 
> echo "$sum"
> 
> 
> 
> Then, as a last step, it asks us to 'mute' the output of grep:
> 
> address@hidden ~/t % ./step3 9 -2 4
> 11
> address@hidden ~/t % ./step3 9 -2 four
> Sorry, 'four' is not a number
> address@hidden ~/t % cat ./step3
> #!/bin/bash
> 
> sum=0
> for number in "$@"; do
>   echo "$number" | grep '[^0-9+-]' >/dev/null
>   if [ $? -eq 0 ]; then
>     echo "Sorry, '$number' is not a number"
>     exit 1
>   fi
> 
>   sum=$((sum + number))
> done
> 
> echo "$sum"
> 
> 
> 
> Now, this is what the teacher *asked for*. But, I don't like it, I
> would've done it like this:
> 
> address@hidden ~/t % ./correct 9 -2 4
> 11
> address@hidden ~/t % ./correct 9 -2 four
> Sorry, 'four' is not a number
> address@hidden ~/t % cat correct
> #!/bin/bash
> 
> shopt -s extglob
> 
> sum=0
> for number in "$@"; do
>   if [[ $number != ?([+-])+([[:digit:]]) ]]; then
>     echo "Sorry, '$number' is not a number"
>     exit 1
>   fi
> 
>   sum=$((sum + number))
> done
> 
> echo "$sum"
> 
> 
> 
> I don't want to make this explanation long, so I'll just give you
> some links you can read on why I said that:
> 
> - http://mywiki.wooledge.org/BashGuide/TestsAndConditionals#Conditional_Blocks
> - http://wiki.bash-hackers.org/syntax/ccmd/if_clause
> - http://mywiki.wooledge.org/glob#extglob 
> 
> That should explain why:
> - avoid using grep when you can do it inside the shell
> - avoid doing: command; if [ $? -eq 0 ]; then.... when you can test
>   directly: if command; then ...
> 
> So, that's it. If you have questions on that, please ask.
> 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]