Shell Scripting

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

So I wrote my first shell script today. But I want it to be a little cooler. I want the variable $season to be converted to lowercase, so that if someone types in Fall, FALL, FaLl etc, it will still give them the fall message. I thought about using tr but I can't figure out a way to use tr with a variable

(tr [A-Z] [a-z] $season)=$season doesn't work. Any (simple) ideas?

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Create another function that reads the season and converts it to lower case. Instead of calling [incode]read season[/incode] everywhere, call your new function instead.

Here's one way to convert a variable's contents to lowercase: [incode]foo=`echo $foo | tr A-Z a-z`[/incode].

Btw, is this homework? Smiling

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

Ah, the pipe. Good idea.

Quote: Btw, is this homework?

Nope Smiling. We talked about if thens in class, but I didn't quite understand the syntax. So I wanted to create a simple script to help me better understand. It took me forever to figure out the difference between this=that and this = that. I guess whitespace does matter in more instances that I thought.

Thanks for the help again Abhishek. Did you learn *nix in a class setting like this, or on your own?

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

teammatt3;210808 wrote: It took me forever to figure out the difference between this=that and this = that. I guess whitespace does matter in more instances that I thought.

Yeah, it's really arcane. I avoid shell scripting with bash and co. whenever possible, unless it's a quick one-liner.

teammatt3;210808 wrote: Did you learn *nix in a class setting like this, or on your own?

Not in a class setting, but not entirely on my own. Online documentation is indispensable. Wink

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.