I am one of those people that loves the command line. I like having the ability to write a script that allows me to execute something in a loop. Some programs are not very loop friendly. Here are some examples:
SSH
As a system administrator, you find yourself needing to execute a command on a bunch of servers. If you start out with a simple for loop with an ssh inside, you notice something weird. Only the first loop executed. It took me a while to figure out how to overcome this problem. The ssh program is a special program. It does special things with stdin to transfer control of it to the remote server. The problem with that is it consumes all of stdin inside of the loop. This means it consumes the contents of the for loop. I don't fully understand what is going on under the hood, but the for loop uses stdin. After ssh consumes stdin, there is no more entries to loop over. The simple solution is to echo into ssh. This gives ssh its own stdin to consume.
# Loop over hosts
for x in server1 server1
do
# Give ssh its own stdin
echo | ssh server1 hostname
done
Mplayer
Recently, I have been trying to convert my AVI files to MP4 files. I have been matching bitrates between the 2 containers/codecs. I usually use mplayer's identity flag to detect the bitrate of the source file. That worked when called once, but inside of a loop, it caused problems. Mplayer was corrupting stdin. I tried the echo trick, but that didn't work. Apparently, when you echo into mplayer, it acts differently. Instead of identifying the input file, it identified the input stream in stdin. When you are just echo'ing into mplayer, it just told me that it get an EOF. I couldn't get passed this problem. To get passed this problem, I ran ffmpeg against the input file, without giving an output file. The program errored out, but it gave the bitrate of the old filename in the error message. This is not repeatable, so I know this script won't last the test of time.
FFMpeg
I am using ffmpeg to convert from AVI to MP4. An interesting thing happened when executing ffmpeg (with an output specified) in a loop. FFMpeg displayed less output, but the x264 command it was running dumped the contents of the output file in hex format to stdout. The conversion was still happening, but GNU Screen was taking up 50% of one of my cpu's just to handle the stdout. I am sure the conversion was happening slower than normal as well. I couldn't get passed this problem either. I decided to convert my script to a bunch of echo's. I echo'ed the ffmpeg command instead of executing it. I redirected the output of the script to a file. Now I have a huge Bourne shell script that I could execute. That generated script contains no loops, so it runs just fine.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.