Click to See Complete Forum and Search --> : "type casting" in Shell
gamblor01
06-03-2005, 07:35 PM
I know I can use declare -i to make something an integer. I'm wondering if it's possible to do subtraction on what would be a char variable in Java. For example, if someone gave me an argument in the shell script "My favorite letter is B" I would want to produce the output "My favorite letter is A". In Java I could do it like this...
char c = myString.charAt( myString.length() -1 );
int prev = (int)c - 1;
char newC = (char)prev;
I can get the last char in shell easily enough:
lastLet=`echo ${myString: -1}`
Now what once I've got that letter?
bwkaz
06-04-2005, 09:08 AM
Oooh, a useless use of backquotes! ;)
Your code probably should read:
lastLet=${myString: -1}
instead.
But anyway. No, I don't think the shell has any way of doing what you're talking about. However, the tr command does:
echo $lastLet | tr A-Za-z ZA-Yza-y
will print out one letter before whatever $lastLet is. So if $lastLet is "a", it'll print "z". You would need backquotes here, if you want to stuff this into another variable.
gamblor01
06-04-2005, 02:18 PM
Oooh, a useless use of backquotes!
Rofl! If I want to write inefficient code then so be it I will! Heh actually I didn't know that wasn't necessary, but now that I look at it...it's pretty obvious that it's not.. I'll probably still use it anyway though since I think it just looks cool. ;)
Thanks for the help though.
hammer123
06-12-2005, 01:39 AM
Originally posted by bwkaz
echo $lastLet | tr A-Za-z ZA-Yza-y
will print out one letter before whatever $lastLet is. So if $lastLet is "a", it'll print "z". You would need backquotes here, if you want to stuff this into another variable.
Please explain the magic of these statements to tr as I cannot figure them out. I'm currently able to use tr to make all upper case letters lower case and vice versa and cant figure out how to pass it more than two statements. I really want to make every instance of _[a-z] into a space and a capital letter. The tr man page always only has two arguments and I have no idea how you are making use of multiple arguments.
bwkaz
06-12-2005, 01:32 PM
tr always takes a range (...well, or a set) of characters as its first argument, and turns each instance of them in its input into the character in the corresponding slot in the second range (or set) in the output.
That sounds complicated. Hmm...
You give tr two sets of characters. If either set is a-z, for example, that means "all characters from a to z, inclusive".
tr then runs through its input. At each character, it looks to see whether the character is in the first set. If it is, tr replaces it with the character in the same position in the second set. Otherwise, it lets it through unchanged.
You can't make multiple-character matches or replacements with tr; it only works one character at a time. Case is also significant.
So the tr above can be broken down into upper and lowercase versions. If we look at just lowercase, it says to translate from a-z (all lowercase characters) to za-y. This is still all lowercase letters, but they're in a different order. When tr finds a lowercase a in its input, it sees that a is in the first set in the first position. So it looks at the second set's first position, and finds a z. It replaces the a with a z.
If tr finds a lowercase b in its input, it sees that lowercase b is in the first set of characters at the second position. The second position in the second set of characters has an a in it, so it replaces the b with an a.
Lowercase c likewise gets replaced with lowercase b, and so on down the line. And uppercase works the same way.