LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training 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 03-26-2013, 05:34 PM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
bash values ## and %%


I am reconfiguring a bash script called "/data/price_spin" and it has these values after the varaible %% and ##
I don't know what they are called, what they do and entering in ## in google is not working


The script below takes a human price - like 5.00 and turns in into something this process can read
which in this case is 0000050000

I do not understand how the below script does what it does -- the statement "printf "%06d%.4f" "${price%%.*}" ".${price##*.}"" is a mystery to me

/data/price_spin IBM 5.00
IBM
0000050000

Code:
#!/bin/bash
stock=$1
price=$2

spin_price=$(printf "%06d%.4f" "${price%%.*}" ".${price##*.}" | sed 's/0\.//' )

echo "$stock"
echo "$spin_price"
 
Old 03-26-2013, 05:44 PM   #2
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Debian, Arch
Posts: 1,161
Blog Entries: 7

Rep: Reputation: 328Reputation: 328Reputation: 328Reputation: 328
Those aren't related to bash, but to the printf command.

Code:
man 1 printf
and

Code:
info coreutils 'printf invocation'

Last edited by goumba; 03-26-2013 at 05:46 PM. Reason: Added info on "info"
 
Old 03-26-2013, 06:03 PM   #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: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
The ##/%% are two of the bash built-in parameter substitution patterns (aka parameter expansion). They remove substrings from the beginning and ending, respectively, of the string in the variable.

In this case they remove the parts before and after the decimal.

Bash also provides its own built-in version of printf, too, which is not exactly equal to the coreutils one.

One common use of printf is zero-padding numbers, as in this case.


Edit: incidentally, you can eliminate the sed program with another substitution pattern. Which then also means that printf can set the variable directly.

Code:
#!/bin/bash
stock=$1
price=$2

printf -v spin_price '%06d%.4f' "${price%%.*}" ".${price##*.}"
spin_price=${spin_price/0.}

echo "$stock"
echo "$spin_price"
I'd imagine that it's possible to create a better printf statement too, that wouldn't need the extra manipulation afterwards. But I'm too tired right now to bother with figuring it out.

Last edited by David the H.; 03-26-2013 at 06:13 PM.
 
Old 03-26-2013, 06:25 PM   #4
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Debian, Arch
Posts: 1,161
Blog Entries: 7

Rep: Reputation: 328Reputation: 328Reputation: 328Reputation: 328
Oops, my error, didn't read it all apparently.
 
Old 03-26-2013, 10:51 PM   #5
nicksu
Member
 
Registered: Dec 2012
Posts: 35

Rep: Reputation: Disabled
Quote:
Originally Posted by casperdaghost View Post
I am reconfiguring a bash script called "/data/price_spin" and it has these values after the varaible %% and ##
I don't know what they are called, what they do and entering in ## in google is not working


The script below takes a human price - like 5.00 and turns in into something this process can read
which in this case is 0000050000

I do not understand how the below script does what it does -- the statement "printf "%06d%.4f" "${price%%.*}" ".${price##*.}"" is a mystery to me

/data/price_spin IBM 5.00
IBM
0000050000

Code:
#!/bin/bash
stock=$1
price=$2

spin_price=$(printf "%06d%.4f" "${price%%.*}" ".${price##*.}" | sed 's/0\.//' )

echo "$stock"
echo "$spin_price"
Hi,you can refer to below

[ssh@Nick sf_osshare]$ echo $a
0c122c312
[ssh@Nick sf_osshare]$ echo ${a#*c}
122c312
[ssh@Nick sf_osshare]$ echo ${a##*c}
312
[ssh@Nick sf_osshare]$ echo ${a%c*}
0c122
[ssh@Nick sf_osshare]$ echo ${a%%c*}
0

a # would takes up the character the first "c" from left to right and his left side characters,while both would become greedy will take up the last "c" and his left side characters so it shows out as "122c312" and "312" respectively.
and for % is from right to left.
and you can keep in mind that the # is left to the $,and % is right to it.we usually use $ to stand for a variable,so # to handle the left side and % the right side .
 
Old 03-27-2013, 07:28 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,511

Rep: Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891
Are you likely to have more than 2 digits entered after the decimal point?
If not, you could simply do something like:
Code:
printf "%010d\n" ${price/.}00
 
  


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
[SOLVED] summinf values in bash julirio Linux - Newbie 2 10-10-2012 09:03 AM
[SOLVED] Get values from property file using bash samadm Programming 5 10-06-2012 03:10 PM
[SOLVED] replacing empty values in bash xeon123 Linux - Newbie 6 03-11-2011 11:12 AM
bash; reading values from a file km4hr Programming 16 07-28-2005 02:07 PM
bash - comparing a variable to several values davee Programming 3 05-05-2003 07:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:08 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