Echoing command results


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Echoing command results
# 8  
Old 10-25-2012
what are you trying to achieve?even without a echo it should work..
# 9  
Old 10-25-2012
Quote:
Originally Posted by msabhi
No just like what rdrtx1 has mentioned...
Tried by removing the brackets from the awk:
Code:
echo (`history 2 | grep -v history | awk 'print $3'`)

and got:
Code:
> 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
Code:
echo `history 2 | grep -v history | awk '{print $3}'`

and got
Code:
> cat ios.cfg | ConfProc.sh


You have ended the program
Good bye

>

# 10  
Old 10-25-2012
Please try:-

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

And for variable assignment try:-

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


Last edited by Yoda; 10-25-2012 at 04:45 PM..
# 11  
Old 10-25-2012
Quote:
Originally Posted by spacebar
Just run the line like this in your shell script:
Code:
history 2 | 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 ----------

Quote:
Originally Posted by Marc G
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

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 ----------

Quote:
Originally Posted by bipinajith
Please try:-

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

same results:
Code:
> 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 ----------

Quote:
Originally Posted by bipinajith
Please try:-

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

And for variable assignment try:-

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

when I get rid of the graves quotes,
Code:
FILENM=$(echo "history 2 | grep -v history | awk ' { print $3 } '")

It actually prints the command out
Code:
> 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:

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

and I get:

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

You have ended the program
Good bye

>

# 12  
Old 10-25-2012
No you have to use the backticks or $(..) at any cost
Code:
`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?
# 13  
Old 10-25-2012
Quote:
Originally Posted by bipinajith
Please try:-

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

And for variable assignment try:-

Code:
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:
Code:
> cat ios.cfg | ConfProc.sh


You have ended the program
Good bye

>

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

yields:
Code:
> 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 ----------

Quote:
Originally Posted by msabhi
No you have to use the backticks or $(..) at any cost
Code:
`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
# 14  
Old 10-25-2012
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 ----------

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

Include as above...This should work...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find with rm command gives strange results

I want to remove any files that are older than 2 days from a directory. It deletes those files. Then it comes back with a message it is a directory. What am I doing wrong here? + find /mydir -mtime +2 -exec rm -f '{}' ';' rm: /mydir is a directory (2 Replies)
Discussion started by: jtamminen
2 Replies

2. Shell Programming and Scripting

Cannot get results from grep command

Hi, i have a file hello.log which as several line that look like the below 2015-12-07 09:46:56 0:339 120.111.12.12 POST /helloWorld 2015-12-07 09:46:57 0:439 122.111.12.12 POST /helloWorld .... when i grep expecting to see results like the below. ... (6 Replies)
Discussion started by: mohtashims
6 Replies

3. Shell Programming and Scripting

echoing complex awk command into file fails

Using hp-ux's shell, I'm trying to echo a complex awk command into a script file for later use. But it fails on a newline character and splits the rest of the command onto the next line. echo ' printf("%s: TOTAL = %18.0lf\n", FILENAME, TOTAL) >> "TOTAL.TXT";' >>awk.script Looks... (3 Replies)
Discussion started by: Scottie1954
3 Replies

4. Shell Programming and Scripting

echoing date command into file

I want to echo into file1 echo & date commands, which in turn will be echoed into file2 string and the current date. So when I'll run file1 it will echo into file2 the commands 'echo' & 'date' My problem is that the date command turns into the actual date value. Example:... (2 Replies)
Discussion started by: liav
2 Replies

5. UNIX for Dummies Questions & Answers

Acting on results from a grep command

Hi, I am currently reading a tar file and searching for a particular word using grep e.g. Plane. At the moment, if a sentence is found with the word "Plane" the sentence itself is piped to another file. Here is the code i am using; for jar in 'cat jar_file.tar'; do tar -tvf... (3 Replies)
Discussion started by: crunchie
3 Replies

6. Shell Programming and Scripting

SSH with and without command gives different results

works as expected $ ssh 172.24.40.100 Last login: Mon Jan 1 06:07:24 2001 from 172.24.41.78 # /path/script.sh gives me error consistent with env setup $ ssh 172.24.40.100 /path/script.sh Which implies the latter is running the script.sh on host a, when I want to 'launch' in from a, and... (3 Replies)
Discussion started by: IanVaughan
3 Replies

7. UNIX for Dummies Questions & Answers

df and du command showing different results

I recently encountered this on the AIX system df command showed usage is 100% i.e 1.5 GB while du command showed usage is only 500MB Why are the 2 commands showing different output This command shows usage is 1.5 GB nlxdsm29:deqadm 24> df -k . /usr/sap/DEQ ... (3 Replies)
Discussion started by: ameya_joshi
3 Replies

8. Shell Programming and Scripting

getting results after using ps command

Hi, I want to use the following ps coomand: ps -ef | grep test Result of this command is: Test 161220 1 0 Oct 04 - 1:11 /test/test Just mentioning the description of each value in the result: UID PID PPID C STIME TTY TIME CMD Test 161220... (11 Replies)
Discussion started by: yale_work
11 Replies

9. Shell Programming and Scripting

joining command results, and substitution

Hello community I'd like to join to command results and put it to the same line in one file, how can I do that? file: a.txt so when I put Date '+%H:%M' and echo date '+%D' in the file appears 14:44 01/05/08 not 14:44 01/05/08 I like to know how can I make a substituion of a whole... (6 Replies)
Discussion started by: ncatdesigner
6 Replies

10. UNIX for Dummies Questions & Answers

Setting the Results of a Command to a Variable

Hi, Hi, I run the command: hostname to get the host back from the server: db201 Now, I need to take that result and set it to a variable. Can anyone help me with this?? I need to be able to use the same script on multiple servers so I do not want to hardcode the hostname result into... (1 Reply)
Discussion started by: stky13
1 Replies
Login or Register to Ask a Question