Let statement | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Let statement

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 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 09:05 PM.. Reason: Please use code tags
Sponsored Links
    #2  
Old 01-27-2010
frans's Avatar
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 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 09:05 PM.. Reason: Added code tags
    #4  
Old 01-27-2010
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 31 August 2014, 7:32 PM EDT
Location: Toronto, Canada
Posts: 2,877
Thanks: 0
Thanked 110 Times in 102 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 Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 09:46 PM
If statement (yes or no) bobo UNIX for Dummies Questions & Answers 7 10-21-2008 03:46 PM
How is use sselect statement o/p in insert statement. nkosaraju Shell Programming and Scripting 2 08-06-2008 10:26 PM
If statement - How to write a null statement april Shell Programming and Scripting 3 04-16-2008 02:14 PM
Let statement in ksh HELP bobo UNIX for Dummies Questions & Answers 3 01-12-2007 01:13 AM



All times are GMT -4. The time now is 09:32 PM.