Ok, I have tried figuring out as much as I could on my own, but this is not as obvious as I hoped. I am attempting to script some tasks I have to do, but I have no control over one of the scripts I have to use... and they output all kinds of useless things on the screen.
My goal is simple: Capture all output from their scripts, and create a progress line that only shows the most recent output from their stuff.
So, here was my first solution; a file I called "spin":
Code:
#!/bin/bash
spinX(){
PROC=$1
STRT=`date "+%s"`
while [ -d /proc/$PROC ];do
last3="`getLine $2`"
echo -en "\r" "<(''<) $last3";sleep 0.3
last3="`getLine $2`"
echo -en "\r" "<(' ')> $last3";sleep 0.3
last3="`getLine $2`"
echo -en "\r" " (>'')> $last3";sleep 0.3
last3="`getLine $2`"
echo -en "\r" "<(' ')> $last3";sleep 0.3
done
END=`date "+%s"`
Finished=`printf "%02d:%02d:%02d" $((($END-$STRT)/3600)) $((($END-$STRT)/60%60)) $((($END-$STRT)%60))`
echo -en "\r" "(^-^) Done after [$Finished]";
echo
}
getLine(){
tmp=`tail -1 $1`
if [ "$tmp" != "" ];
then echo "[$tmp]";
echo "$tmp" >$1;
else echo "";
fi;
}
spinX $1 $2
To use it, you pass it a process ID and a file that contains the output from that process. As the process continues, a kurby dances on the screen (To let you know that the process has not hanged), and the tail of the output is shown (To let you know what it is doing). When the process ends, the kurby stops dancing and the time it took is displayed.
And here is the file I call "noise":
Code:
#!/bin/bash
while [ i -lt 100 ];do
i=1
echo "Look at me count!$"
sleep 1
let "i=$i+1"
done
This does nothing but create random output, for testing. It counts from 1 to 99 on the screen.
To run my test, I do the following:
Code:
( noise ) &>tmp.txt & spin $! tmp.txt
It works relatively well, but it is messy. I don't like creating a temp file, and I don't like the messy syntax for calling my program. I decided that I would rather move everything into the spin program, to make using it less messy:
Code:
#Spin Psuedo code
#$1 = command I am about to run
(exec $1) &>tmp.txt & spinX $! tmp.txt
By executing the process inside of the spin code, I can get rid of the tmp file later on without changing a lot of scripts (Or move it, or whatever). I can also call it by passing the command to the script, which I find more elegant.
So here is what I would like to know:
1) If possible, I would love to get rid of the tmp file all together, and store the most recent line of output from script 1 into a variable that script 2 can print out instead... is it possible?
2) How can I run a random command that is passed as an argument? Basic ones work fine, but anything with a pipe fails me.
Example of a script:
Code:
#!/bin/bash
#myEcho.sh
echo;echo "Recieved command: ";echo $1;echo;
echo "Attempting to run command: ";echo
exec $1
Example code for passing commands to script:
Quote:
> myEcho.sh "ls -al" #works
> myEcho.sh 'ls -al' #works
> myEcho.sh "ls -al|grep *.sh" #fail
# Output:
#ls: invalid option -- |
#Try `ls --help' for more information.
> myEcho.sh "ls -al|grep \"*.sh\"" #fail
# Output:
#ls: invalid option -- |
#Try `ls --help' for more information.
> myEcho.sh 'ls -al|grep *.sh\' #fail
# Output:
#ls: invalid option -- |
#Try `ls --help' for more information.
|
Anyone have ideas?