Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a script:
#!/bin/bash
var1=1111688
var2=374335
prctg=`echo | awk '{print 100 - $var2\*100/$var1}'`
echo "$prctg"
prctg1=$((echo | awk '{print 100 - $var2\*100/$var1}'))
echo "$prctg1"
But when I invoke it, it's giving me following error:
awk: syntax error near line 1
awk: illegal statement near line 1
testscript.sh: syntax error at line 6: `prctg1=$' unexpected
Could anybody rectify, where I am making mistake? Running it on Solaris 10.
you shouldn't escape * in awk code (it's already quoted for bash by the single quotes)
you can use the BEGIN pattern instead of the echo | awk ... kludge
Code:
prctg=`awk -vvar1="$var1" -vvar2="$var2" 'BEGIN{print 100 - var2*100/var1}'`
# or, if you are doing just arithmetic in awk, bash's string interpolation could work:
prctg=`awk "BEGIN{print 100 - $var2*100/$var1}"`
Ntubski, thanks for your reply. But unfortunately none of your suggested commands worked! I am running it in Solaris 10 and I am running this script with #!/bin/bash. Bash version installed in my system is 3.00.16(1). If I am using following expression in my script:
prctg=`awk "BEGIN{print 100 - $var2*100/$var1}"`
It gives the error:
awk: divided by zero
Else, all other commands give the error I already mentioned.
Even if I change the shell to #!/bin/csh or #!/bin/tcsh, and set variables using "set <var-name>=<value>", it's still giving that same error as mentioned previously!!
it works fine, as you see. The only division you're doing is by $var1, so I assume the error is there (or in a line of your code you haven't posted)
Yes Snark, it finally worked. Perhaps the awk code I was trying previously was wrong. I created a simple script in same environment, tested it, and it worked:
#!/bin/bash
var1=1111688
var2=374335 echo | awk "BEGIN{print 100 - $var2*100/$var1}"
Output: 66.3273
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.
awk operates on values fed to it through stdin or files. It doesn't do stand-alone operations except inside the BEGIN part of the statement.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.