Bash Expr Multiplication Syntax Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack expr syntax error in shell script Overflow the company Business Learn more about hiring developers or posting ads with us bash expr multiply Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a bash expr math community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Multiplication with expr in shell script up vote 2 down vote favorite 1 I am multiplication in unix shell script making a basic calculator to add, subtract, multiply, and divide. Addition works, but not multiplication. When I attempt to multiply, I get the "You did not run the program correctly" response: $ ./calculator 4 + 5 9 $ ./calculator 4 * 5 You did not run the program correctly Example: calculator 4 + 5 I've searched around on google, where I found the \\* code, but still doesn't work. Can someone
Bash Multiplication Variable
provide me with a solution or explanation? Here is my code #!/bin/bash if [ $# != 3 ]; then echo You did not run the program correctly echo Example: calculator 4 + 5 exit 1 fi if [ $2 = "+" ]; then ANSWER=`expr $1 + $3` echo $ANSWER fi if [ $2 = "*" ]; then ANSWER=`expr $1 \\* $3` echo $ANSWER fi exit 0 linux bash shell unix expr share|improve this question edited Nov 30 '14 at 21:07 that other guy 45k54070 asked Nov 30 '14 at 20:55 Alex Wolfe 11112 It's unlikely you need expr; ANSWER=$(( $1 + $3 )), for example, should work with any shell you are likely to be using with Linux. –chepner Nov 30 '14 at 21:36 add a comment| 2 Answers 2 active oldest votes up vote 3 down vote Your code has many problems. Here is a fix. * means "all files in the current directory". To instead mean a literal asterisk/multiplication character, you have to escape it: ./calculator 3 \* 2 or ./calculator 3 "*" 2 You also have to double quote "$2", otherwise * will start meaning "all files" again: #!/bin/bash #Calculator #if [ `id -u` != 0 ]; then # echo "Only root may run this progra
functionality list is quite similar to that of UNIX and become very popular over the last several years. Our Basic motive is to provide
Bash Multiplication Decimals
latest information about Linux Operating system. Python Programming Python is a comparatively simple shell script for multiplication of two numbers programming language, compared to c++. Although some of the benefits of c++ are abstracted away in python, they are replaced shell script multiplication table with an overall easier to learn language with many "intuitive" features. For this reason it is common and recommended by most professionals that people new to programming start with python. Perl Programming http://stackoverflow.com/questions/27218141/multiplication-with-expr-in-shell-script Perl is an open-source, general-purpose interpreted programming language. Used often for CGI, Perl is also used for graphics programming, system administration, network programming, finance, bioinformatics, and other applications. The Perl languages borrow features from other programming languages including C, shell scripting (sh), AWK, and sed. They provide powerful text processing facilities without the arbitrary data-length limits of many contemporary UNIX command line tools, facilitating http://www.bashguru.com/2010/12/math-in-shell-scripts.html easy manipulation of text files. Android Android is an operating system based on the Linux kernel, and designed primarily for touch screen mobile devices such as smart phones and tablet computers. Android is a Linux-based software system, and similar to Linux, is free and open source software. This means that other companies can use the Android operating developed by Google and use it in their mobile devices.Android gives you a world-class platform for creating apps and games for Android users everywhere, as well as an open marketplace for distributing to them instantly. Sunday, December 26, 2010 Posted by venu k 144 comments | 7:36 AM Math in Shell Scripts One thing that often confuses new users to the Unix / Linux shell, ishow to do (even very simple) maths. In most languages, x = x + 1 (oreven x++) does exactly what you would expect. The Unix/Linux shell isdifferent,however. It doesn't have any built-in mathematical operatorsfor variables. It can do comparisons, but maths isn't supported, noteven simple addition. Shell script variables are by default treated as strings,not numbers,which adds some complexity to doing math in shell script. To keep withscript programming paradigm
& Answers This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in this forum UNIX for Beginners Questions & Answers Search Forums http://www.unix.com/unix-for-dummies-questions-and-answers/60062-multiplication-shows-syntax-error.html Show Threads Show Posts Tag Search Advanced Search Unanswered Threads Find All Thanked Posts Go to Page... linux operating commands and unix operating commands multiplication shows syntax error UNIX for Dummies Questions & Answers Thread Tools Search this Thread Display Modes #1 04-07-2008 sonicstage Registered User Join Date: Apr 2008 Last Activity: 4 May 2008, 7:09 PM EDT Posts: 5 Thanks: 0 shell script Thanked 0 Times in 0 Posts multiplication shows syntax error my shell script is simple arithmetic process.my addtion,substract and divide all can be worked only except multiplication.please help me.the shell is following: Code: #!/usr/bin/sh echo "enter a number1:" read number1 echo "enter an operator:" read operator echo "enter a number2:" read number2 echo "result is:" result='expr $number1 $operator $number2' echo $result Last edited by Yogesh Sawant; 04-08-2008 at 04:57 AM.. Reason: bash expr multiplication added code tags Remove advertisements Sponsored Links sonicstage View Public Profile Find all posts by sonicstage #2 04-07-2008 vbe Moderator Join Date: Sep 2005 Last Activity: 1 October 2016, 12:30 PM EDT Location: Switzerland - GE Posts: 6,296 Thanks: 239 Thanked 514 Times in 478 Posts What does * represent for the shell? That should help you see what is going wrong... Remove advertisements Sponsored Links vbe View Public Profile Find all posts by vbe #3 04-07-2008 vbe Moderator Join Date: Sep 2005 Last Activity: 1 October 2016, 12:30 PM EDT Location: Switzerland - GE Posts: 6,296 Thanks: 239 Thanked 514 Times in 478 Posts Since you have posted very lately a few, that give me the feeling its close to homework if not... I will just show you that modifying just a little your script I end with: Code: ra:/home/vbe $ calcul enter a number1: 34 enter an operator: * enter a number2: 2 result is: 68 ra:/home/vbe $ Look at my previous post and think hard... Last edited by Yogesh Sawant; 04-08-2008 at 04:58 AM.. Reason: added code tags vbe View Public Profile Find all posts by vbe #4 08