Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Let statement

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-27-2010
erora erora is offline
Registered User
 
Join Date: Jan 2010
Last Activity: 14 July 2011, 11:16 PM EDT
Posts: 10
Thanks: 0
Thanked 1 Time in 1 Post
Let statement

I came across the following program:

Code:
if test $# = 3
then
	case $2 in
	 +) let z=$1+$3;;
	 -) let z=$1-$3;;
	 /) let z=$1/$3;;
	 x|X) let z=$1*$3;;
	 *) echo Warning - $2 invalied operator, only +,-,x,/ operator allowed
	    exit;;
	esac
	echo Answer is $z

What is the usage of let statement? I have never come across the same. Please let me know the usage, syntax and the need.

Instead of using the let statement above, we could have directly used

Code:
case $2 in
	 +)  z=$1+$3;;
	 -)  z=$1-$3;;
	 /)  z=$1/$3;;
	 x|X)  z=$1*$3;;

Then what is the need of using "let" here or anywhere else?

Last edited by Scott; 01-27-2010 at 08:05 PM.. Reason: Please use code tags
Sponsored Links
    #2  
Old Unix and Linux 01-27-2010
frans's Unix or Linux Image
frans frans is offline Forum Advisor  
Registered User
 
Join Date: Oct 2009
Last Activity: 4 December 2013, 9:39 AM EST
Location: France
Posts: 839
Thanks: 4
Thanked 54 Times in 53 Posts
let performs arithmetic operations try just :

Code:
A=2
B=5
let C1=$A+$B
echo $C1
C2=$A+$B
echo $C2

You see ?
Sponsored Links
    #3  
Old Unix and Linux 01-27-2010
erora erora is offline
Registered User
 
Join Date: Jan 2010
Last Activity: 14 July 2011, 11:16 PM EDT
Posts: 10
Thanks: 0
Thanked 1 Time in 1 Post
Hi
Thanks, got it.
So, instead of using let and still have the same effect, I can use expr right?

Code:
A=2
B=5
C3=`expr $A + $B`
echo $C3

Let command is just simpler since I do not need to use expr command and worry about the backquotes, spaces on either side of + sign and so on so forth

Right?
Thanks again

Last edited by Scott; 01-27-2010 at 08:05 PM.. Reason: Added code tags
    #4  
Old Unix and Linux 01-27-2010
cfajohnson's Unix or Linux Image
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 27 July 2015, 12:41 PM EDT
Location: Toronto, Canada
Posts: 2,896
Thanks: 0
Thanked 131 Times in 116 Posts
Quote:
Originally Posted by erora View Post
Hi
Thanks, got it.
So, instead of using let and still have the same effect, I can use expr right?

Code:
A=2
B=5
C3=`expr $A + $B`
echo $C3

Let command is just simpler since I do not need to use expr command and worry about the backquotes, spaces on either side of + sign and so on so forth

Right?

The let command is unnecessary and is not standard.

There is no need for expr in a modern (i.e., POSIX) shell. Use the shell's own arithmetic capabilities:


Code:
C3=$(( $A + $B ))

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
If statement help sdpinoy Shell Programming and Scripting 3 01-24-2009 08:46 PM
If statement (yes or no) bobo UNIX for Dummies Questions & Answers 7 10-21-2008 02:46 PM
How is use sselect statement o/p in insert statement. nkosaraju Shell Programming and Scripting 2 08-06-2008 09:26 PM
If statement - How to write a null statement april Shell Programming and Scripting 3 04-16-2008 01:14 PM
Let statement in ksh HELP bobo UNIX for Dummies Questions & Answers 3 01-12-2007 12:13 AM



All times are GMT -4. The time now is 11:52 PM.