LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-20-2011, 10:42 AM   #1
BMan8577
Member
 
Registered: Apr 2011
Posts: 73

Rep: Reputation: 0
Question Still Need Help...


Can this command for the first part be an alternative? I just want to make sure that this would work in the .bashrc file. What I did was join the two commands (the second command I know would only work if the first command is successful) like so: VAR="$VAR{LINUX_VERS}";head -1 /var/log/dmesg. Would this be a solution to regenerating the /var/log/dmesg so the variable LINUX_VERS is set dynamically when machine is booted? And if I do not get it right could someone let me know? I just do not want to clobber or screw up the ~/.bashrc file. Thanks guys and thanks for the links. I understand the concepts it's just a little difficult implementing certain things whats hard is easy and whats easy is hard...lol.
 
Old 04-20-2011, 11:45 AM   #2
savona
Member
 
Registered: Mar 2011
Location: Bellmawr, NJ
Distribution: Red Hat / Fedora
Posts: 195

Rep: Reputation: 50
Are you trying to put the output of "head -1 /var/log/dmesg" into the variable LINUX_VERS? If so what you have will not work.

The semi colon just tells the shell to run both commands, not redirect the output.

You can do this in bash by setting the variable with backticks (under the tilde)


# LINUX_VERS=`head -1 /var/log/dmesg`

# echo $LINUX_VERS
Linux version 2.6.18-238.5.1.el5 (mockbuild@x86-002.build.bos.redhat.com) (gcc version 4.1.2 20080704 (Red Hat 4.1.2-50)) #1 SMP Mon Feb 21 05:52:39 EST 2011


So the command you need in bashrc is:
LINUX_VERS=`head -1 /var/log/dmesg`
 
Old 04-20-2011, 11:55 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
I'm not quite sure I understand what you're trying to do. The line you gave is actually two separate commands.

The first one:
Code:
VAR="$VAR{LINUX_VERS}"
Sets the value of VAR to the previous value of VAR with the literal string {LINUX_VERS} tacked onto it.

The second command is, of course:
Code:
head -1 /var/log/dmesg
But it would be fairly pointless, as all it would do is echo the first line of dmesg onto the shell at startup.

";" is simply a concatenator, kind of like a synonym for a new line. the running of the second command occurs after the first one, no matter what the outcome. If you want the second command to run only if the first one is successful, you have to use "&&" instead.

I have no idea what you mean by "regenerating the /var/log/dmesg so the variable LINUX_VERS is set dynamically when machine is booted" either. Could you please explain what you're trying to do in more detail?

As for "screwing up your bashrc", you can always test the command directly in an existing shell first. Any command in bashrc can in theory be run on the command line, and vice-versa. If it screws up in the shell you can just close it and start over.

Finally, please use [code][/code] tags around your code, to preserve formatting and to improve readability.

Edit @savona:

Although it isn't wrong, $(..) is recommended over `..`, so this is better:
Code:
LINUX_VERS=$( head -1 /var/log/dmesg )
I don't really think this is what the OP wants though. What's the purpose of setting that variable to what could turn out to be a random line from dmesg? My output for the command is currently "[ 0.000000] Initializing cgroup subsys cpuset", for example.

Last edited by David the H.; 04-20-2011 at 12:04 PM. Reason: as stated
 
Old 04-20-2011, 01:26 PM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Quote:
Originally Posted by savona View Post
backticks (under the tilde)
No!!!

Use $( command ) instead. It's much better and easier to read.
 
Old 04-20-2011, 01:35 PM   #5
savona
Member
 
Registered: Mar 2011
Location: Bellmawr, NJ
Distribution: Red Hat / Fedora
Posts: 195

Rep: Reputation: 50
Quote:
Originally Posted by MTK358 View Post
No!!!

Use $( command ) instead. It's much better and easier to read.
OK, OK Im sorry.
 
Old 04-20-2011, 07:49 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,437

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
@MTK358 - No need to be quite so harsh

@savona - no need to be sorry as you were not incorrect

Whilst I personally prefer the $() the choice is up to each user. The ability to be easier to read is one
aspect, but I find where it really shines is nesting process substitutions and the need for escaping is
reduced and in some cases erradicated altogether.

Follow Dadvid's link for a fuller explanation.
 
Old 04-20-2011, 07:55 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,286

Rep: Reputation: 2347Reputation: 2347Reputation: 2347Reputation: 2347Reputation: 2347Reputation: 2347Reputation: 2347Reputation: 2347Reputation: 2347Reputation: 2347Reputation: 2347
Concur with grail; guys take it easy on a new guy hey
Basically, backticks (``) is old school, $() is new, but either will work and its as well to be aware of both syntaxes.
Certainly the new one is easier to read and nest; old one is less typing haha
 
1 members found this post helpful.
Old 04-22-2011, 03:16 PM   #8
BMan8577
Member
 
Registered: Apr 2011
Posts: 73

Original Poster
Rep: Reputation: 0
thanks guys for your help. I really appreciate it and creditably thank all of you for your help.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off




All times are GMT -5. The time now is 10:07 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration