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 ...
- 02-26-2008 #1Just 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!
- 02-26-2008 #2Linux 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:
For changing the case, tr will come in handy here:Code:firstchar=`expr substr "$string" 1 1`
This converts to uppercase, swap the a-z with A-Z to go lowercase.Code:newstring=`echo $string | tr a-z A-Z`
Can't help with lynx, I know very little about it.
- 02-26-2008 #3Just 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!
- 02-26-2008 #4
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
at the top of that chapter, and only way later on explains that the backticks have been superseded by somethingCode: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 anotherCode:like $(this)
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!).Code:like $(this stupid $(example))
Now. Your second question:
First, just to see what you're getting, try this at the command line:what options/commands do I use with Lynx in order to go to a website to get my wan ip?
(This line will take a few seconds to complete.)Code:lynx -source http://ip.dnsexit.com/
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:
(The first of those lines will take a few seconds to complete.)Code:xxx=$(lynx -source http://ip.dnsexit.com/) echo $xxx $xxx
--
Bill
Old age and treachery will overcome youth and skill.
- 02-26-2008 #5Linux User
- Join Date
- Aug 2006
- Posts
- 458
in bash
using awkCode:# a="astring" # echo ${a:0:1} a # echo $a | tr 'a-z' 'A-Z' ASTRING
Code:# echo $a | awk '{print substr($0,1,1)}' a # echo $a | awk '{print toupper($0)}' ASTRINGif you have wget, you can use it too. Please read the bash manual in my sig for more information.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!
- 02-26-2008 #6Linux 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.
- 02-26-2008 #7Just 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!
- 09-26-2008 #8
$ echo abcd|cut -c 1
a


Reply With Quote
