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!
msabhi
October 25, 2012, 3:23pm
2
Try without using the open and close brackets....
rdrtx1
October 25, 2012, 3:24pm
3
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
msabhi
October 25, 2012, 3:32pm
6
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}'
msabhi
October 25, 2012, 3:35pm
8
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
>
Yoda
October 25, 2012, 3:38pm
10
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 } '`")
Marc_G1
October 25, 2012, 3:49pm
11
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 ----------
Please try:-
echo "`history 2 | grep -v history | awk ' { print $3 } '`"
And for variable assignment try:-
FILENM=$(echo "`history 2 | grep -v history | awk ' { print $3 } '`")
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
>
msabhi
October 25, 2012, 3:51pm
12
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?
Marc_G1
October 25, 2012, 4:00pm
13
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 } '`")
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 ----------
msabhi:
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?
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
msabhi
October 25, 2012, 4:17pm
14
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...
Yoda
October 25, 2012, 4:18pm
15
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
Marc_G1
October 25, 2012, 4:23pm
16
msabhi:
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 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
>
msabhi
October 25, 2012, 4:25pm
17
I guess that limited to only bash...
History command inside bash script - Unix and Linux
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
1 Like
Marc_G1
October 25, 2012, 4:28pm
18
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
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
msabhi
October 25, 2012, 4:31pm
19
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