Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 10-16-2007, 03:02 PM   #1
Registered: Feb 2005
Distribution: Slackware 11
Posts: 109

Rep: Reputation: 15
bash script read error and awk ouptut error

awk '//{print $2, $(($1/$y))}' ## ERROR does not divide
here are the two lines I have narrowed my errors down 2.

Project is due in 1 hr
Old 10-16-2007, 03:29 PM   #2
Senior Member
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Rep: Reputation: 53
I don't get it what do you want to do?
Old 10-16-2007, 04:18 PM   #3
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
assignment to and use of variables in borne style shells is done like this:
myvar="my value"
echo "use it like this: $myvar
i.e. you only use the $ when you want to return the value of a variable - not when you are assigning to it. Also, 'single quotes' evaluate to a literal string, thus:
echo 'myvar is $myvar'
will print:
myvar is $myvar
If you want the shell to expand the $myvar to the value which is assigned to it, you cannot do this inside 'single quotes'. You can use "double quotes" for this, e.g:
echo "myvar is $myvar"
will print:
myvar is my value
So, in your awk program, you cannot use the shell variable y in the program which is in single quotes, as awk will only get the literal string '$y'.
Old 10-16-2007, 06:01 PM   #4
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
To add to matthew's post:

To get to a shell variable in awk assign it to an awk variable on
invocation of awk
awk -v y=$y '{print $2, $(($1/y))}'

Old 10-16-2007, 07:05 PM   #5
Registered: Jul 2007
Location: United Kingdom, W Mids
Distribution: SUSE 11.0 as of Nov 2008
Posts: 195

Rep: Reputation: 40
## read with prompt. The prompt you used is "MNAME " assignment is by default to the reserved variable REPLY.
## if you wish to assign to a variable it should follow the prompt.
## you can assign to a number of variables with:
## read -p "MYPROMPT " var1 var2 varjunk
## that will assign the first variable into var1 the second into var2 and all further into varjunk
read -p "MNAME "
## this just demonstrates what what was assigned to the default variable REPLY.
echo reply is: $REPLY
## I don't see you piping or otherwise transferring data gleaned from the read command to the awk script.

## Tinker, I admit to not understanding the $(( )) construct within the awk code.
## The ( ) to force an expression is good for me. perhaps Tinker can enlighten us please.

## I never write code, I develop it. That is to say I regularly put in echo and print statements to prove anything and everything as I progress to ensure that all assumptions I make are tested during the coding. I throw away probably 5/6 or more of what I write, because I don't write it every day to be as familiar as I aught to be.

## Oh, and a problem shared is someone elses! If your project is going overtime, don't hide it but tell your manager. He might not be too happy at the prospect, but to get someone else to do the job won't be practical and once he knows about it, then he isn't going to be set upon by a client demanding answers and not knowing the score. As soon as he says, "ok hurry it up", then the pressure is off of you and on the manager.
## Have a happy project.

## PAix

Last edited by PAix; 10-16-2007 at 07:07 PM.


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
bash script error noir911 Programming 4 09-27-2006 07:52 AM
bash script error noir911 Programming 5 09-12-2006 06:42 AM
bash script error noir911 Programming 4 07-25-2006 03:58 AM
error when tying to run python script(bash error?) shanenin Programming 5 01-10-2006 10:01 AM
bash script error near 'else' ? nutthick Linux - Newbie 5 01-23-2005 03:41 PM

All times are GMT -5. The time now is 04:11 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration