LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 03-20-2012, 10:05 AM   #1
rit
LQ Newbie
 
Registered: Feb 2012
Location: kanpur,india
Posts: 21

Rep: Reputation: Disabled
my first script


hello everyone,
this is the script i have written from the ebook i m referring to
#!/bin/sh
echo "enter x"
read x
echo "enter y"
read y
echo "$[x*y]"
the problem is the value of x and y is not
being evaluated in last echo command
 
Old 03-20-2012, 10:21 AM   #2
yancek
LQ Guru
 
Registered: Apr 2008
Distribution: PCLinux, Slackware
Posts: 6,984

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Change the last echo line. You didn't put spaces before the x and after the y:
Quote:
echo "$[ x*y ]"

Last edited by yancek; 03-20-2012 at 10:29 AM.
 
Old 03-20-2012, 10:30 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
Congratulations on beginning your scripting journey.

First of all, please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

Second, clean, consistent formatting makes code readable and more easily debuggable. Indent all your sub-commands, and separate logical sections with whitespace. Add comments anywhere the code isn't completely obvious (and remember, what seems obvious to you now will not be a year or so down the line).

Third, $[..] is deprecated syntax. arithmetic expressions should be done these days with $((..)) or ((..)).

Other than that, there are no syntax errors in your code, and it runs for me as posted. You should however use #!/bin/bash instead of #!/bin/sh (assuming you are coding for bash, of course). Using "sh" means that the script will be interpreted as a posix-compliant script, and many shell-specific features will be unavailable or have altered behavior.

Also, your next step should be to do some input checking on the variables, to make sure you're actually getting integers before you process them. This usually means running the input part in a loop, with an if or case statement for testing the value.

Code:
while true; do

	echo "enter x"
	read x

	case $x in
		*[^0-9]*) echo "Not an integer, try again."
			  continue
		;;
	esac

	break
done

Last edited by David the H.; 03-20-2012 at 10:32 AM. Reason: removed dangling tag
 
1 members found this post helpful.
Old 03-20-2012, 10:31 AM   #4
rit
LQ Newbie
 
Registered: Feb 2012
Location: kanpur,india
Posts: 21

Original Poster
Rep: Reputation: Disabled
thank you for reply
i did as you said but still it's displaying $[ x*y ]
after reading the value in x and y
 
Old 03-20-2012, 10:45 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
As I said, that syntax is deprecated, and not supported by all shells.

I can now reproduce your behavior by using dash as my shell instead, which many distros use for processing "sh" scripts. So your system is likely using dash to interpret posix scripts, and dash doesn't support "$[..]".

So change your script to "$(( x * y ))", and change your shebang to #!/bin/bash, as I suggested before.

Last edited by David the H.; 03-20-2012 at 10:52 AM. Reason: rewording for clarification
 
1 members found this post helpful.
Old 03-20-2012, 12:06 PM   #6
rit
LQ Newbie
 
Registered: Feb 2012
Location: kanpur,india
Posts: 21

Original Poster
Rep: Reputation: Disabled
i changed it from #!/bin/sh to #!/bin/bash and it worked.
late reply due to net problem.
thank you
 
  


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
Shell Script: Getting signal handlers to work when running an application by script pkjss Programming 4 09-14-2011 05:23 AM
[SOLVED] bash and xterm: how make apps started by and for a script persist when script terminates porphyry5 Linux - General 4 06-15-2011 02:27 PM
[SOLVED] Script question: create a shell script in kde to log in on a server with ssh c4719929 Linux - Newbie 1 01-31-2011 04:05 AM
How to get full path to script file inside script itself? And in case of sym links? maggus Linux - Newbie 3 05-28-2009 09:40 AM
Shell Script: want to insert values in database when update script runs ring Programming 2 10-25-2007 11:48 PM


All times are GMT -5. The time now is 12:42 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration