# problem getting ipfw output



## networker_ (Nov 28, 2011)

Hi

I'm in trouble, I want to get the output of [cmd=]ipfw show[/cmd] and write it to a plain file, I'm trying it with this:


```
#!/bin/sh
VAR=`ipfw show|grep count|grep in|awk '{print $3}'`
echo $VAR >> file
```

The strange thing is that if I run it manually (*$ sh script*) it works, but if I *cron* the script each minute my file just receives a blank line without the number that I need.


----------



## SirDice (Nov 28, 2011)

Keep in mind that cron uses a limited PATH. Use the fully qualified names for commands. Like /sbin/ipfw instead of just ipfw.


----------



## networker_ (Nov 28, 2011)

SirDice said:
			
		

> Keep in mind that cron uses a limited PATH. Use the fully qualified names for commands. Like /sbin/ipfw instead of just ipfw.



I've done it, in my cron I have:


```
*/1 * * * * /bin/sh /usr/local/www/data/script
```

and in the sccript


```
#!/bin/sh
IPF=`which ipfw`
VAR=`$IPF show | grep count | grep in |awk '{print $3}'`
```
and the behaviour is the same.


----------



## kpa (Nov 28, 2011)

The ipfw(8) command is writing its messages to stderr so you need to write your pipelines with redirections like this:
`# ipfw show 2>&1 | grep ...`


----------



## networker_ (Nov 28, 2011)

kpa said:
			
		

> The ipfw(8) command is writing its messages to stderr so you need to write your pipelines with redirections like this:
> `# ipfw show 2>&1 | grep ...`



It didn't work for me

My crontab:

```
*/1 * * * * /bin/sh /usr/local/www/data/script
```

My script:

```
#!/bin/sh
CMD=`which ipfw`
INDATA=`$CMD show 2>&1 | grep count | grep in | awk '{print $3}'`
echo "$INDATA" >> file
```

My /etc/crontab:

```
SHELL: /bin/sh
PATH:..:/bin:...
```

Any comment?


----------



## DutchDaemon (Nov 28, 2011)

networker_, format your posts!


----------



## phoenix (Nov 29, 2011)

For sh(1)-based shells:

```
/sbin/ipfw show >> /path/to/file.txt 2>&1
```

*>>* sends standard output to /path/to/file.txt
*2>&1* sends standard error (2) output to the same location as standard (1) output.


----------



## networker_ (Nov 29, 2011)

phoenix said:
			
		

> For sh(1)-based shells:
> 
> ```
> /sbin/ipfw show >> /path/to/file.txt 2>&1
> ...



heyyy, many thanks, you helped me a LOT !

but it was strange, because i didn't need to use *2>&1*, but in your reply you posted a detail, the ".txt", so what I did is:


```
VAR=`/sbin/ipfw show | grep count | grep in | awk '{print $3}'`
echo "$VAR" >> /path/file.txt
```

and it worked fineeee

thanks !

PD: smallest details can made a difference
PPD: I formated my reply 

-------------------------

sadly I have to edit

it works just for one single line

my idea is to do:

```
#!/bin/sh
CMD=`which ipfw`
INDATA=`$CMD show 2>&1 | grep count | grep in | awk '{print $3}'`
OUTDATA=`$CMD show 2>&1 | grep count | grep out | awk '{print $3}'`
echo "$INDATA" >> file 2>&1
echo "$OUTDATA" >> file 2>&1
```


----------

