Echoing command results

Sorry folks, Second time today.

I am working on a script that accepts data via pipe and processes it.
I expect it to work as:

# command | ProcScript.sh

Within ProcScript.sh, I want to be able to give the target of the prev run command

I am using history 2 | grep -v history | awk '{print $3}', which works off the command line:

   > cat ios.cfg | ConfProc.sh
   > history 2 | grep -v history | awk '{print $3}'
   ios.cfg
   >

But when I run it in my script, I get errors as follows:
in the script:

echo (`history 2 | grep -v history | awk '{print $3}'`)

Error:

/ConfProc.sh: line 15: syntax error near unexpected token ``history 2 | grep -v history | awk '{print $3}'`
/ConfProc.sh: line 15: `echo (`history 2 | grep -v history | awk '{print $3}'`)

What am I doing wrong?

Thanks!

Try without using the open and close brackets....:b:

try:

echo `history 2 | grep -v history | awk '{print $3}'`

Or the new style:

echo $(history 2 | grep -v history | awk '{print $3}')

you mean in the awk?

I am using parenthesis around the entire command. Should they be brackets?

If not, I did try without the para's but still had the brackets in the awk

Marc

---------- Post updated at 02:28 PM ---------- Previous update was at 02:26 PM ----------

This just gets me an empty answer:

> cat ios.cfg | ConfProc.sh


You have ended the program
Good bye

---------- Post updated at 02:30 PM ---------- Previous update was at 02:28 PM ----------

Same with the other suggestion,
I just get:

> cat ios.cfg | ConfProc.sh


You have ended the program
Good bye

No just like what rdrtx1 has mentioned...

Just run the line like this in your shell script:

history 2 | grep -v history | awk '{print $3}'

what are you trying to achieve?even without a echo it should work..

Tried by removing the brackets from the awk:

echo (`history 2 | grep -v history | awk 'print $3'`)

and got:

> cat ios.cfg | ConfProc.sh
/ConfProc.sh: line 14: syntax error near unexpected token ``history 2 | grep -v history | awk 'print $3'`'
/ConfProc.sh: line 14: `echo (`history 2 | grep -v history | awk 'print $3'`)'
>

===========================
and removing the para's from the statement

echo `history 2 | grep -v history | awk '{print $3}'`

and got

> cat ios.cfg | ConfProc.sh


You have ended the program
Good bye

>

Please try:-

echo "`history | tail -2 | head -1 |  grep -v history | awk ' { print $3 } '`"

And for variable assignment try:-

FILENM=$(echo "`history | tail -2 | head -1 | grep -v history | awk ' { print $3 } '`")

I tried that early on and got no output form the command where, on the command line, I get:
> cat ios.cfg | ConfProc.sh > history 2 | grep -v history | awk '{print $3}' ios.cfg

---------- Post updated at 02:41 PM ---------- Previous update was at 02:39 PM ----------

I even tried using a variable:
FILENM=(history 2 | grep -v history | awk '{print $3}')
echo $FILENM

---------- Post updated at 02:42 PM ---------- Previous update was at 02:41 PM ----------

same results:

> cat ios.cfg | ConfProc.sh


You have ended the program
Good bye

>

---------- Post updated at 02:45 PM ---------- Previous update was at 02:42 PM ----------

when I get rid of the graves quotes,

FILENM=$(echo "history 2 | grep -v history | awk ' { print $3 } '")

It actually prints the command out

> cat ios.cfg | ConfProc.sh
history 2 | grep -v history | awk '{ print  }'

You have ended the program
Good bye

>

---------- Post updated at 02:49 PM ---------- Previous update was at 02:45 PM ----------

sorry,

getting myself confused.

When I get rid of the graves quotes, it looks like this:

echo "history 2 | grep -v history | awk '{ print $3 }'"

and I get:

> cat ios.cfg | ConfProc.sh
history 2 | grep -v history | awk ' { print  } '

You have ended the program
Good bye

>

No you have to use the backticks or $(..) at any cost

`history 2 | grep -v history | awk '{ print $3 }'`

The above command line should give output..But it isn't! its not because you are using it wrongly..but your logic is going wrong..why do you want to print column 3?what it has?

Results:
Option 1:
echo "`history 2 | grep -v history | awk ' { print $3 } '`"

yields:

> cat ios.cfg | ConfProc.sh


You have ended the program
Good bye

>

Option 2:

FILENM=$(echo "`history | tail -2 | head -1 | grep -v history | awk ' { print $3 } '`")
echo $FILENM

yields:

> cat ios.cfg | ConfProc.sh


You have ended the program
Good bye

>

---------- Post updated at 03:00 PM ---------- Previous update was at 02:56 PM ----------

when I cat a file, column three has the name of that file

Can you write out what you mean when you say I have to use back ticks?
Is that with an echo? in a variable assignment? or on the script line

because i tried it as a script line(by itself) and as an echo and that failed

The reason you aren't getting any o/p from history command(though used properly) is, "history" is disabled when used inside the shell scripts...

---------- Post updated at 04:17 PM ---------- Previous update was at 04:11 PM ----------

HISTFILE=~/.bash_history
set -o history
echo `history 2 | grep -v history | awk '{print $3}'`

Include as above...This should work...

I'm not sure that statement is correct, I wrote a small script:-

cat hist.sh

#!/bin/ksh

FN=$(echo "`history | tail -2 | head -1 |  grep -v history`"); echo $FN

Ran it and got the result:-

./hist.sh
1092 cat hist.sh

I tried that as shown:

HISTFILE=~/.bash_history
set -o history
echo `history 2 | grep -v history | awk '{print $3}'`

and got:

> cat ios.cfg | ConfProc.sh


You have ended the program
Good bye

>

I guess that limited to only bash...
History command inside bash script - Unix and Linux

1 Like

when I run on the command line:

> history 2 | grep -v history | awk '{ print $3 }'

I get:

> history 2 | grep -v history | awk '{ print $3 }'
ios.cfg
>

---------- Post updated at 03:28 PM ---------- Previous update was at 03:26 PM ----------

I even tried adding your function to the script and got nothing from the echo

Hey Marc, am sorry for delaying your results..but am also new to this..apologies for that...can i know which shell you are using?
and can you try using bash in your script and see if that helps(if not included previously)

#!/bin/bash