Unix Technical Forum

redirect for stdout in ${command_line}

This is a discussion on redirect for stdout in ${command_line} within the AIX Operating System forums, part of the Unix Operating Systems category; --> Greetings! I want to build and submit a command from within ksh to initiate a nohup background process, and ...


Go Back   Unix Technical Forum > Unix Operating Systems > AIX Operating System

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-05-2008, 10:09 AM
Robin
 
Posts: n/a
Default redirect for stdout in ${command_line}

Greetings!

I want to build and submit a command from within ksh to initiate a
nohup background process, and redirect the stdout to another file...

I'm having trouble with the way ">" gets parsed when using ${...}

Either the line gets broken into two lines (if using "\")...

....or the ">" does not get recognized as a redirect, but rather as part
of the command invoking the executable (ERROR: Unknown command line
option >)...

Any help would be greatly appreciated...

Regards;

--
_______________________________
Robin Arthur Gustavson
Affiliated Computer Services, Inc.
TSG / Filenet / Dallas
Robin.Gustavson@acs-inc.com
214-584-5795
142*784*229

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-05-2008, 10:09 AM
steven_nospam at Yahoo! Canada
 
Posts: n/a
Default Re: redirect for stdout in ${command_line}

Robin wrote:
> Greetings!
>
> I want to build and submit a command from within ksh to initiate a
> nohup background process, and redirect the stdout to another file...
>
> I'm having trouble with the way ">" gets parsed when using ${...}
>
> Either the line gets broken into two lines (if using "\")...
>
> ...or the ">" does not get recognized as a redirect, but rather as part
> of the command invoking the executable (ERROR: Unknown command line
> option >)...
>
> Any help would be greatly appreciated...


I don't know if this helps you or not, but when building a script that
will create its own workfile script, you need to consider how the
character is going to be translated in the line. For example, any
quotes must be preceded by a backslash to indicate that you want the
quote to stay in the line when it is written to the new executable you
are creating.

Likewise, if I use \n to add a carriage return or newline, the slash
will first be translated by your calling script so you need two in a
row to get it to show properly in the generated script. So \n becomes
\\n instead - you may need to do the same for your redirection
characters, although I had no problems just as long as they were in
quotes.

Here is a sample:

#!/bin/ksh
#
# Sample script that builds another script and runs it
#
echo "Main script - Starting Build..."
echo "echo \"\\nListing of current directory:\" > /tmp/sample.list" >
/tmp/sampl
e.script.$$
echo "ls -l >> /tmp/sample.list" >> /tmp/sample.script.$$
chmod a+x /tmp/sample.script.$$

echo "Main script - running sample script"
.. /tmp/sample.script.$$

echo "Main script - completed - show created files:"
ls -l /tmp/sample.script.$$ /tmp/sample.list

sleep 2

echo "Main script - deleting created files"
/bin/rm /tmp/sample.script.$$ /tmp/sample.list

# END OF SCRIPT #


#SAMPLE RUN #
MyServer> chmod a+x test.script
MyServer> test.script
Main script - Starting Build...
Main script - running sample script
Main script - completed - show created files:
-rw-rw-r-- 1 root system 1864 Aug 08 13:40
/tmp/sample.list
-rwxrwxr-x 1 root system 83 Aug 08 13:40
/tmp/sample.script.65022
Main script - deleting created files

If you were to indicate what your ultimately trying to do, you may get
some other responses that provide a better way to do this. Someone may
know how to accomplsih your goal without having to use a script that
gets built on the fly...

HTH

Steve

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-05-2008, 10:09 AM
Robin
 
Posts: n/a
Default Re: redirect for stdout in ${command_line}

steven_nospam at Yahoo! Canada wrote:
> If you were to indicate what your ultimately trying to do, you may get
> some other responses that provide a better way to do this.


Thanks, Steve...

Here is a piece of what I am trying to execute...

#
cmd_1='nohup /fnsw/local/bin/NLS_Archive -f
/tmp/ssar_conv_cmds/surface'
cmd_2='.txt '
cmd_3=' /tmp/ssar_conv_cmds/surface'
cmd_4='.out &'
command_line=$cmd_1$surf_in$cmd_2\ > \$cmd_3$surf_in$cmd_4
${command_line}
#


I'm going to keep tinkering...

Thanks!

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-05-2008, 10:09 AM
steven_nospam at Yahoo! Canada
 
Posts: n/a
Default Re: redirect for stdout in ${command_line}


Robin wrote:
> Here is a piece of what I am trying to execute...
>
> #
> cmd_1='nohup /fnsw/local/bin/NLS_Archive -f
> /tmp/ssar_conv_cmds/surface'
> cmd_2='.txt '
> cmd_3=' /tmp/ssar_conv_cmds/surface'
> cmd_4='.out &'
> command_line=$cmd_1$surf_in$cmd_2\ > \$cmd_3$surf_in$cmd_4
> ${command_line}
> #


Hi Robin,

I did a similar test and I see what you are probably running into. Here
is what I tried:

# START OF SCRIPT #
#!/bin/ksh
#
echo "Enter surf_in value: \c"
read surf_in

command_line="nohup /bin/ls /dev/$surf_in* 1>/tmp/devices.$surf_in 2>&1
&"

echo "Command line is shown below:"
echo "${command_line}"

${command_line}

sleep 3
echo "Results:"
ls -l /tmp/devices.$surf_in
cat /tmp/devices.$surf_in
# END OF SCRIPT#

This script was supposed to simulate what you are doing with the
NLS_Archive. I would accept a variable and build a command line while
inserting the variable at certain points. My script was simply going to
list device names in the /dev directory.

For surf_in, I entered "tty" as what I wanted to search for. When I ran
it I got the following:

Enter surf_in value: tty
Command line is shown below:
nohup /bin/ls /dev/tty* 1>/tmp/devices.tty 2>&1 &
Sending nohup output to nohup.out.
Results:
ls: 0653-341 The file /tmp/devices.tty does not exist.
cat: 0652-050 Cannot open /tmp/devices.tty.

When I checked the nohup.out that gets created, here are the contents:

ls: 0653-341 The file > does not exist.
ls: 0653-341 The file /tmp/devices.tty does not exist.
ls: 0653-341 The file 2>&1 does not exist.
ls: 0653-341 The file & does not exist.
/dev/tty
/dev/tty0
/dev/tty1
/dev/tty2
/dev/tty3

What it appears to be doing is treating the redirection and even & at
the end as a parameter passed to the ls command instead of as the
redirection it was supposed to be.

I believe that you can prevent that from happening by writing the
command line you are trying to execute to a temporary file and then
nohup'ing that file. Of course, that means that you will have temp
files to clean up afterward. Here is how I got it to work:

# START OF SCRIPT #
#!/bin/ksh
#
echo "Enter surf_in value: \c"
read surf_in

echo "/bin/ls /dev/$surf_in* 1>/tmp/devices.$surf_in 2>&1" >
/tmp/robin.$$
echo "/bin/rm /tmp/robin.$$" >> /tmp/robin.$$

echo "Temp file contents are shown below:"
cat /tmp/robin.$$

chmod a+x /tmp/robin.$$
nohup /tmp/robin.$$ &

sleep 3
echo "Results:"
ls -l /tmp/devices.$surf_in /tmp/robin.$$
cat /tmp/devices.$surf_in
# END OF SCRIPT#


And the results were:

Enter surf_in value: tty
Temp file contents are shown below:
/bin/ls /dev/tty* 1>/tmp/devices.tty 2>&1
/bin/rm /tmp/robin.85604
Sending nohup output to nohup.out.
Results:
ls: 0653-341 The file /tmp/robin.85604 does not exist.
-rw-rw-r-- 1 root system 236 Aug 09 10:49
/tmp/devices.tty
/dev/tty
/dev/tty0
/dev/tty1
/dev/tty2
/dev/tty3
/dev/tty3p
/dev/ttyp0
/dev/ttyp1
/dev/ttyp2
/dev/ttyp3
/dev/ttyp4
/dev/ttyp5
/dev/ttyp6
/dev/ttyp7
/dev/ttyp8
/dev/ttyp9
/dev/ttypa
/dev/ttypb
/dev/ttypc
/dev/ttypd
/dev/ttype
/dev/ttypf

The error from the final ls command shows me that my temp file in fact
did get deleted by the rm command within that temp script. So it cleans
up after itself.

HTH

Steve

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-05-2008, 10:10 AM
Robin
 
Posts: n/a
Default Re: redirect for stdout in ${command_line}


steven_nospam at Yahoo! Canada wrote:
>
> I believe that you can prevent that from happening by writing the
> command line you are trying to execute to a temporary file and then
> nohup'ing that file.


Heh... great minds and all that... that's exactly what I did.. gave up
trying to be elegant and just did what works...!!


print $cmd_1$surf_in$cmd_2">"$cmd_3$surf_in$cmd_4 > run_file
chmod 755 run_file
./run_file

....thanks, steven...!!

--
r.a.g.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 08:46 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com