Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calculate 1 * 2 1

Status
Not open for further replies.

PC888

Programmer
Nov 7, 2003
117
CA
Hi everyone,

I am trying out an elementary script running on bash Fedora to do a binary calculation, such as 3 * 2 = 6.

The trouble is, the parameter substitution directly substitutes the * into file names of the current directory before the script could handle anything.

It appears that there is a bash argument -f to suppress this phenonomenon, but it is not documented in man, nor does it work.

Could someone give me a hint, please?
 
Yes, this is the idea, as I have the line
echo $(($1 $2 $3))
further down in the script.

I absolutely need to put the operator as $2 so that I could use different functions, such as exponentiation, addition, subtraction, modulo, etc.

However, the command-line parameter $2 gets expanded to file names before I can catch it. For now, I am asking the users to type in an x for * and ^ for **, and so on.

Alternatively, users could type in '*' for multiplication to avoid the expansion.

I suppose there is a way to suppress the file name expansion.

Many thanks for looking into it.
 
use "$2" instead of $2

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Nope, it still gives the second file name of the directory!
In fact, I tried $2, "$2", ${2} all give the same thing. As expected, '$2' just outputs $2.

Would that have something to do with glob or some related parameter?
 
set -f
set -o noglob

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
$2 still expands to the second file name when I put the two set commands at the beginning of the script.
Just for your info, I am using Fedora's bash shell.
 
Any chance you could post your actual script and how you test it ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Here is my test script, transcribed manually, because I do not have the linux box connected to the network yet.

#!/bin/bash
# file name is testpar
echo $2
echo '$2'
echo "$2"
echo ${2}

Then I type
testpar 1 * 2

it gives
calculate
$2
calculate
calculate

Note: calculate is the second file name in the directory.
 
After that, I added the two set lines:

#!/bin/bash
# file name is testpar
set -f
set -o noglob
echo $2
echo '$2'
echo "$2"
echo ${2}

and with the same results.
 
And this ?
(set -f; testpar 1 * 2)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Yes, it works!

Now could you enlighten me as to the difference in putting the set -f command inside and outside the script.

Is there a way I could put it inside to make it work? This way the user does not have to worry about an extra command.

Thanks.
 
Put the set -f line in the user's profile.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Sorry that earlier on I might have misled you by saying that -f does not work. I put it in the first line like
#!/bin/bash -f
and it did not work. It wasn't documented either.
 
I have just put the command set -f in .bashrc and tested it after exiting and login. It still works.

Many thanks to you PHV.
 
In fact the * wasn't expanded by testpar but the interactive shell.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
That explains why I never had a chance to handle it.
Thanks again and good night.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top