LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   bash script read error and awk ouptut error (http://www.linuxquestions.org/questions/programming-9/bash-script-read-error-and-awk-ouptut-error-592327/)

whited 10-16-2007 03:02 PM

bash script read error and awk ouptut error
 
$y=5
read -p MNAME ## ERROR DOES NOT READ
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

Alien_Hominid 10-16-2007 03:29 PM

I don't get it what do you want to do?

matthewg42 10-16-2007 04:18 PM

assignment to and use of variables in borne style shells is done like this:
Code:

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:
Code:

echo 'myvar is $myvar'
will print:
Code:

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:
Code:

echo "myvar is $myvar"
will print:
Code:

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'.

Tinkster 10-16-2007 06:01 PM

To add to matthew's post:

To get to a shell variable in awk assign it to an awk variable on
invocation of awk
Code:

awk -v y=$y '{print $2, $(($1/y))}'

Cheers,
Tink

PAix 10-16-2007 07:05 PM

## 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
Quote:

read -p "MNAME "
## this just demonstrates what what was assigned to the default variable REPLY.
Quote:

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


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