Posts Tagged ‘Bash’

Pseudothreading with BASH

January 20, 2010

It’s more like a trick, but it’s better than repeat the same operation linearly. Some explanations:

TH_NUM=`ps aux | grep Python | grep -v "grep" | wc -l`

  • TH_MAX is the maximum number of “threads” that can be executed at the same time.
  • The first grep selects the threads that make use of python (you can change this, it depends on your script)
  • The second grep excludes the command you issued above 😉
  • wc counts the number of lines. The first time the result of the pipe is empty, so wc gives “0” as result.

#!/bin/bash
TH_MAX=10
for sample in `ls ./data`
do
while [ TRUE ]; do
TH_NUM=`ps aux | grep Python | grep -v "grep" | wc -l`
if [ "$TH_NUM" -le "$TH_MAX" ]
then
echo $( ./analyze_sample.py -s ${sample} ) > /dev/null &
echo -en " ${sample} "
break
else
echo -en "."
sleep 1
fi
done
done

Advertisements

Sage to Google Reader OPML converter

September 24, 2008

For some reason, when exporting rss feeds from Sage, a Firefox plugin, to an OPML file it does not save the feed (i.e. the xml/rss/atom/whatever) link, but the feeds themselves, i.e. the content of the feeds.

If you have the same problem, here’s a fast-and-dirty solution.

  1. export your Firefox bookmarks in HTML format, in a file called, say, bookmarks.html
  2. using a bash shell, extract the feed lines into a file called feeds.xml doing
     grep FEEDURL bookmarks.html > feeds.xml
  3. create the following script called “scriptfeed.sh”
    #!/bin/bash
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
    echo "<opml version=\"1.0\">"
    echo "<head><title>RSS Subscriptions</title></head>"
    echo "<body>"
    while read line
    do
            temp=`echo $line | awk -F"<" {'print $3;'}`
            feed=`echo $temp | awk -F"\"" {'print $2;'}`
            url=`echo $temp | awk -F"\"" {'print $4;'}`
            text=`echo $temp | awk -F">" {'print $2;'}`
            echo "<outline text=\"$text\""
            echo "  title=\"$text\""
            echo "  type=\"rss\""
            echo "  xmlUrl=\"$feed\""
            echo "  htmlUrl=\"$url\"/>"
    done < feeds.xml
    echo "</body>"
    echo "</opml>"
  4. Give the script the right to execute
    chmod +x scriptfeed.sh
  5. Launch the script on the output file, say, exp.xml
    ./scriptfeed.sh > exp.xml

I know, it’s dirty and tricky, but it works 😛

Remove all binaries from a directory tree

June 4, 2008

Here is a one liner that packs some serious bash punch:

rm `find . -type f -exec file ‘{}’ \; | grep -i linux | awk -F: ‘{print $1}’`