LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Closed Thread
  Search this Thread
Old 09-10-2005, 02:03 PM   #1
flobadon
LQ Newbie
 
Registered: Oct 2004
Location: Davenport, IA
Distribution: Gentoo 2005.1
Posts: 5

Rep: Reputation: 0
Variables and escape characters?


Hello all. I am supposed to write a simple script in bash for class, and have been having a heck of a time with something that should *probably* be pretty simple, here is what I need (or want, anyway)...

I am writing a script that does arithmetic via the commandline, for instance "./arithmetic 3 * 2" should output "3 * 2 = 6". So here is what I have written:

echo $1 $2 $3 = $(( $1$2$3 ))

Which works fine for everything except multiplication, since the asterisk gets interpreted as a wildcard. So, is it possible to make the script interpret it as the multiplication operator without entering an escape character into the command? Should I be using let instead?

Here are some rules for the writing of the script as given from my instructor:

"Your script should NOT use any conditional or repetition type statements.

Thanks in advance.
 
Old 09-10-2005, 02:22 PM   #2
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
Maybe a completely different approach might be more helpful: have a look at bc.

The Advanced Bash-Scripting Guide is a very good source if you want to script in bash.
 
Old 09-10-2005, 02:41 PM   #3
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Learn how to quote in the shell and it'll serve you for life. You can escape single characters with the \ (backslash) character and quote strings with single and double quotes. man sh (it's long!) describes it all, and it'll be well worth learning. Play around with
Code:
echo $HOME
echo \$HOME
echo "$HOME"
echo '$HOME'
echo hello world
echo hello                world
echo "hello              world"             again
and see how the behaviour differs.
 
Old 09-10-2005, 02:46 PM   #4
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Simply put, you should be able to quote each of the args in the substitiution operator. I would think $(( "$1$2$3" )) should work for you.
 
Old 09-10-2005, 04:34 PM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
posting homework questions goes against the rules of using LQ.org. it is up to you to understand the work, not us.

Whilst all members responses here have been provided in an encouraging angle rather than a solution as such, members should remember not to provide stock answers in situations like these, as it doens't benefit anyone.
 
  


Closed Thread



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
passing escape characters along with lp command zita Linux - General 0 10-05-2005 01:58 PM
bach scripting and escape characters... Bud-froggy Linux - Newbie 3 07-09-2004 04:55 PM
Escape Characters in linux shunraj Linux - Software 1 05-18-2004 03:21 PM
Handle escape characters in a string Helene Programming 7 05-01-2004 11:43 PM
escape characters not escaping BobNz Linux - Software 2 04-09-2004 03:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:25 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
Open Source Consulting | Domain Registration