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

.



Relevant Pages

  • Re: csccmd not running in script
    ... I moved the code around a little so that csccmd runs before anything else ... and I get no echo. ... command to complete so we can retrieve the return code. ... you may need to use "Run as Administrator" when you run the script. ...
    (microsoft.public.scripting.vbscript)
  • Re: Why no output display when I input at console
    ... echo running $e: ... There is no output on screen, which is not like that in script file ... After you think you fixed your PATH variable, look at it with the command ...
    (comp.unix.shell)
  • Re: Referencing Korn Shell Array Names as a Variable
    ... Your idea worked for displaying the output from the command. ... $ echo $ ... # Attempt to use variable array name and index to reference array value ... The issue is with this line of the script ...
    (comp.unix.shell)
  • Re: Shell scripts: variable assignment within read loops
    ... things) to obtain information from a command, ... echo $foo ... echo "NICs: $nics" ... the script is to be used to gather ...
    (freebsd-questions)
  • Scripting Help
    ... some of you more experienced script writers ... # WRITING COMMAND PARAMETERS TO TEMP FILE WHILE READING THE ... I have used the temp file as a means to expand the variables as the ... My problem is that when I try to cat utemp5 and pipe it to ksh that I ...
    (comp.unix.aix)