Reading UNIX commands from file and redirecting output to a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading UNIX commands from file and redirecting output to a file
# 1  
Old 07-07-2011
Lightbulb Reading UNIX commands from file and redirecting output to a file

Hi All

I have written the following script:
Code:
#!/bin/ksh
while read cmdline
do 
  echo `$cmdline`
  pid="$cmdline"
done<commands.txt

===========
commands.txt contains:
Code:
ps -ef | grep abc | grep xyz |awk '{print $2};

My objective is to store the o/p of the command in a variable and do ceratin operations on it. It works fine if i just ls but not working with pipe...

Last edited by Franklin52; 07-08-2011 at 03:42 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 07-07-2011
If your snipit of code is cut directly from your file you are missing a quote
Code:
ps -ef | grep abc | grep xyz |awk '{print $2};'

If it was just a cut/paste error in your quote, this might also be a better way:

Code:
evail pid=\$( $cmdline )

The eval causes the contents of $cmdline to be expanded, and then the statement is executed which causes the command inside $( ... ) to be executed the the resulting output assigned to pid.

Hope this gets you moving in the right direction.
# 3  
Old 07-08-2011
I tried bt its giving syntax error that '(' is expected i the line:

Code:
eval pid= \$( $cmdline )


Last edited by Franklin52; 07-08-2011 at 03:42 AM.. Reason: Please use code tags for code and data samples, thank you
# 4  
Old 07-08-2011
Space between the equal and backslant is the problem I think:

Code:
eval pid=\$( $cmdline )

There cannot be spaces before or after the equal sign in bash or ksh.

Last edited by agama; 07-17-2011 at 02:15 PM.. Reason: typo
# 5  
Old 07-08-2011
u cn just write: pid=eval "$cmdline" and it will work fine....

Thanks for giving the idea of eval... Its wrking fine nw
# 6  
Old 07-08-2011
I'm curious why you don't just set the script executable and ./script > file ?
# 7  
Old 07-12-2011
Actually i need to use the output in some other script as well as i m getting this input by running other script... Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Redirecting output to file

Hi, I have created script which redirect the output to file.I am able to get the output in file but not in the format. Output :Content of the log which have 10 -15 lines. Actal :Line1 ..Line 2Line3 Line4 Line 5 Expected:Line1 Line 2 Line3 Please... (7 Replies)
Discussion started by: karthik771
7 Replies

2. Homework & Coursework Questions

Help with redirecting output to an HTML file

1. The problem statement, all variables and given/known data: I'm having trouble redirecting the output of my sysinfo_page script into my sysinfo_page.html file. The task at hand is to be able to email both the html file and the script to myself. I'm assuming that the html should appear as a web... (8 Replies)
Discussion started by: braing
8 Replies

3. UNIX for Dummies Questions & Answers

Help with redirecting output to an HTML file

I'm very new to shell scripting and am practicing how to write a script, then redirect the output into an HTML file, and then email both the script and the HTML file to myself. I have created a script called sysinfo_page, and thought it would have redirected the output into the sysinfo_page.html... (3 Replies)
Discussion started by: braing
3 Replies

4. UNIX for Dummies Questions & Answers

Redirecting the multiple commands output to single file

Hi, I am new to shell scripting and have a question. I would like to redirect the output of multple commands to single file, From what I read from the bash manpage and from some searching it seems it cannot be done within the shell except setting up a loop. Is it? I am running all clearcase... (1 Reply)
Discussion started by: saku
1 Replies

5. Solaris

How can I output all previous Unix commands in Solaris to a file??

Hello friends: I login to solaris with a username/Password and I can see quite a lot of previous I use dbefore, it accumulates a lot, I hope to output them into a Command.txt file as reference, not copy/paste 1 by 1, is there any way I can collect all commands in batch then put into a file, ... (3 Replies)
Discussion started by: sunnysunnysunny
3 Replies

6. UNIX for Dummies Questions & Answers

redirecting the script output to more than 1 file

Hi, I want to redirect my script output to more than one file without printing the result to the screen. How to do that? ex: echo "hi" >> a.txt b.txt cat a.txt hi b.txt :confused: (2 Replies)
Discussion started by: boopathyvasagam
2 Replies

7. Shell Programming and Scripting

Redirecting output to file

Hi, Below is the whole string which is to be redirected to the new file. su - oracle -c "exp $user/$pass file=/oracle/oradata/backup/exp_trn_tables_`date +%d_%b_20%y_%H_%M_%S`.dmp log=/oracle/oradata/backup/exp_trn_tables_`date +%d_%b_20%y_%H_%M_%S`.log tables=table1,table2 statistics=none" ... (3 Replies)
Discussion started by: milink
3 Replies

8. Shell Programming and Scripting

Redirecting output of Make to file

Hi, I am unable to get this script to work as desired. Basically, if an argument "log" is sent into the script, it outputs the result of the Make to a file output.log. However, if the argument is not passed, I want the output to be just put on screen (no redirection). See code snippet below. #... (3 Replies)
Discussion started by: srujan45
3 Replies

9. Shell Programming and Scripting

Redirecting command output as well as commands

I have a Bourne Shell script that is normally run as a background job and redirects it's output to a file internally (using exec >>); I use "set -x" to capture each command which provides me with a nice shell execution log if it all goes to pieces. I now also need to be able to also run this as... (4 Replies)
Discussion started by: AncientCoder
4 Replies

10. Shell Programming and Scripting

Redirecting output of a command to a file

Hi We are having a requirement where one shell script, say a.sh (which uses Java and connects to Oracle database using JDBC) keeps on running everytime. I created a wrapper (to check whether a.sh is running and if not then to start it) and scheduled it in the crontab. Now all the output from... (3 Replies)
Discussion started by: ankitgoel
3 Replies
Login or Register to Ask a Question