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 09-28-2012, 12:14 PM   #1
sciss
LQ Newbie
 
Registered: Sep 2012
Posts: 6

Rep: Reputation: Disabled
Quoting brackets inside a bash function


Hey!
I want to use in console a fine calculator Qalculate! It can manage ISU units, brackets - its something like science calculator. I can use it like this:
Code:
$ qalc 2+2
2 + 2 = 4
When I try using brackets, it's gives me back an error:
Code:
$ qalc (2+2)2
bash: błąd składni przy nieoczekiwanym znaczniku `2+2'
(Translation: syntax error near unexpected marker `2+2' )
I can quote it in several ways and then it's work fine:
Code:
$ qalc "(2+2)2"
(2 + 2) * 2 = 8
$ qalc '(2+2)2'
(2 + 2) * 2 = 8
$ qalc \(2+2\)2
(2 + 2) * 2 = 8
But I wanted to use it by alias "c" and without qouting all equation, in.ex.
Code:
$ c (2+2)2
So I wrote a function inside a .bashrc. I tried:
Code:
function c() { qalc $@; }
function c() { qalc "$@"; }
function c() { "qalc $@"; }
function c() { qalc '$@'; }
function c() { qalc \"$@\"; }
function c() { qalc "\"$@\""; }
Nothing works. Is there any way to do it?
Sorry for my English.
 
Old 09-28-2012, 12:34 PM   #2
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 421
Blog Entries: 28

Rep: Reputation: 2
I don't know the software qalc, but in console, you should be able to do simple arithmetics by default:
Code:
ted:$ echo $((2+2))
4
server:/~
ted:$ echo $((10/2))
5
server:/~
ted:$ echo $((10-2))
8
server:/~
ted:$ echo $((5*(10-2)))
40
If you are trying to achieve something else could you explain a bit more?
Ted
 
Old 09-28-2012, 12:56 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
To multiply, you need "*"----eg:
(2+2)*2
 
Old 09-28-2012, 12:56 PM   #4
sciss
LQ Newbie
 
Registered: Sep 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Yes, I'm know that. I use tilda console, and I want to be able to do some quick calculations without problems.
Code:
$ qalc "(54km-456m)/(5h+55s)"
((54 * kilometer) - (456 * meter)) / ((5 * hour) + (55 * second)) = approx. 2.9656051(m / s)
I want the same effect by command (equation without quotation marks and alias qalc=c):
Code:
$ c (54km-456m)/(5h+55s)
 
Old 09-28-2012, 01:02 PM   #5
sciss
LQ Newbie
 
Registered: Sep 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pixellany View Post
To multiply, you need "*"----eg:
(2+2)*2
Yes, but its not a problem. Qalc understand that:
Code:
$ qalc "(2+2)2"
(2 + 2) * 2 = 8
$ qalc (2+2)2
bash: błąd składni przy nieoczekiwanym znaczniku `2+2'
(syntax error near unexpected marker `2+2' )

Last edited by sciss; 09-28-2012 at 01:20 PM.
 
Old 09-28-2012, 01:31 PM   #6
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 421
Blog Entries: 28

Rep: Reputation: 2
can you post the outputs of
Code:
ls /bin | grep qalc
ls /sbin | grep qalc
ls /usr/sbin | grep qalc
ls /usr/bin | grep qalc
separately?
I am trying to get the env here.
Ted
 
Old 09-28-2012, 01:37 PM   #7
sciss
LQ Newbie
 
Registered: Sep 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Code:
$ ls /bin | grep qalc
$ ls /sbin | grep qalc
$ ls /usr/sbin | grep qalc
$ ls /usr/bin | grep qalc
qalc
qalculate
qalculate-gtk
 
Old 09-28-2012, 01:38 PM   #8
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 421
Blog Entries: 28

Rep: Reputation: 2
do
Code:
ln -s /usr/bin/qalc /usr/bin/c
Now you should be able to do
Code:
c "(2+2)2"
Ted
 
Old 09-28-2012, 01:47 PM   #9
sciss
LQ Newbie
 
Registered: Sep 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
But it works just fine. I can do
Code:
c "(2+2)2"
But I want to do
Code:
c (2+2)2
Sorry, maybe I wrote it in unnecessarily complicated way.

Last edited by sciss; 09-28-2012 at 01:48 PM.
 
Old 09-28-2012, 01:51 PM   #10
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 421
Blog Entries: 28

Rep: Reputation: 2
Can you do
Code:
c \(2+2\)2
?
To my knowledge, () are special characters in bash, without quotation marks, you DO need to escape them, because bash will parse them specially.
 
1 members found this post helpful.
Old 09-28-2012, 01:58 PM   #11
sciss
LQ Newbie
 
Registered: Sep 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
yes, I can. I thought it can be done to somehow pass over this rule, and simplify the command.
Thanks for your help.
 
Old 09-30-2012, 09:00 AM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Just to make it clear, this simply cannot be done without quoting.

Since the shell parses the line before executing it, and since parentheses and several other characters have special meanings to it, they must be protected from the shell in order for them to be properly passed to the command.

This is true of all commands, BTW. If a raw text string contains anything the shell could consider special, those characters have to be escaped first, either with quotes or backslashes.


You can, however, always execute qalc in interactive mode, where you can talk to it directly without the shell getting in the way.
 
1 members found this post helpful.
  


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] Bash syntax - question regarding quoting: quoting doesn't work as I expect. c.stadegaart Linux - General 5 04-03-2012 12:40 AM
Using an alias inside a function in bash martindl Linux - Newbie 1 06-09-2011 12:55 AM
create a global associative array variable inside a function of a bash script konsolebox Programming 3 07-14-2009 07:08 AM
shell quoting within a function dazdaz Linux - Software 4 09-03-2007 12:01 PM
ImageMagick: the rand() function and using brackets in commands LinuxLala Linux - Software 0 04-21-2005 02:46 PM


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