LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-02-2009, 06:26 PM   #1
Xyan
Member
 
Registered: Oct 2003
Location: Arizona
Distribution: ClearOS, Ubuntu, CentOS, Fedora
Posts: 33

Rep: Reputation: 15
Question Shell scripting question - joining variables or variable + text


Hello,
I've done some searching, but I'm not quite sure how to word this and I am not getting any results. So, I apologize if this has been asked, but I am at a loss for the correct wording.

Anyway I want to try something like this where...

1) you set $var1 to be "cat" (via the read command)
2) you would like to make a file based on the variable's name ex: /home/cat.log

Back in the days when I was a windows user and was on mIRC, you would use mIRC's flavor of scripting like so:

/home/ $+ $var1 $+ .log

and that would translate into /home/cat.log

Is there such a way to do this in the shell? Thank you very much ahead of time!
 
Old 01-02-2009, 06:38 PM   #2
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
See if this does what you want.
Code:
var=""
echo -n "Enter thy text variable: "
read var

filename=/home/$var.log

echo $filename
 
Old 01-02-2009, 06:46 PM   #3
Xyan
Member
 
Registered: Oct 2003
Location: Arizona
Distribution: ClearOS, Ubuntu, CentOS, Fedora
Posts: 33

Original Poster
Rep: Reputation: 15
Thumbs up

wow... that worked. I'm somewhat surprised. Is it because you set another variable containing that one? I tried that just plain-text (not setting a second variable) and didn't get results...

At any rate, thank you!
 
Old 01-02-2009, 07:02 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
As weibullguy shows, there's generally no need to do anything special with bash variables. Simply put them wherever you want the value to appear in the output.

Check out the Advanced Bash Scripting guide chapter on variables. Lots of good info and examples there. I also recommend the next chapter on quoting, which can have a big effect on the output.
 
Old 01-02-2009, 07:14 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quote:
Originally Posted by Xyan View Post
wow... that worked. I'm somewhat surprised. Is it because you set another variable containing that one? I tried that just plain-text (not setting a second variable) and didn't get results...
No, it doesn't have anything to do with the second variable. I'm sure he just used it as an example of how to use the output. You can even use variables directly from the command line.

Code:
david:~$ var="cat"
david:~$ echo "/home/$var.log"
/home/cat.log
david:~$
 
Old 01-02-2009, 07:34 PM   #6
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
As David the H. stated, it has nothing to do with the second variable. I showed that simply because I presumed you wanted to do something more exciting than echo /home/$var.log. I think it is better to use a variable for this purpose rather than risk a typo every time you use /home/$var.lpg (see I did it right there).

I also second David the H.'s recommended reading. Bookmark it if you plan to do much BASH scripting! Don't get scared about the Advanced title either.
 
Old 01-03-2009, 11:54 AM   #7
Xyan
Member
 
Registered: Oct 2003
Location: Arizona
Distribution: ClearOS, Ubuntu, CentOS, Fedora
Posts: 33

Original Poster
Rep: Reputation: 15
Smile

Thanks guys. Yeah that has definitely helped -- I've created two shell scripts to use openssl to generate a CA certificate + key and a user cert/key/pkcs12 set. Just simplifying the process as more of an educational endeavor.

My latest project has been a shell script + cron setup to reapply chmod/chown on several of the shares on the network server because they often get changed for various reasons.

Quote:
Originally Posted by weibullguy View Post
I presumed you wanted to do something more exciting than echo /home/$var.log. I think it is better to use a variable for this purpose rather than risk a typo every time you use /home/$var.lpg
Haha, yeah, I wanted something more exciting than that -- you presumed correctly. That actually helped me a lot, now that I know I can make variables within variables without much trouble (as opposed to some programming languages).

Thank you again, I just had it stuck in my mind that I needed something to join the variables (old dog, new tricks... hehe)

Last edited by Xyan; 01-03-2009 at 12:10 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell scripting : how to name a variable with the value of another variable JimOrJoe Programming 9 06-15-2008 09:36 PM
shell scripting/bash/variables mayaabboud Linux - Newbie 13 01-06-2008 12:16 PM
shell scripting/bash/variables mayaabboud Linux - General 4 01-06-2008 08:17 AM
Bash Shell Scripting Dynamic Variable naming question ZuG Programming 2 02-07-2007 02:39 PM
shell scripting: variable problem XST1 Programming 7 01-30-2005 01:45 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration