Find the answer to your Linux question:
Results 1 to 8 of 8
I'm writing a bash script that uses a string variable. I need to somehow truncate it so that only the first character remains, but I don't know how to do ...
  1. #1
    Just Joined!
    Join Date
    Feb 2008
    Posts
    3

    Two shell script questions

    I'm writing a bash script that uses a string variable. I need to somehow truncate it so that only the first character remains, but I don't know how to do this. After searching on google for an hour, I figured I'd come here and ask. How do I grab the first character from a string variable? And how do I force a string into all upper- or lower-case?

    My other question is what options/commands do I use with Lynx in order to go to a website to get my wan ip? I've yet to figure out how to use Lynx properly in a script.

    I appreciate anybody's help!

  2. #2
    Linux Engineer
    Join Date
    Nov 2004
    Location
    Ft. Polk, LA
    Posts
    796
    The Advanced Bash Scripting Guide is an excellent resource for this type of thing. To grab the first character, do something like this:
    Code:
    firstchar=`expr substr "$string" 1 1`
    For changing the case, tr will come in handy here:
    Code:
    newstring=`echo $string | tr a-z A-Z`
    This converts to uppercase, swap the a-z with A-Z to go lowercase.

    Can't help with lynx, I know very little about it.

  3. #3
    Just Joined!
    Join Date
    Feb 2008
    Posts
    3
    I tried tr before but I couldn't get the syntax right... I got that script working properly now so thanks a lot!

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I am grateful to valan for providing the link to this Bash scripting guide. I've just spent a few moments splashing around in it, and like what I see.

    One minor fault I find with it, though, is that for the command substitution that valan explains, the author of the guide in fact uses the same syntax that valan uses: backticks
    Code:
    like `this`
    at the top of that chapter, and only way later on explains that the backticks have been superseded by something
    Code:
    like $(this)
    There's a good reason to get into the habit of using the newer syntax. Someday, before you know it, you might want to nest one of them inside another
    Code:
    like $(this stupid $(example))
    Although it's possible to do that by "escaping" the inner backticks (as the author is careful to point out), the format above is much more readable (and writeable!).

    Now. Your second question:
    what options/commands do I use with Lynx in order to go to a website to get my wan ip?
    First, just to see what you're getting, try this at the command line:
    Code:
    lynx -source http://ip.dnsexit.com/
    (This line will take a few seconds to complete.)

    You'll find that it gives you your external IP address. It's the only site I know of that gives you useful output without using any HTML.
    To put that output in a bash variable, try this:
    Code:
    xxx=$(lynx -source http://ip.dnsexit.com/)
    echo $xxx $xxx
    (The first of those lines will take a few seconds to complete.)
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by hyperhobbes View Post
    I'm writing a bash script that uses a string variable. I need to somehow truncate it so that only the first character remains, but I don't know how to do this. After searching on google for an hour, I figured I'd come here and ask. How do I grab the first character from a string variable? And how do I force a string into all upper- or lower-case?
    in bash
    Code:
    # a="astring"
    # echo ${a:0:1}
    a
    # echo $a | tr 'a-z' 'A-Z'
    ASTRING
    using awk
    Code:
    # echo $a | awk '{print substr($0,1,1)}'
    a
    # echo $a | awk '{print toupper($0)}'
    ASTRING
    My other question is what options/commands do I use with Lynx in order to go to a website to get my wan ip? I've yet to figure out how to use Lynx properly in a script.

    I appreciate anybody's help!
    if you have wget, you can use it too. Please read the bash manual in my sig for more information.

  6. #6
    Linux Engineer
    Join Date
    Nov 2004
    Location
    Ft. Polk, LA
    Posts
    796
    I knew bash had it's own way of doing it, but I couldn't remember it and saw the use of expr first. That's the fun thing about shell scripting though, there's so many ways of doing something it makes almost everything possible without much work.

  7. #7
    Just Joined!
    Join Date
    Feb 2008
    Posts
    3
    Yeah, I love the things you can do with Linux. It makes me feel invincible.

    Well I got both of my problems solved thanks to you guys. The -source option was what I was missing from my ip script. I greatly appreciate your help!

  8. #8
    Linux Newbie Sangal-Arun's Avatar
    Join Date
    May 2006
    Location
    Gurgaon, India + Denver Colorado USA
    Posts
    101
    $ echo abcd|cut -c 1
    a
    Brgds,

    ARUN SANGAL
    SCM: 1- 720 251 9962
    Email: sangal.ak04@gmail.com
    Email: sangal_ak04@yahoo.com

Posting Permissions

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