LinuxQuestions.org
Visit Jeremy's Blog.
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 01-30-2012, 01:47 PM   #1
mp85
LQ Newbie
 
Registered: Dec 2011
Posts: 18

Rep: Reputation: Disabled
Getting a variable from printf


I had to do some rounding in a script using printf, but how do I get the output to become a variable

Code:
$velper=141.600
printf "%.0f\n" $velper
What do I add so that i can have a new variable become the result of printf
(I have a feeling this is really simple)


thanks
 
Old 01-30-2012, 02:30 PM   #2
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Code:
myvar="$(command)"
This will put the output of "command" into variable "myvar".
 
1 members found this post helpful.
Old 01-30-2012, 02:31 PM   #3
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
@ Reply

Hi mp85,

Welcome to LQ!!!

If you are trying to round of the value of variable velper and want to replace the existing value of velper to the rounded of value then you can do it as follows:

Code:
velper=`printf "%.0f\n" $velper`
If you want to save the value to any other variable then you can replace the velper on left hand side with any other variable as follows:

Code:
new=`printf "%.0f\n" $velper`
 
1 members found this post helpful.
Old 01-30-2012, 02:31 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
Assuming this is bash:

Code:
newVariable=$(printf "%.0f\n" $velper)
Keep in mind that you may want to remove the '\n' in your format specification for printf. If you do not, then newVariable will have a newline as part of its value--which is probably not what you want.
 
1 members found this post helpful.
Old 01-30-2012, 02:41 PM   #5
mp85
LQ Newbie
 
Registered: Dec 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Dark_Helmet View Post
Assuming this is bash:

Code:
newVariable=$(printf "%.0f\n" $velper)
Keep in mind that you may want to remove the '\n' in your format specification for printf. If you do not, then newVariable will have a newline as part of its value--which is probably not what you want.
Thanks so much, I did think I tried this but with the \n which i assume caused it to fail. I had no idea what that part of the printf function was doing.
 
Old 01-31-2012, 09:28 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
Or alternately, still assuming bash:

Code:
$ velper=141.600
$ printf -v newvar "%.0f" "$velper"
$ echo "$newvar"
142
Use "help printf" for a short summary, and "man bash > SHELL BUILTIN COMMANDS" for more detail.

And remember, always quote your variable substitutions. Get into the habit now.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with script writing: Storing cmd in variable, print variable, then exe cmds Arodef Programming 3 01-17-2012 12:26 AM
problem while comparing awk field variable with input variable entered using keyboard vinay007 Programming 12 08-23-2011 12:44 AM
Script to copy specific directory based on variable to folder with that variable name fluxburn Programming 7 01-07-2010 07:59 PM
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 12:08 AM
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 03:44 PM


All times are GMT -5. The time now is 03:23 AM.

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