# Command in a for loop



## bluetick (Feb 4, 2009)

I'm capturing video from security ip cameras. The error logs and video for each delay any return from each command so I have added the & to continue to the next for loop. Running the script from the command line, it works but will not exit to a clean prompt. Thanks for any help, I'm streching the noob bubble =)
capcam ()
{
        rm ${LOGS}/*CAM.log
        for X in ${ADDIES} ;do
        exec ${CAPTOOL} ${CAP_OPT1} http://${X}/mjpg/video.mjpg ${CAP_OPT2} ${STORE}/${X}.$ROT.avi > ${LOGS}/${X}CAM.log &

done
}


----------



## J65nko (Feb 5, 2009)

Why do you use that 'exec'.? sh(1) says:

```
exec [command [arg ...]]
	     Unless command is omitted, the shell process is replaced with the
	     specified program (which must be a real program, not a shell
	     built-in command or function).  Any redirections on the exec com-
	     mand are marked as permanent, so that they are not undone when
	     the exec command finishes.
```
Your 'exec ${CAPTOOL}' replaces the current shell process with ${CAPTOOL}. That is the cause of your problem. Just get rid of that 'exec'. 

An example of the other use of exec, which make the redirects permanent:
	
	



```
$ cat exec.tst
#!/bin/sh

# redirect standard output to file 'output'
exec >output

# redirect standard error to standard out, and
# thus to file 'output'
exec 2>&1 

# --- These statements need no redirects anymore
echo A
echo B
echo C  >&2

$ ./exec.tst
$ cat output
A
B
C
$
```


----------



## bluetick (Feb 6, 2009)

Thanks for the help. 

```
#!/bin/sh -
#Primative IP Camera Capture Script
#Axis 210a Camera
#Use a Cron Job To Control 
ROT=$(date "+%b%d%y%H%M")
CAPTOOL=/usr/local/bin/mencoder
CAP_OPT1="-prefer-ipv4 -fps 6 -demuxer lavf"
CAP_OPT2="-nosound -oac mp3lame -ovc xvid -xvidencopts pass=1 -o"
ADDIES="cam1 cam3 cam4 cam5" # IP must be in hosts
STORE=/camera
ISTORE=/str/backup
LOGS=/var/log
DSPACE=200000
USED=`df -hm $STORE | awk '{print $1}'`
CAM_USED=`du -ms $STORE | awk '{print $1}'`
CAM_MAX=200000
STR_USED=`du -ms $ISTORE | awk '{print $1}'`
STR_MAX=200000
unset SUDO_COMMAND
export MKISOFS=/usr/local/bin/mkisofs
BURNSIZE=4196
DEVICE=/dev/cd1
BURNLIST=$(ls $STORE/*.avi)
GROWISOFS=/usr/local/bin/growisofs
MKISOFS=/usr/local/bin/mkisofs

#send this in cron email
echo cam_used $CAM_USED
echo str_used $STR_USED
capcam ()
{
        rm ${LOGS}/cam*.log
        for X in ${ADDIES} ;do
        ${CAPTOOL} ${CAP_OPT1} http://${X}/mjpg/video.mjpg ${CAP_OPT2} ${STORE}/${X}.$ROT.avi > ${LOGS}/${X}.log &
done
}

cdir  ()
{
        for Y in ${BURNLIST} ;do
        rm $Y
done
}

killall -9 mencoder
sleep 3
if [ $STR_USED -lt $STR_MAX ]
          then
        if [ $CAM_USED -lt $BURNSIZE ]
          then
                capcam
          else
        if ${GROWISOFS} -dvd-compat -Z ${DEVICE} -J -R ${BURNLIST}
          then
                cdir
                capcam
          else
        if ${MKISOFS} -o $ISTORE/${ROT}.iso -R ${BURNLIST}
          then
                cdir
                capcam
          else
                echo System Full
        fi
     fi
   fi
fi
```


----------

