Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Possible extra data in script?

  1. #1
    Join Date
    Dec 2012
    Beans
    50

    Question Possible extra data in script?

    Just wanted to verify something. I know this is really basic to most of you. I just don't want to get too far ahead of myself
    I highlighted two parts of concern in red. (This script was taken right from my book and it works).

    Do I actually need to specify a value for firstname and lastname variable when using a read statement? I noticed that I'm able to leave the values blank and it still works. I guess what I'm asking is: why is the book telling me to put a name in (john smith) if the user interacting with the script is changing the value of the variable with a read statement by what he/she types anyway?

    And notice the echo after the second read lastname. Why is the book telling me to add that? I removed it and the script still worked as expected. Could these be typos in the book?
    The book by the way is: Essential Linux Administration - Chuck Easttom and Serge Palladino

    #!/bin/bash

    lastname=
    smith
    firstname=
    john
    echo Please type in your first name
    read firstname
    echo Please type in your last name
    read lastname
    echo (<--why do I need this?)
    echo Hello $firstname $lastname it is good to meet you

    exit 0
    Last edited by starstreams; December 27th, 2012 at 04:19 AM.

  2. #2
    Join Date
    May 2006
    Location
    Boston
    Beans
    1,909
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Possible extra data in script?

    Quote Originally Posted by starstreams View Post
    Just wanted to verify something. I know this is really basic to most of you. I just don't want to get too far ahead of myself
    I highlighted two parts of concern in red. (This script was taken right from my book and it works).

    Do I actually need to specify a variable for firstname and lastname when using a read statement? I noticed that I'm able to leave the variable blank and it still works. I guess what I'm asking is: why is the book telling me to put a name in (john smith) if the user interacting with the script is changing the variable by what he/she types anyway?

    And notice the echo after the second read lastname. Why is the book telling me to add that? I removed it and the script still worked as expected. Could these be typos in the book?
    The book by the way is: Essential Linux Administration - Chuck Easttom and Serge Palladino
    i see a lot of useless code in it. Also the echo statment should have the "" ! are you sure it' s no t an exrcise. I haven't find that many revies about the book. That's not a good sign!

    i suggest you this http://shop.oreilly.com/product/9780596005283.do

    blog
    Donations BTC : 12FwoB7uAM5FnweykpR1AEEDVFaTLTYFkS
    DOUBLEPLUSGOOD!!

  3. #3
    Join Date
    Apr 2012
    Beans
    5,862

    Re: Possible extra data in script?

    The extra 'echo' just outputs a newline I think - to format the output a bit more nicely.

    The lastname=smith and firstname=john assignments just supply default values - if you wanted to be fancier you could use a construct like ${firstname:-john} in the 'echo Hello ... ' line instead

    Code:
    echo Hello ${firstname:-john} ${lastname:-smith} it is good to meet you
    See http://tldp.org/LDP/abs/html/paramet...stitution.html

    EDIT: pretty much agree with fdrake though - although there's nothing actually 'wrong' with it, the style is pretty basic - there are nicer ways of prompting for user input (at least in bash 4+) e.g. something like

    Code:
    #!/bin/bash
    
    read -p "Please type in your first name: " firstname
    
    read -p "Please type in your last name: " lastname
    
    echo "Hello ${firstname:-john} ${lastname:-smith} it is good to meet you"
    
    exit 0
    Last edited by steeldriver; December 27th, 2012 at 03:44 AM.

  4. #4
    Join Date
    Apr 2012
    Beans
    Hidden!

    Re: Possible extra data in script?

    Hi, it looks like this simple script is teaching an example
    of how to change a defined field or var. In this excersize,
    Lastname is defined as smith and first name as john.
    The read statement changes the $firstname and $lastname
    to the new input. The blank echo is just that...a blank line for
    easier reading of the script and a newline on the output.
    so...yes....if you enter nothing for firstname and lastname it
    will forever be john smith....hence. Its called learning about
    defined variables,and how to change them.It is good that you question, but...it is not
    a type O.
    Craving anchovy, herring and squid pizza.....with clam sauce

  5. #5
    Join Date
    Dec 2012
    Beans
    50

    Re: Possible extra data in script?

    @fdrake: I'll order that book right now if you say it's good.
    The book I mentioned only has a small section of scripting, though I have learned a lot of different topics from it in the few chapters I've read It's sometimes vague on somethings.

    I'm not sure what you mean by "" !
    This was an an exercise but all the other exercise examples don't use "" !
    I thought you only needed to add double machine quotes between values with spaces? I'm not sure, what do you do with this --> "" !

    @steeldriver:

    what you said about the output statement outputting a new like makes sense. Sort of like how a P tag can be used to add a new line in HTML rather then just starting a new paragraph. I'll have to read up on constructs. But why would you add something that is longer to type such as ${firstname:-john} when adding $firstname makes a call to the variable using less characters. I don't understand the construct I think.


    @Hadaka
    Hi, it looks like this simple script is teaching an example
    of how to change a defined field or var. In this excersize,
    Lastname is defined as smith and first name as john.
    The read statement changes the $firstname and $lastname
    to the new input.


    EDIT:
    Ok, this makes sense now.
    Yes you are correct, the book was demonstrating how to change a defined field var using the read statement.
    That is correct.
    And what you explained makes sense now, Thanks for verifying. I just wasn't sure why they added the extra echo.
    And I guess they added the names for values just to show that the read statement would override them anyway. They were using it to make a point I guess.

    Thank you
    Ordering the LPI Linux Certification in a Nutshell, 2nd Edition book right NOW...
    Last edited by starstreams; December 27th, 2012 at 04:04 AM.

  6. #6
    Join Date
    May 2006
    Location
    Boston
    Beans
    1,909
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Possible extra data in script?

    Quote Originally Posted by Hadaka View Post
    .
    so...yes....if you enter nothing for firstname and lastname it
    will forever be john smith....
    are you sure. If you just press ente you will get an empty string> You have to escape in order not to assaign the value

    blog
    Donations BTC : 12FwoB7uAM5FnweykpR1AEEDVFaTLTYFkS
    DOUBLEPLUSGOOD!!

  7. #7
    Join Date
    Apr 2012
    Beans
    Hidden!

    Re: Possible extra data in script?

    @fdrake...you are correct...you get a blank field....my error
    thanks for pointing that out..
    Craving anchovy, herring and squid pizza.....with clam sauce

  8. #8
    Join Date
    Dec 2012
    Beans
    50

    Re: Possible extra data in script?

    By the way, what would happen if you left the exit 0 out? hehe
    I was afraid to try it last night .. for my computer might go running off on me in an endless loop to never return.
    On a side note, it looks as though you have to terminate the if statements also with fi, sometimes after the exit 0 which seems kind of odd, and un nested. Some of this stuff reminds me so much of HTML, XHTML.. CSS.. ect.

    Quote Originally Posted by fdrake View Post
    .. If you just press enter you will get an empty string> You have to escape in order not to assign the value
    For me, as long as there is a read statement with user input, I get what ever value the user (myself) enters on the keyboard. Or as you mentioned, if the user doesn't enter anything, it's just blank. Just confirming I understand. But why would you escape if leaving the value blank was intended? Unless maybe there are other complex situations like databases where a value can't be left blank possibly?
    Last edited by starstreams; December 27th, 2012 at 04:17 AM.

  9. #9
    Join Date
    May 2006
    Location
    Boston
    Beans
    1,909
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Possible extra data in script?

    Quote Originally Posted by starstreams View Post
    [B]@fdrake:

    I'm not sure what you mean by [B]"" !
    It's a good use to use "" when you print a message . it's like using brackets with operations .(it's a coding style that most books push you to follow) It' s easier to read and if you use a good editor it will use a different colour, so oit makes it more intuitive to.

    blog
    Donations BTC : 12FwoB7uAM5FnweykpR1AEEDVFaTLTYFkS
    DOUBLEPLUSGOOD!!

  10. #10
    Join Date
    May 2006
    Location
    Boston
    Beans
    1,909
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Possible extra data in script?

    Quote Originally Posted by starstreams View Post
    By the way, what would happen if you left the exit 0 out? hehe
    I was afraid to try it last night .. for my computer might go running off on me in an endless loop to never return.
    On a side note, it looks as though you have to terminate the if statements also with fi, sometimes after the exit 0 which seems kind of odd, and un nested. Some of this stuff reminds me so much of HTML, XHTML.. CSS.. ect.
    you can leave it out. but it is good use to get used writing it, especcially when you will have to use conditions for error megs, or warnings.

    blog
    Donations BTC : 12FwoB7uAM5FnweykpR1AEEDVFaTLTYFkS
    DOUBLEPLUSGOOD!!

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •