Tag Archive for Bash

EzTube: Playing YouTube Videos without Flash

#! /usr/bin/env bash
#
#    eztube2.sh
#    Playing Youtube Videos without flash, using mplayer.
#    Revised and $(hopefully) better version.
#    ksaver (at identi.ca), Sep 2010.
#    Public Domain Code.
#    Dependences:  (Unix-like OS, python, bash implicit).
#                  Youtube-dl: <a href="http://bitbucket.org/rg3/youtube-dl/" >http://bitbucket.org/rg3/youtube-dl/</a>
#                  Zenity:     <a href="http://freshmeat.net/projects/zenity" >http://freshmeat.net/projects/zenity</a>
#                  MPlayer:    <a href="http://www.mplayerhq.hu/" >http://www.mplayerhq.hu/</a>
#
#    More Info: <a href="http://ur1.ca/0g3gu" >http://ur1.ca/0g3gu</a>

scriptname=$(basename $0)
version='0.2.1'
dependences='zenity mplayer youtube-dl'

#Edit this as needed/
workdir="$HOME/Video"
wait_sec=60
##/

function check_dep()
    {
        for DEPEND in "$@"
        do
            /usr/bin/which $DEPEND &> /dev/null ||
            (echo "[ERR]: Dependence '$DEPEND' Not found in your path!" &&
            return 1)
            
        done
    }

function dl_video()
    {
        /usr/bin/env youtube-dl -q --continue "$1" -o "$2" &
        DLPID="$!"
    }
    
function get_url()
    {
        YTURL=$(zdialog --entry --text='Youtube Video URL:')
    }

function kill_dl()
    {
        PID=$1
        SEC=$2
        /bin/ps   -p  $PID &> /dev/null && waitload $SEC
        /bin/kill -15 $PID &> /dev/null
    }

function play_video()
    {
        /usr/bin/env mplayer -zoom "$1"
    }
    
function saveordrop()
    {
        zdialog --question --text="Save downloaded video?"
    }

function waitload()
    {
     #Not actually checking progress video size, just waits.
        CNT=0
        SEC=$1
        while [[ $CNT -lt $SEC ]]
        do
            echo  $(($CNT*100/$SEC)) #%percent%
            let   CNT=$CNT+1
            sleep 1
            
        done | zdialog --progress --auto-close 
               --text="Loading video...
Please wait about $wait_sec secs."
    }
        
function zdialog()
    {
        /usr/bin/env zenity --title="$scriptname $version" "$@"
    }
    
function _main_()
    {
        check_dep $dependences || exit
        
        YTURL=''
        while [[ ! $YTURL ]]
        do
            get_url || exit
        done         

        VIDNAME="$workdir/$(echo $YTURL |cut -d '&' -f 1 |cut -d '=' -f 2).flv"
        dl_video "$YTURL" "$VIDNAME"

        # If file already exists play it
        if [[ -f $VIDNAME ]]
        then
            play_video "$VIDNAME"
            
        else
            waitload $wait_sec
            play_video "$VIDNAME"
            
        fi
                
        #Save or delete file
        saveordrop
        if [ $? -eq 0 ]
        then
            FILENAME=$(zdialog --file-selection --filename="$VIDNAME")
            
            #wait to download...
            kill_dl $DLPID $wait_sec
            
            #and move downloaded video to new destiny
            /bin/mv "$VIDNAME" "$FILENAME"
            
        else
            #kill download and delete file
            kill_dl $DLPID 0
            if [ -f "$VIDNAME" ]
            then
                /bin/rm "$VIDNAME"
            fi
            
        fi        
    }

#Run script...
while true
do
    _main_
done

source

progress_bar.sh

#  snippet:   progress_bar.sh
#  version:   0.02
#  author:    ksaver (at identi.ca)
#  license:   <a href="http://creativecommons.org/licenses/BSD/" >http://creativecommons.org/licenses/BSD/</a>
#  sets:      progress_bar function
#  arguments: progress_bar function: optional arguments "Text" and width.
#             "Text": Any informative text string.
#             Width:  An integer number, is the max width for the progress bar.
#  usage:     Use into a script:
#             source progress_bar.sh
#             a) progress_bar "Counting:" 25 <<< $PERCENT
#             b) for i in {1..100}; do echo $i; sleep 1; done |progress_bar "" 20
#  output:    Counting: [ ============> ] 50%          

function progress_bar()
{
    while read PERCENT
    do
        COUNT=0
        FOO='>'
        BAR=''
        CHAR='='                 # Change by "#", "|" or any character.
        TEXT=${1:-"Progress:"}   # Sets default value to var $TEXT
        WIDTH=${2:-40}           # Sets default value to var $WIDTH
        
        PROGRESS=$(($PERCENT*$WIDTH/100))
        
        while [ $COUNT -lt $(($PROGRESS+1)) ]
        do  
            printf "
	$TEXT [ %s%s ] $PERCENT%% " ${BAR} ${FOO}
            
            BAR="${BAR}$CHAR"
            COUNT=$(($COUNT+1))
        done
    done
    echo " Done."
}

source

Quick Look From the Command Line

qlmanage -p /PATH/TO/FILE/.txt|pdf|mp3|etc...
qlmanage -t /PATH/TO/FILE/.txt|pdf|mp3|etc...

source

find the largest top 10 files and directories on a Linux / UNIX / BSD filesystem

du -a directory | sort -n -r | head -n 10

source

Sending html from Mutt

mutt -e "my_hdr Content-Type: text/html" -s "my subject" <a href="mailto:you@xxxxxxxxxxx">you@xxxxxxxxxxx</a> < message.html

source

Zip and Unzip using Command Line

# ZIP IT
tar -cf archivename.tar directoryname
gzip archivename.tar

# UNZIP IT
tar xzf archivename.tar.gz

source

Getting Timestamp into BASH Script (or in SSH)

NOW=$(date +'%m.%d.%y at %H:%M:%S')

source

Set SGID bit on directory and subdirectories

find /path/to/directory -type d -exec chmod g+s '{}' ;

source

reading default config file in a bash script

deftwitlogrc="~/.twitlog.rc"

#
# Parse command line
#

me=`basename $0 .sh`
usage="Usage: $me [-c config-file] <message>  

NOTE: config-file defaults to $deftwitlogrc"

while getopts ":c:" options; do
  case $options in
    c ) twitlogrc=$OPTARG;;
    h ) echo -e $usage
        exit 0;;
    ? ) echo -e $usage
         exit 1;;
    * ) echo -e $usage
          exit 1;;
  esac
done  

shift $((OPTIND - 1))

#
# Try and read config file
#
if [ -z "$twitlogrc" ]
then
        source ${HOME}/.twitlog.rc
else
        source $twitlogrc
fi

if [ $? -ne 0 ]
then
        echo "Cannot open config file"
        echo
        echo -e $usage

        exit 1
fi

source

drupal – update core

wget drupal-x.xx.tar.gz; tar -xzf drupal-x.xx.tar.gz; cd drupal-x-xx;
rm -rf sites; rm .htaccess; rm robots.txt; 
cp -r ./* ../;

source