Click to See Complete Forum and Search --> : Shell script, quotes, and variables


Enchantment
02-23-2001, 02:20 PM
I'm still learning the basics of shell scripting so bear with me. I'm trying to create a simple shell script that accepts one input and writes a couple of lines out to my firewall.blocked file. Here's what I have so far.

#!/bin/sh
<stuff>
echo "# Blocked" >> $BLOCKFILE
echo "$IPCHAINS -A input -s $1 -d $MACHINENET -j DENY" >> $BLOCKFILE
<stuff>

This does create the desired information in the file, but not in the format that I want. I would like to see the following in the file
if I call it with 1.1.1.1 as the parm.

# Blocked
$IPCHAINS -A input -s 1.1.1.1 -d $MACHINENET -j DENY

The firewall script knows how to resolve the variables $IPCHAINS and $MACHINENET. I've tried various combinations of " ' and ` but I haven't found the right combo. What should my line of code look like?

Thanks

YaRness
02-23-2001, 02:35 PM
what are you typing on the command line, and what does the result in the blockfile look like? your code looks like it should work just fine.

also, i don't know if there's anything one can do in a shell script to clobber the $1,$2,etc parameters.

Enchantment
02-23-2001, 03:18 PM
On the command line I'm entering "./test1 1.1.1.1"

The file then has the following lines appended to it. (I masked out the desination IP and my test1 script knows about all the variables as well.)
# Blocked
/sbin/ipchains -A input -s 1.1.1.1 -d x.x.x.x/x.x.x.x -j DENY

This is what I had hoped to have appear in the file.
# Blocked
$IPCHAINS -A input -s 1.1.1.1 -d $MACHINENET -j DENY

(We are changing to a different ISP in a few months so I'm trying to minimize the number of changes to be made on the Linux machine)

Energon
02-23-2001, 04:20 PM
example using a diff variable so I don't gotta type out all your stuff:

echo \$PATH

will echo this:

$PATH... ie, the variable name (just escaping the $ symbol)

echo $PATH

will echo the system path... ie, what the variable value is...

Enchantment
02-23-2001, 07:13 PM
Thanks Energon. That solved my problem.