Introducing… *drum rollz*

The One Script to rule them all… :lol:

Updated today Nov. 17, 2011

This post will be the natural successor to this one.

## Source of inspiration.
/etc/init.d/SCRIPT -> /lib/init/upstart-job

I took the liberty to recombine my three most used scripts into one. ;-)
Its usage is simple:

## The scale and quote scripts take additional parameter(s).
## For 'scale': 4cif, pal or hd are the only options at the moment.
## But feel free to add your own.
## All others: script.name file.name
you@somewhere:~$ ./scale.avi name-of-your-movie.avi (4cif, pal, hd)
e.g.: ./scale.avi name-of-your-movie.avi hd # Upscale file to "$HD" i.e. 1280x720
you@somewhere:~$ ./quote.avi name-of-your-movie.avi (Start End (Either in whole seconds or HH:MM:SS.MSS))
e.g.: ./quote.avi name-of-your-movie.avi 30 60 # Starts at 30s and has a duration of 60s.
e.g.: ./quote.avi name-of-your-movie.avi 60 # Starts at 0s and has a duration of 60s.
you@somewhere:~$ ./xtract.z.avi name-of-your-movie.avi
## Or: (if the script is within your "$PATH"?
scale.avi name-of-your-movie.avi hd # Upscale file to "$HD" i.e. 1280x720
quote.avi name-of-your-movie.avi 30 60 # Starts at 30s and has a duration of 60s.
quote.avi name-of-your-movie.avi 59,04 # Starts at 0s and has a duration of 59.040s.
xtract.z.avi name-of-your-movie.avi
xtract.z.flv name-of-your-movie.flv # I.e. Youtube or equiv. ; - )
## 'z'? equals 'from' ; - )

As for one’s “$PATH”.

# http://linuxcommand.org/wss0010.php
## His Pah is very strong. : - )
## Insert the following in your ~/.bashrc
## Preferably at the end!
## What I use.
# PATH=$PATH:$HOME/.usr/bin
PATH=$PATH:$HOME/your/folder/of/choice
export PATH
## Then all scripts you create and which are placed in "$PATH"
## Can be executed from anywhere you have permission to execute those.
#
## Otherwise you'll have to 'cd' to the folder where your script lives and
## do: ./your-script with-whatever-options-you-have-concocted with-file.ext
## Oh, don't forget to do: chmod u+x on your script first.

In my enthusiasm to post this asap I forgot to mention that for one this script allows you to copy and paste directly from e.g. nautilus. :-)

But (what I as well forgot to mention):
Your folders and files will be RENAMED if either of those contains space(s)!!! If you don’t want that to happen then simply renaming your file(s), and if applicable your folder(s), before running this script should do the trick. ;-)

## When copied from nautilus:
## From:
file:///your/file/at%20some%20where/with%20nasty%20spaces.ext
## To:
/your/file/at.some.where/with.nasty.spaces.ext
## When using tab completion in your shell.
## Type and, if exist, tab until completion.
## From:
/your/file/at\ some\ where/with\ nasty\ spaces.ext
## To:
/your/file/at.some.where/with.nasty.spaces.ext

You might just want to save the following few lines just below for posterity’s sake!? ;-) Why? Well… In order for the script posted on this page to work you’ll have to embrace the concept of linking!

## You might just want to save the following few lines just below for posterity's sake. ; - )
SCRIPT="$(basename "$0")"
for FILE in quote.o{ga,gg,gm} quote.flac quote.avi quote.m{p2,p3,p4,pe,peg,pg,kv} quote.w{mv,av};do ln -s -T "${SCRIPT}" "$(echo "${FILE}")";done
for FILE in scale.o{gg,gm} scale.flac scale.avi scale.m{p4,pe,peg,pg,kv} scale.wmv;do ln -s -T "${SCRIPT}" "$(echo "${FILE}")";done
for FILE in xtract.z.o{gg,gm} xtract.z.avi xtract.z.flv xtract.z.m{p4,pe,peg,pg,kv} xtract.z.wmv;do ln -s -T "${SCRIPT}" "$(echo "${FILE}")";done

Part 1. Its head.

#!/bin/bash
# Modified: Today by E.L.F.
#
## Licensed as:
## http://www.opensource.org/licenses/mit-license.php
#
## Script-name - skel.sh
#
set -e
if [ "$USER" = root ];then
  echo -e ${RED}"Are you Insane!"${NC}
  echo -e ${RED}"Error: In order to use this script, one must NOT be "$USER""${NC}
  echo -e ${YELLOW}"Exiting..."${NC}
  exit 0
else
  echo ""
  echo -e ${BLUE}"    "$USER" may proceed."${NC}
  echo -e ${CYAN}"    May peace be with you."${NC}
fi
clear
for COMMAND in mencoder ffmpeg oggenc cat
do COMMAND=$(which "$COMMAND")
if [ ! -x "$COMMAND" ];then RESULT=NO
echo -e ${YELLOW}"    Please install "${COMMAND}" first before continuing.${NC}
${CYAN}sudo apt-get install "$(basename "${COMMAND}")""${NC};fi
done
if [ "$RESULT" = NO ]; then echo -e ${BLUE}"See (for example):${NC}${YELLOW} http://ubuntuforums.org/showthread.php?t=766683${NC}${BLUE} for more info on the matter."${NC};exit 0;fi
## Variables for the 'scale' command.
date="$(date +"%A, %B %-d, %Y")"
copy=$(echo "Up to you.")
publisher=$(echo "Up to you.")
UPS_EXT=.mkv
HD=1280x720
PAL=720x576
_4CIF=704x576
## Variables for the 'quote' command.
## If 'START' is 'empty' then.
ZERO=00:00:00.000
## If 'END' is 'empty' then.
HALF=00:00:30.000
## Variables for the 'xtract.z' command.
## Print working directory.
PWD=$(pwd)
## Scriptname.
SCRIPT="$(basename "$0")"
## 'basename' of the script.
BASE="$(basename ${SCRIPT%.*})"
## Its 'extension'.
EXT="${SCRIPT/${BASE}/}"
## End variables!
## What would you have your 'arbiter' do.
for OPTION in "${BASE}";do OPTION="${OPTION}";done
for CHOICE in "${EXT}";do CHOICE="${CHOICE}";done
#  ## When no argument is given, print its usage.
  if [ "$BASE" = scale ];then USAGE=$(echo "Usage: "${SCRIPT}" yourfile"${EXT}" 4cif, pal, hd")
  elif [ "$BASE" = quote ];then USAGE=$(echo "Usage: "${SCRIPT}" yourfile"${EXT}" Start End (Either in whole seconds or HH:MM:SS.MSS)")
  else USAGE="Usage: "${SCRIPT}" yourfile"${EXT}"";fi
  if [ "$#" == "0" ];then echo -e ${YELLOW}"$USAGE"${NC};exit 0;fi

Part 2. Its body part 1.

case "$OPTION" in
"cat" | "create")
  if [ "$#" == 1 ];then OUTPUT="$1";OUTPUT+="${EXT}";else echo -n "Enter filename: ";read OUTPUT;OUTPUT+="${EXT}";fi
  echo "$OUTPUT"
  case "$OPTION" in
  "cat")
    case "$CHOICE" in
    ".avi" | ".vob" | ".wmv")
    # LS=$(ls *.{avi,mp{e,eg,g},wmv} 2>/dev/null)
    LS=$(ls *"${EXT}" 2>/dev/null)
    echo "$LS"
    mencoder -forceidx -oac copy -ovc copy $(ls *.avi | sort -n) -o "$OUTPUT"
    mv "$OUTPUT" ../"$OUTPUT"
    \rm *"${EXT}" 2>/dev/null
    mv ../"$OUTPUT" .
    ;;
    ".mpg")
    VAR="mpg-files.txt"
    ls *.mp{e,eg,g} 2>/dev/null | sort > $VAR
    cat $VAR | while read line; do
    INPUT=$(echo ${line})
    cat "$INPUT" >> "$OUTPUT"
    done
    \rm $VAR
    mv "$OUTPUT" ../"$OUTPUT"
    \rm *.mp{e,eg,g} 2>/dev/null
    mv ../"$OUTPUT" .
    ;;
    esac
  ;;
  "create")
    echo "#EXTM3U" > "$OUTPUT"
    find -iname '*.ogg' -exec ls "{}" \; | sort -n >> "$OUTPUT"
    find -iname '*.mp2' -exec ls "{}" \; | sort -n >> "$OUTPUT"
    find -iname '*.mp3' -exec ls "{}" \; | sort -n >> "$OUTPUT"
    find -iname '*.mp4' -exec ls "{}" \; | sort -n >> "$OUTPUT"
    find -iname '*.mpg' -exec ls "{}" \; | sort -n >> "$OUTPUT"
    find -iname '*.wma' -exec ls "{}" \; | sort -n >> "$OUTPUT"
    find -iname '*.flv' -exec ls "{}" \; | sort -n >> "$OUTPUT"
    find -iname '*.f4v' -exec ls "{}" \; | sort -n >> "$OUTPUT"
    find -iname '*.avi' -exec ls "{}" \; | sort -n >> "$OUTPUT"
    find -iname '*.mkv' -exec ls "{}" \; | sort -n >> "$OUTPUT"
    find -iname '*.wmv' -exec ls "{}" \; | sort -n >> "$OUTPUT"
    ;;
  esac
;;

Part 3. Its body part 2 (Ah, IT has no tail :lol: ).

"scale" | "quote" | "xtract.z")
  ## Does the filename contain spaces.
  URLENC=$(echo "$1" | sed -e "s_%20_\\ _g")
  if [ -n "${URLENC}" ];then DIR=${URLENC/file:\/\//};else DIR="${DIR}";fi
  ## If so, then move the file into a more 'workable' state.
  MOVED=$(echo "${DIR}" | sed -e "s_\\ _._g")
  if [ -f "${MOVED}" ];then DIR="${DIR}";else mv "${DIR}" "${MOVED}";DIR=${MOVED};fi
  ## Filename. <-- Everything is a 'file' or should I say 'object'.
  FILE=$(basename "${DIR}")
  ## Its 'basename'.
  BASEF=$(basename ${FILE%.*})
  ## Its 'extension'.
  EXTF="${FILE/${BASEF}/}"
  ## Name of out-file.
  OUTPUT=$(basename ${DIR%.*})
  ## Uh oh! 'Error' time.
  if [ ! -f "$DIR" ];then echo -e ${YELLOW}"    The file: "$DIR" doesn't exist!${NC}\n${RED}    Exiting...!!!"${NC}; exit 0;fi
  if [ ! "$EXT" = "$EXTF" ];then
  echo -e ${YELLOW}"  You made a typo!?${NC} ${CYAN}*auto-correcting*${NC}\n
  ${BLUE}From:${NC} ${YELLOW}"$SCRIPT" "${DIR}"${NC}\n
  ${BLUE}To:${NC} ${YELLOW}  "$BASE""$EXTF" "${DIR}""${NC}
  echo
  SCRIPT="$BASE""$EXTF"
  unset EXT
  EXT="${SCRIPT/${BASE}/}"
  sleep 1
  fi
  case "$OPTION" in
  "scale")
    if [ -f "$DIR" ];then
    if [ "$#" == "1" ];then DIR="$DIR";RES=pal;else
    for RES in "$2";do RES=${RES};done
    case "${RES}" in
      "4cif" )
      RES="$HD"
      ;;
      "pal" )
      RES="$PAL"
      ;;
      "pal" )
      RES="$_4CIF"
      ;;
      *)
      echo -e ${YELLOW}"    Please choose a valid option!"${NC}
      echo -e ${BLUE}"    "Usage:"${NC} ${CYAN}"$SCRIPT" "$DIR"${NC} |${CYAN}"4cif"${NC}|${CYAN}"pal"${NC}|${CYAN}"hd"${NC}|"
      exit 0
      ;;
    esac
    fi
    OUTPUT+="$UPS_EXT"
    ffmpeg -y -i "$DIR" \
    -acodec copy \
    -alang eng \
    -vlang eng \
    -metadata title=""$(basename "$OUTPUT" "$UPS_EXT")". ®" \
    -metadata artist=""$USER"" \
    -metadata date="$date" \
    -metadata publisher="$publisher" \
    -metadata copyright="$copy" \
    -vcodec libx264 -vpre lossless_ultrafast -crf 18 \
    -s "$RES" \
    "$OUTPUT"
    fi
  ;;
  "quote")
    if [ -f "$1" ];then
      if [ "$#" == "3" ];then START="$2" && DUR="$3"
      elif [ "$#" == "2" ];then START="${ZERO}" && DUR="$2"
      else [ "$#" == "1" ]
        echo -e ${BLUE}"\tEither in seconds or...,"${NC}
        echo -e ${BLUE}"\tHH:MM:SS.MSS or...,"${NC}
        echo -e ${BLUE}"\tPress enter for starts at 0."${NC}
        echo -en "\tEnter Start Position : "
        read START
        echo -e ${BLUE}"\tEither in seconds or...,"${NC}
        echo -e ${BLUE}"\tHH:MM:SS.MSS"${NC}
        echo -e ${BLUE}"\tPress enter for lasts 30 seconds."${NC}        
        echo -en "\tEnter Duration : "
        read DUR
      fi
      if [ -z "$START" ];then START="${ZERO}";fi
      if [ -z "$DUR" ];then DUR="${HALF}";fi
      for DIGITS in "$START" "$DUR";do
      if [[ "$DIGITS" =~ [^\ 0-9.,:]+ ]];then exit 0;fi
      done
      ## Arg "$2" NEEDS TO BE "QUOTED", e.g. "00 01 30.200"! But at least it works now. ; - )
      SPACE=$(echo "$START" | sed -e "s_\\ _:_g")
      if [ -n "$SPACE" ];then START="$SPACE"
      else start=${START%[ .,_-]*};end=${START/${start}[ .,_-]/}
        if [ "${start}" = "${end}" ];then end=000;else end="$end";fi
        START="$start"."$end"
      fi
      ## My num pad doesn't have a dot but uses comma instead.
      COMMA=$(echo "$START" | sed -e "s_,_._g")      
      if [ "$COMMA" = . ];then COMMA="${ZERO}";fi
      if [ -n "$COMMA" ];then START="$COMMA"
      else start=${START%[ .,_-]*};end=${START/${start}[ .,_-]/}
        if [ "${start}" = "${end}" ];then end=000;else end="$end";fi
        START="$start"."$end"
      fi
      SPACE=$(echo "$DUR" | sed -e "s_\\ _:_g")
      if [ -n "$SPACE" ];then DUR="$SPACE"
      else start=${DUR%[ .,_-]*};end=${DUR/${start}[ .,_-]/}
        if [ "${start}" = "${end}" ];then end=000;else end="$end";fi
        DUR="$start"."$end"
      fi
      COMMA=$(echo "$DUR" | sed -e "s_,_._g")
      if [ "$COMMA" = . ];then COMMA="${HALF}";fi      
      if [ -n "$COMMA" ];then DUR="$COMMA"
      else start=${DUR%[ .,_-]*};end=${DUR/${start}[ .,_-]/}
        if [ "${start}" = "${end}" ];then end=000;else end="$end";fi
        DUR="$start"."$end"
      fi
      echo -e ${YELLOW}""${START}""${NC}
      echo -e ${YELLOW}""${DUR}""${NC}      
    OUTPUT+=.quote"$EXT"
    if [ "$EXT" = .oga ] || [ "$EXT" = .ogg ] || [ "$EXT" = .flac ] || [ "$EXT" = .wav ] || [ "$EXT" = .mp3 ];then Choice=AUDIO;else Choice=VIDEO;fi
      case "$Choice" in
      "AUDIO")
      echo -e ${BLUE}"\tffmpeg -y -i "$1" -ss "${START}" -t "${DUR}" -acodec copy -vn "${OUTPUT}""${NC}
      ;;
      "VIDEO")
      echo -e ${BLUE}"\tmencoder -ss "${START}" -endpos "${DUR}" -oac copy -ovc copy "$1" -o "${OUTPUT}""${NC}
      ;;
      esac
    fi
  ;;
  "xtract.z")
    if [ -f "$DIR" ];then WD=ripped/"${BASEF}";OUTPUT=${PWD}"/"${WD}"/"${BASEF};[ ! -d "${WD}" ] && mkdir -p "${WD}"
    ffmpeg -y -threads 2 -i "${DIR}" -vn -acodec pcm_s16le -ac 2 "${WD}"/${BASEF}.wav
    ffmpeg -y -threads 2 -i "${WD}"/${BASEF}.wav -f mp3 -acodec libmp3lame -ac 2 -ab 320k "${WD}"/${BASEF}.mp3
    oggenc "${WD}"/${BASEF}.wav -q 9
    ffmpeg -y -threads 2 -i "${DIR}" -an -vcodec copy "${WD}"/${BASEF}.rip.avi
    fi
  ;;
  esac
;;
esac
exit 0

Part 4. Its “tail” (Well sort of. :lol: ).

Questions, suggestions, bugs found!? Feel free to let me know at this address: http://wp.me/PNJrg-p. Will I answer? That depends entirely upon you. ; - ) If you found a flaw/bug and you want me to point you out for that on here, then just say so. Otherwise all other email will be processed confidentially.

The actual script live and for the taking.

#!/bin/bash
# Modified: Today by E.L.F.
#
## Licensed as:
## http://www.opensource.org/licenses/mit-license.php
#
## Script-name - skel.sh
#
set -e
if [ "$USER" = root ];then
  echo -e ${RED}"\tError:${CYAN} \""$USER"\"${YELLOW} isn't allowed to use this script!"${NC};exit 0
else
  echo -e ${BLUE}"\t"$USER" may proceed.\n\t${CYAN}May peace be with you."${NC}
  echo
fi
clear
if [ "$(basename "$0")" = skel.sh ];then
  echo -e "${YELLOW}\tIf the${CYAN} \"Van\"${YELLOW} is${CYAN} Rockin'
  ${YELLOW}\tThen Don't Come a${CYAN} Knockin'"${NC};exit 0
fi
for COMMAND in mencoder ffmpeg oggenc cat
do COMMAND=$(which "$COMMAND")
if [ ! -x "$COMMAND" ];then RESULT=NO
echo -e ${YELLOW}"\tPlease install "${COMMAND}" first before continuing.${NC}
${CYAN}\tsudo apt-get install "$(basename "${COMMAND}")""${NC};fi
done
if [ "$RESULT" = NO ]; then echo -e ${BLUE}"\tSee (for example):${YELLOW} http://ubuntuforums.org/showthread.php?t=766683${BLUE} for more info on the matter."${NC};exit 0;fi
## Variables for the 'scale' command.
date="$(date +"%A, %B %-d, %Y")"
copy=$(echo "Up to you.")
publisher=$(echo "Up to you.")
UPS_EXT=.mkv
HD=1280x720
PAL=720x576
_4CIF=704x576
## Variables for the 'quote' command.
## If 'START' is 'empty' then.
ZERO=00:00:00.000
## If 'END' is 'empty' then.
HALF=00:00:30.000
## Variables for the 'xtract.z' command.
## Print working directory.
PWD=$(pwd)
## Scriptname.
SCRIPT="$(basename "$0")"
## 'basename' of the script.
BASE="$(basename ${SCRIPT%.*})"
## Its 'extension'.
EXT="${SCRIPT/${BASE}/}"
## End variables!
## What would you have your 'arbiter' do.
for OPTION in "${BASE}";do OPTION="${OPTION}";done
for CHOICE in "${EXT}";do CHOICE="${CHOICE}";done
#  ## When no argument is given, print its usage.
  if [ "$BASE" = scale ];then USAGE=$(echo "Usage: "${SCRIPT}" yourfile"${EXT}" 4cif, pal, hd")
  elif [ "$BASE" = quote ];then USAGE=$(echo "Usage: "${SCRIPT}" yourfile"${EXT}" Start End (Either in whole seconds or HH:MM:SS.MSS)")
  elif [ "$BASE" = clean.up ];then USAGE=$(echo "Usage: "${SCRIPT}" yourfile"${EXT}" norm, clean, rev, flash")
  else USAGE="Usage: "${SCRIPT}" yourfile"${EXT}"";fi
  if [ "$#" == "0" ];then echo -e ${YELLOW}"$USAGE"${NC};exit 0;fi
case "$OPTION" in
"cat" | "create")
  if [ "$#" == 1 ];then OUTPUT="$1";OUTPUT+="${EXT}";else echo -en "Enter filename: ";read OUTPUT;OUTPUT+="${EXT}";fi
  echo "$OUTPUT"
  case "$OPTION" in
  "cat")
    case "$CHOICE" in
    ".avi" | ".vob" | ".wmv")
    # LS=$(ls *.{avi,mp{e,eg,g},wmv} 2>/dev/null)
    LS=$(ls *"${EXT}" 2>/dev/null)
    echo "$LS"
    mencoder -forceidx -oac copy -ovc copy $(ls *"$EXT" | sort -n) -o "$OUTPUT"
    mv "$OUTPUT" ../"$OUTPUT"
    \rm *"${EXT}" 2>/dev/null
    mv ../"$OUTPUT" .
    ;;
    ".mpg")
    VAR="mpg-files.txt"
    ls *.mp{e,eg,g} 2>/dev/null | sort > $VAR
    cat $VAR | while read line; do
    INPUT=$(echo ${line})
    cat "$INPUT" >> "$OUTPUT"
    done
    \rm $VAR
    mv "$OUTPUT" ../"$OUTPUT"
    \rm *.mp{e,eg,g} 2>/dev/null
    mv ../"$OUTPUT" .
    ;;
    esac
  ;;
  "create")
    echo "#EXTM3U" > "$OUTPUT"
    find -iname '*.ogg' -exec ls "{}" \; | sort -n >> "$OUTPUT"
    find -iname '*.mp2' -exec ls "{}" \; | sort -n >> "$OUTPUT"
    find -iname '*.mp3' -exec ls "{}" \; | sort -n >> "$OUTPUT"
    find -iname '*.mp4' -exec ls "{}" \; | sort -n >> "$OUTPUT"
    find -iname '*.mpg' -exec ls "{}" \; | sort -n >> "$OUTPUT"
    find -iname '*.wma' -exec ls "{}" \; | sort -n >> "$OUTPUT"
    find -iname '*.flv' -exec ls "{}" \; | sort -n >> "$OUTPUT"
    find -iname '*.f4v' -exec ls "{}" \; | sort -n >> "$OUTPUT"
    find -iname '*.avi' -exec ls "{}" \; | sort -n >> "$OUTPUT"
    find -iname '*.mkv' -exec ls "{}" \; | sort -n >> "$OUTPUT"
    find -iname '*.wmv' -exec ls "{}" \; | sort -n >> "$OUTPUT"
    ;;
  esac
;;
"scale" | "quote" | "xtract.z")
  ## Does the filename contain spaces.
  URLENC=$(echo "$1" | sed -e "s_%20_\\ _g")
  if [ -n "${URLENC}" ];then DIR=${URLENC/file:\/\//};else DIR="${DIR}";fi
  ## If so, then move the file into a more 'workable' state.
  MOVED=$(echo "${DIR}" | sed -e "s_\\ _._g")
  if [ -f "${MOVED}" ];then DIR="${DIR}";else mv "${DIR}" "${MOVED}";DIR=${MOVED};fi
  ## Filename. <-- Everything is a 'file' or should I say 'object'.
  FILE=$(basename "${DIR}")
  ## Its 'basename'.
  BASEF=$(basename ${FILE%.*})
  ## Its 'extension'.
  EXTF="${FILE/${BASEF}/}"
  ## Name of out-file.
  OUTPUT=$(basename ${DIR%.*})
  ## Uh oh! 'Error' time.
  if [ ! -f "$DIR" ];then echo -e ${YELLOW}"\tThe file: "$DIR" doesn't exist!\n${RED}    Exiting...!!!"${NC}; exit 0;fi
  if [ ! "$EXT" = "$EXTF" ];then
  echo -e ${YELLOW}"\tYou made a typo!? ${CYAN}*auto-correcting*\n
  ${BLUE}\tFrom: ${YELLOW}"$SCRIPT" "${DIR}"\n
  ${BLUE}\tTo: ${YELLOW}  "$BASE""$EXTF" "${DIR}""${NC}
  echo
  SCRIPT="$BASE""$EXTF"
  unset EXT
  EXT="${SCRIPT/${BASE}/}"
  sleep 1
  fi
  case "$OPTION" in
  "scale")
    if [ -f "$DIR" ];then
    if [ "$#" == "1" ];then DIR="$DIR";RES=pal;else
    for RES in "$2";do RES=${RES};done
    case "${RES}" in
      "4cif" )
      RES="$HD"
      ;;
      "pal" )
      RES="$PAL"
      ;;
      "pal" )
      RES="$_4CIF"
      ;;
      *)
      echo -e ${YELLOW}"\tPlease choose a valid option!"${NC}
      echo -e ${BLUE}"\t"Usage:" ${CYAN}"$SCRIPT" "$DIR" |${CYAN}"4cif"|${CYAN}"pal"|${CYAN}"hd"${NC}|"
      exit 0
      ;;
    esac
    fi
    OUTPUT+="$UPS_EXT"
    ffmpeg -y -i "$DIR" \
    -acodec copy \
    -alang eng \
    -vlang eng \
    -metadata title=""$(basename "$OUTPUT" "$UPS_EXT")". ®" \
    -metadata artist=""$USER"" \
    -metadata date="$date" \
    -metadata publisher="$publisher" \
    -metadata copyright="$copy" \
    -vcodec libx264 -vpre lossless_ultrafast -crf 18 \
    -s "$RES" \
    "$OUTPUT"
    fi
  ;;
  "quote")
    if [ -f "$DIR" ];then
      if [ "$#" == "3" ];then START="$2" && DUR="$3"
      elif [ "$#" == "2" ];then START="${ZERO}" && DUR="$2"
      else [ "$#" == "1" ]
        echo -e ${BLUE}"\tEither in seconds or...,"${NC}
        echo -e ${BLUE}"\tHH:MM:SS.MSS or...,"${NC}
        echo -e ${BLUE}"\tPress enter for starts at 0."${NC}
        echo -en "\tEnter Start Position : "
        read START
        echo -e ${BLUE}"\tEither in seconds or...,"${NC}
        echo -e ${BLUE}"\tHH:MM:SS.MSS"${NC}
        echo -e ${BLUE}"\tPress enter for lasts 30 seconds."${NC}
        echo -en "\tEnter Duration : "
        read DUR
      fi
      if [ -z "$START" ];then START="${ZERO}";fi
      if [ -z "$DUR" ];then DUR="${HALF}";fi
      for DIGITS in "$START" "$DUR";do
      if [[ "$DIGITS" =~ [^\ 0-9.,:]+ ]];then exit 0;fi
      done
      ## Arg "$2" NEEDS TO BE "QUOTED", e.g. "00 01 30.200"! But at least it works now. ; - )
      SPACE=$(echo "$START" | sed -e "s_\\ _:_g")
      if [ -n "$SPACE" ];then START="$SPACE"
      else start=${START%[ .,_-]*};end=${START/${start}[ .,_-]/}
        if [ "${start}" = "${end}" ];then end=000;else end="$end";fi
        START="$start"."$end"
      fi
      ## My num pad doesn't have a dot but uses comma instead.
      COMMA=$(echo "$START" | sed -e "s_,_._g")
      if [ "$COMMA" = . ];then COMMA="${ZERO}";fi
      if [ -n "$COMMA" ];then START="$COMMA"
      else start=${START%[ .,_-]*};end=${START/${start}[ .,_-]/}
        if [ "${start}" = "${end}" ];then end=000;else end="$end";fi
        START="$start"."$end"
      fi
      SPACE=$(echo "$DUR" | sed -e "s_\\ _:_g")
      if [ -n "$SPACE" ];then DUR="$SPACE"
      else start=${DUR%[ .,_-]*};end=${DUR/${start}[ .,_-]/}
        if [ "${start}" = "${end}" ];then end=000;else end="$end";fi
        DUR="$start"."$end"
      fi
      COMMA=$(echo "$DUR" | sed -e "s_,_._g")
      if [ "$COMMA" = . ];then COMMA="${HALF}";fi
      if [ -n "$COMMA" ];then DUR="$COMMA"
      else start=${DUR%[ .,_-]*};end=${DUR/${start}[ .,_-]/}
        if [ "${start}" = "${end}" ];then end=000;else end="$end";fi
        DUR="$start"."$end"
      fi
    OUTPUT+=.quote"$EXT"
    if [ "$EXT" = .oga ] || [ "$EXT" = .ogg ] || [ "$EXT" = .flac ] || [ "$EXT" = .wav ] || [ "$EXT" = .mp3 ];then Choice=AUDIO;else Choice=VIDEO;fi
      case "$Choice" in
      "AUDIO")
      ffmpeg -y -i "${DIR}" -ss "${START}" -t "${DUR}" -acodec copy -vn "${OUTPUT}"
      ;;
      "VIDEO")
      mencoder -ss "${START}" -endpos "${DUR}" -oac copy -ovc copy "${DIR}" -o "${OUTPUT}"
      ;;
      esac
    fi
  "xtract.z")
    if [ -f "$DIR" ];then WD=ripped/"${BASEF}";OUTPUT=${PWD}"/"${WD}"/"${BASEF};[ ! -d "${WD}" ] && mkdir -p "${WD}"
    ffmpeg -y -threads 2 -i "${DIR}" -vn -acodec pcm_s16le -ac 2 "${WD}"/${BASEF}.wav
    ffmpeg -y -threads 2 -i "${WD}"/${BASEF}.wav -f mp3 -acodec libmp3lame -ac 2 -ab 320k "${WD}"/${BASEF}.mp3
    oggenc "${WD}"/${BASEF}.wav -q 9
    ffmpeg -y -threads 2 -i "${DIR}" -an -vcodec copy "${WD}"/${BASEF}.rip.avi
    fi
  ;;
  esac
;;
esac
exit 0

Mass editing text files too

The real reason why I bumped this post is for the following:

“Tor is written for and supported by people like you.”
“Tor jest pisany dla i wspierany przez ludzi takich, jak Ty.”
“Tor {is,wordt} geschreven voor en ondersteund door mensen zoals jou.”

Moet ik ‘m natuurlijk wel ‘up to date’ houden en niet na pas 2 maanden bijwerken. ;-)

http://tor-relay.thruhere.net/tor/

http://tor-relay.thruhere.net/tor/dist/

This post picks up where I left off in my last post.

Let’s start with this one:
Insert (arbitrary text) into the middle of some text file.

#!/bin/bash
## Modified: Today by E.l.f.
#
## Script-name - insert_html.sh
# -> Commented out command.
## -> My comments/explanation(s).
RED="\033[0;31m"
BLUE="\033[1;34m"
CYAN="\033[1;36m"
YELLOW="\033[1;33m"
NC="\033[0m"
if [ $USER = root ]; then
  echo -e $RED"   Are you Insane!"
  echo -e $CYAN"    Error: In order to use this script, one must NOT be $USER"
  echo -e $YELLOW"    Exiting..."$NC
  exit 0
else
  echo ""
  echo -e $BLUE"    $USER may proceed."$NC
  echo -e $CYAN"    May peace be with you."$NC
fi
clear # Clear the screen.
## Could be replaced with a for arg in something; do ...
echo -n "Enter filename: "
read INPUT
INPUT=$INPUT
## There's no need to type the extension after the filename.  ; - )
## Just let the script take care of that.
INPUT+=".html"
## Use your "grep.txt"\!
# e.g. grep -in \<div\ class=\"main-column\"\> *.html >> grep.txt
## That last command will output the exact line number where you wish to insert
## something else.
if [ ! -f "$INPUT" ]; then
  echo ""
  echo -e $RED"Error: File doesn't exist!"$NC
  echo -e $YELLOW"Exiting..."$NC
  echo ""
  exit 0
fi
## Yep, smilies still don't work well within 1 delimiters on wordpress.
## But be(a)ware of the single quote!!!  : - D : - D
## See:
# echo 'Why can'\''t I write '"'"'s between single quotes'
## As an example in why this is almost impossible.
## This particular widget uses ' quotes, hence my warning to you. ; - )
## Must be an exact match [^] (this includes spaces et all.)
## i for edit inplace and a for append.
# sed -i '/^searchForSomeString/a \
# Append sometext to it' YourFile.someExt
#
## Your mileage may vary and so does your text perhaps/possibly. ; - )
## And yes spaces (10 in this case) not tabs (\t), my tabs convert into two spaces while editing.
## Author's preference nothing more.
## FOUND!  : - D
sed -i '/^        <th colspan="3" align="center">Advanced Bash-Scripting Guide:<\/th>/a \
      <\/tr>\
      <tr>\
        <td>&nbsp;</td>\
        <td width="80%" align="center" valign="bottom">\
          <div id="google_translate_element"></div>\
          <script type="text/javascript">\
          //<![CDATA[\
          function googleTranslateElementInit() {\
          new google.translate.TranslateElement({\
            pageLanguage: "en",\
            includedLanguages: "af,sq,ar,hy,az,eu,be,bg,ca,zh-CN,zh-TW,hr,cs,da,nl,en,et,tl,fi,fr,gl,ka,de,el,ht,iw,hi,hu,is,id,ga,it,ja,ko,lv,lt,mk,ms,mt,no,fa,pl,pt,ro,ru,sr,sk,sl,es,sw,sv,th,tr,uk,ur,vi,cy,yi",\
            layout: google.translate.TranslateElement.InlineLayout.HORIZONTAL\
          }, "google_translate_element");\
          }\
          //]]>\
          </script>\
          <script src=\
          "http://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"\
          type="text/javascript">\
          </script>\
        </td>\
        <td>&nbsp;</td> ' "$INPUT"
exit 0

As the script used to be goes here (well at least a part of it):

# echo -n "Enter line-number here (e.g. 36, 41, 42): "
# read LINE
# LINE=$LINE
## Again choose carefully\!
# echo -n "Read the same file from line (usually this would be +1): "
# CNT=$(echo $(($LINE+1)))
## Just in case\!
# cp "$INPUT" "$INPUT".jic
# head --lines=$LINE "$INPUT" > head.html
# tail -n +$CNT "$INPUT" > tail.html
## The google.txt file is assumed to be in the WD,
## it could be any textfile you wish to insert though.
## Which in my case would be a simple translator's gadget.
## Adjust your spaces and tabulations (if applicable!?)
# cat google.txt >> head.html
# cat tail.html >> head.html
# \mv head.html "$INPUT"
# \rm tail.html

As you can see I’ve come a long way from just cutting up some piece of paper inserting some lines in and then gluing them back together (so to speak)! :lol: To me figuring out how one can indeed use sed to insert multiple lines, one must be weary of single ’s though. :lol: The top mentioned script features the google translate widget I meant to insert into a bunch of html files. An example can be viewed here. But this could be used for any purpose whatsoever, hence this publication. I must admit though that there (always) may be a quicker and dirtier way to do this? It still beats having to open an ‘n‘ amount of files and editing all this in manually :-o .

So now that is done with. On to mirroring tor.
Done!

How?
(Of if you prefer then go here.)
First we do:

rsync -av --delete rsync://rsync.torproject.org/tor tor-mirror/  ## I chose 'current' in this instance.

Then (Smallish update for today the 22nd of April.):

#cd into wd (working directory.
#Download the mirror.
rsync -av --delete rsync://rsync.torproject.org/tor tor-current/
cd tor-current
find -iname 'index.html' | wc -l ## None.
## thttpd sends these as 'plain/text'
find -iname 'index.html.en' | wc -l ## Just a few.
find -iname 'index.html.en' -exec ls -lh "{}" \; ## Where are they.
find -iname 'makefile' -exec ls -lh "{}" \; ## More of a necessity for the developer(s)
find -iname 'makefile' -exec \rm "{}" \; ## No need for those on a mirror.
find -iname 'makefile' -exec ls -lh "{}" \; ## Check.
find -iname 'en' -exec ls -lh "{}" \; ## These folders contain *.wml files.
find -iname 'en' -exec \rm -r "{}" \; ## -r because these are folders.
## Also removed any reference to the 'languageswitch' cgi script
## As well as any other shell script encountered.
history >> tor.s.history.txt ## Just to keep track of my own changes.  ; - )
## No need for GID but sticky? Yes.
find . -type d -exec chmod -s {} \; # Remove the setuid!
find . -type d -exec chmod 1755 {} \; # Folders are set to sticky.
## dist is gonna go on its own slice due to lack of space in /var/www.
# alias Bind='sudo mount -o bind' # /sourcefolderurl /destinationfolderurl
# ztar dist.tgz dist/ # alias ztar='tar cvzf ' # Name.tgz folder/
for i in *.html.en;do mv "$i" $(basename "$i" .html.en).html;done
## In tor's wd.
## Please note that I consider it a safer approach to work in one directory at a time!
## This instead of:
# find -iname '*someName*' -exec someThing "{}" \;
cd about
for i in *.html.en;do mv "$i" $(basename "$i" .html.en).html;done
sed -i 's@<h1 id="logo"><a href="../index.html">Tor</a></h1>@<h1 id="logo"><a target="_blank" href="https://www.torproject.org/">Tor</a></h1>@' *.html
sed -i 's@<li><a href="../index.html">Home</a></li>@<li><a href="../">Home</a></li>@' *.html
sed -i 's@<a href="https://blog.torproject.org/blog/">@<a target="_blank" href="https://blog.torproject.org/blog/">@' *.html
sed -i 's@<a href="http://printfection.com/torprojectstore">@<a target="_blank" href="http://printfection.com/torprojectstore">@' *.html
cd ../docs
for i in *.html.en;do mv "$i" $(basename "$i" .html.en).html;done
sed -i 's@.html.en@.html@' *.html
sed -i 's@<h1 id="logo"><a href="../index.html">Tor</a></h1>@<h1 id="logo"><a target="_blank" href="https://www.torproject.org/">Tor</a></h1>@' *.html
sed -i 's@<li><a href="../index.html">Home</a></li>@<li><a href="../">Home</a></li>@' *.html
sed -i 's@<a href="https://blog.torproject.org/blog/">Blog</a>@<a target="_blank" href="https://blog.torproject.org/blog/">Blog</a>@' *.html
sed -i 's@<a href="http://printfection.com/torprojectstore">Store</a>@<a target="_blank" href="http://printfection.com/torprojectstore">Store</a>@' *.html
cd ../donate
for i in *.html.en;do mv "$i" $(basename "$i" .html.en).html;done
sed -i 's@.html.en@.html@' *.html
sed -i 's@<h1 id="logo"><a href="../index.html">Tor</a></h1>@<h1 id="logo"><a target="_blank" href="https://www.torproject.org/">Tor</a></h1>@' *.html
sed -i 's@<li><a href="../index.html">Home</a></li>@<li><a href="../">Home</a></li>@' *.html
sed -i 's@<a href="https://blog.torproject.org/blog/">Blog</a>@<a target="_blank" href="https://blog.torproject.org/blog/">Blog</a>@' *.html
sed -i 's@<a href="http://printfection.com/torprojectstore">Store</a>@<a target="_blank" href="http://printfection.com/torprojectstore">Store</a>@' *.html
cd ../download
for i in *.html.en;do mv "$i" $(basename "$i" .html.en).html;done
sed -i 's@.html.en@.html@' *.html
sed -i 's@<h1 id="logo"><a href="../index.html">Tor</a></h1>@<h1 id="logo"><a target="_blank" href="https://www.torproject.org/">Tor</a></h1>@' *.html
sed -i 's@<li><a href="../index.html">Home</a></li>@<li><a href="../">Home</a></li>@' *.html
sed -i 's@<a href="https://blog.torproject.org/blog/">Blog</a>@<a target="_blank" href="https://blog.torproject.org/blog/">Blog</a>@' *.html
sed -i 's@<a href="http://printfection.com/torprojectstore">Store</a>@<a target="_blank" href="http://printfection.com/torprojectstore">Store</a>@' *.html
cd ../eff
for i in *.html.en;do mv "$i" $(basename "$i" .html.en).html;done
sed -i 's@.html.en@.html@' *.html
sed -i 's@<h1 id="logo"><a href="../index.html">Tor</a></h1>@<h1 id="logo"><a target="_blank" href="https://www.torproject.org/">Tor</a></h1>@' *.html
sed -i 's@<li><a href="../index.html">Home</a></li>@<li><a href="../">Home</a></li>@' *.html
sed -i 's@<a href="https://blog.torproject.org/blog/">Blog</a>@<a target="_blank" href="https://blog.torproject.org/blog/">Blog</a>@' *.html
sed -i 's@<a href="http://printfection.com/torprojectstore">Store</a>@<a target="_blank" href="http://printfection.com/torprojectstore">Store</a>@' *.html
cd ../getinvolved/
for i in *.html.en;do mv "$i" $(basename "$i" .html.en).html;done
sed -i 's@.html.en@.html@' *.html
sed -i 's@<h1 id="logo"><a href="../index.html">Tor</a></h1>@<h1 id="logo"><a target="_blank" href="https://www.torproject.org/">Tor</a></h1>@' *.html
sed -i 's@<li><a href="../index.html">Home</a></li>@<li><a href="../">Home</a></li>@' *.html
sed -i 's@<a href="https://blog.torproject.org/blog/">Blog</a>@<a target="_blank" href="https://blog.torproject.org/blog/">Blog</a>@' *.html
sed -i 's@<a href="http://printfection.com/torprojectstore">Store</a>@<a target="_blank" href="http://printfection.com/torprojectstore">Store</a>@' *.html
cd ../press
for i in *.html.en;do mv "$i" $(basename "$i" .html.en).html;done
sed -i 's@.html.en@.html@' *.html
sed -i 's@<h1 id="logo"><a href="../index.html">Tor</a></h1>@<h1 id="logo"><a target="_blank" href="https://www.torproject.org/">Tor</a></h1>@' *.html
sed -i 's@<li><a href="../index.html">Home</a></li>@<li><a href="../">Home</a></li>@' *.html
sed -i 's@<a href="https://blog.torproject.org/blog/">Blog</a>@<a target="_blank" href="https://blog.torproject.org/blog/">Blog</a>@' *.html
sed -i 's@<a href="http://printfection.com/torprojectstore">Store</a>@<a target="_blank" href="http://printfection.com/torprojectstore">Store</a>@' *.html
cd ../projects/
for i in *.html.en;do mv "$i" $(basename "$i" .html.en).html;done
sed -i 's@.html.en@.html@' *.html
sed -i 's@<h1 id="logo"><a href="../index.html">Tor</a></h1>@<h1 id="logo"><a target="_blank" href="https://www.torproject.org/">Tor</a></h1>@' *.html
sed -i 's@<li><a href="../index.html">Home</a></li>@<li><a href="../">Home</a></li>@' *.html
sed -i 's@<a href="https://blog.torproject.org/blog/">Blog</a>@<a target="_blank" href="https://blog.torproject.org/blog/">Blog</a>@' *.html
sed -i 's@<a href="http://printfection.com/torprojectstore">Store</a>@<a target="_blank" href="http://printfection.com/torprojectstore">Store</a>@' *.html
cd ../torbutton/
for i in *.html.en;do mv "$i" $(basename "$i" .html.en).html;done
sed -i 's@.html.en@.html@' *.html
sed -i 's@<h1 id="logo"><a href="../index.html">Tor</a></h1>@<h1 id="logo"><a target="_blank" href="https://www.torproject.org/">Tor</a></h1>@' *.html
sed -i 's@<li><a href="../index.html">Home</a></li>@<li><a href="../">Home</a></li>@' *.html
sed -i 's@<a href="https://blog.torproject.org/blog/">Blog</a>@<a target="_blank" href="https://blog.torproject.org/blog/">Blog</a>@' *.html
sed -i 's@<a href="http://printfection.com/torprojectstore">Store</a>@<a target="_blank" href="http://printfection.com/torprojectstore">Store</a>@' *.html
cd..
#For all other foreign pages (repeated per directory):
#{ar,da,de,es,fa,fr,it,pl,ru}
for i in *.html.ar;do mv "$i" $(basename "$i" .html.ar).ar.html;done
for i in *.html.da;do mv "$i" $(basename "$i" .html.da).da.html;done
for i in *.html.de;do mv "$i" $(basename "$i" .html.de).de.html;done
for i in *.html.es;do mv "$i" $(basename "$i" .html.es).es.html;done
for i in *.html.fa;do mv "$i" $(basename "$i" .html.fa).fa.html;done
for i in *.html.fr;do mv "$i" $(basename "$i" .html.fr).fr.html;done
for i in *.html.it;do mv "$i" $(basename "$i" .html.it).it.html;done
for i in *.html.pl;do mv "$i" $(basename "$i" .html.pl).pl.html;done
for i in *.html.ru;do mv "$i" $(basename "$i" .html.ru).ru.html;done
# 2> /dev/null suppresses error messages about files being non existent and such clutter.
sed -i 's@.html.ar@.ar.html@' *.{ar,da,de,es,fa,fr,it,pl,ru}.html 2> /dev/null
sed -i 's@.html.da@.da.html@' *.{ar,da,de,es,fa,fr,it,pl,ru}.html 2> /dev/null
sed -i 's@.html.de@.de.html@' *.{ar,da,de,es,fa,fr,it,pl,ru}.html 2> /dev/null
sed -i 's@.html.es@.es.html@' *.{ar,da,de,es,fa,fr,it,pl,ru}.html 2> /dev/null
sed -i 's@.html.fa@.fa.html@' *.{ar,da,de,es,fa,fr,it,pl,ru}.html 2> /dev/null
sed -i 's@.html.fr@.fr.html@' *.{ar,da,de,es,fa,fr,it,pl,ru}.html 2> /dev/null
sed -i 's@.html.it@.it.html@' *.{ar,da,de,es,fa,fr,it,pl,ru}.html 2> /dev/null
sed -i 's@.html.pl@.pl.html@' *.{ar,da,de,es,fa,fr,it,pl,ru}.html 2> /dev/null
sed -i 's@.html.ru@.ru.html@' *.{ar,da,de,es,fa,fr,it,pl,ru}.html 2> /dev/null
sed -i 's@.html.en@.html@' *.{ar,da,de,es,fa,fr,it,pl,ru}.html 2> /dev/null
sed -i 's@<a href="https://blog.torproject.org/blog/">@<a target="_blank" href="https://blog.torproject.org/blog/">@' *.html
sed -i 's@<a href="http://printfection.com/torprojectstore">@<a target="_blank" href="http://printfection.com/torprojectstore">@' *.html
# Oops, I forgot to update the English only pages! *hint do the English pages first!*
sed -i 's@.html.ar@.ar.html@' *.html 2> /dev/null
sed -i 's@.html.da@.da.html@' *.html 2> /dev/null
sed -i 's@.html.de@.de.html@' *.html 2> /dev/null
sed -i 's@.html.es@.es.html@' *.html 2> /dev/null
sed -i 's@.html.fa@.fa.html@' *.html 2> /dev/null
sed -i 's@.html.fr@.fr.html@' *.html 2> /dev/null
sed -i 's@.html.it@.it.html@' *.html 2> /dev/null
sed -i 's@.html.pl@.pl.html@' *.html 2> /dev/null
sed -i 's@.html.ru@.ru.html@' *.html 2> /dev/null

All in all I didn't adjust this mirror in the intended 15 minutes as can be read in the link below.
This is due mainly because this version features a lot of translations of which it is noteworthy to see a lot of them to be in Polish.
ofchmod # alias for chmod 644 all files.
find -iname '*.html' -exec sed -i 's@<title>Tor@<title>Please note that you'\''re viewing a local mirror - Tor@' "{}" \;

tidyup.sh  ## See my previous post about mass editing text.

http://bohemian0wildebeest.wordpress.com/2011/01/22/mass-editing-text-files-too/

The code looks daunting, doesn’t it? It really isn’t! ;-) If I subtract the amount of time necessary to figure things out (which IS the larger part and we don’t like to repeat ourselves!), this basically could be done in less than 10 minutes (minus the download time for rsync of course.) Basically I tracked my own progress while editing, which IS the smarter thing to do. Because next time I don’t have to do the figure it out again part, which I didn’t do last time. ;-)

Until next time,

Alex

Mass editing text files.

As a first.

Google is your friend
Second:
As if this weren’t obvious!? I am still LEARNING and as such will arrive at different conclusions from time to time. See here for more on how I view this or better put how the author of this fine article defines this! :-)

Q: Why the need for such tools?
A: I maintain/manage a relatively large amount of self study material aka mirroring a few sites that have (once peaked) my interest. And because “I am that guy” I allow you to peek at those as well.

Third and last: …

All others just pop open the hood and watch and learn (as if? :lol: )

On to the scripts:

Tidy Up script goes here:

#!/bin/bash
# Modified: Today by E.L.F.
#
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 2 of the License, or (at your option) any later
## version.
#
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
## FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
#
## You should have received a copy of the GNU General Public License along with
## this program; if not, write to the Free Software Foundation, Inc., 51
## Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
## http://www.gnu.org/copyleft/gpl.html
#
## Script-name - tidyup.sh
## Requires tidy and optionally tidy-doc
# apt-cache search tidy
# sudo apt-get install tidy tidy-doc
## Or if you use shorthand as I do. ; - )
# aptsearch tidy tidy-doc
# Install tidy tidy-doc
## Needs the following lines to be present in $HOME/.bash_aliases though.
## To search
# alias aptsearch='apt-cache search '
## When an exact match show package info for.
# alias aptshow='apt-cache show '
## To install
# alias Install='echo "sudo apt-get install " && sudo apt-get install'
RED="\033[0;31m"
BLUE="\033[1;34m"
CYAN="\033[1;36m"
YELLOW="\033[1;33m"
NC="\033[0m"
if [ $USER = root ]; then
  echo -e $RED"   Are you Insane!"
  echo -e $CYAN"   Error: In order to use this script, one must NOT be $USER"
  echo -e $YELLOW"    Exiting..."$NC
  exit 0
else
  echo ""
  echo -e $BLUE"    $USER may proceed."
  echo -e $CYAN"    May peace be with you."$NC
fi
clear
H="html.txt"
find -iname '*.html' | sort > $H
## This scheme ignores filenames%20with%20ugly%20spaces completely. Sweet!
cat $H | while read line; do
I=$(echo ${line})
## I use $HOME/.usr/bin contrary to proper Ubuntu usage whereas they use
## simply $HOME/bin for user land scripts\!
config=$(echo "$HOME/.usr/bin/etc/config.txt")
tidy -config $config "$I"
done
rm $H
exit 0

The config.txt the previous script referred to goes here:

// sample config file for HTML tidy
doctype: transitional
indent: auto
indent-spaces: 2
wrap: 76
markup: yes
bare: yes
clean: yes
preserve-entities: yes
output-xml: no
input-xml: no
output-xhtml: yes
show-warnings: yes
numeric-entities: yes
quote-marks: yes
quote-nbsp: yes
quote-ampersand: no
break-before-br: no
uppercase-tags: no
uppercase-attributes: no
char-encoding: utf8
input-encoding: utf8
output-bom: auto
output-encoding: utf8
new-inline-tags: cfif, cfelse, math, mroot,
  mrow, mi, mn, mo, msqrt, mfrac, msubsup, munderover,
  munder, mover, mmultiscripts, msup, msub, mtext,
  mprescripts, mtable, mtr, mtd, mth
new-blocklevel-tags: cfoutput, cfquery
new-empty-tags: cfelse
repeated-attributes: keep-last
error-file: errs.txt
write-back: yes

The eradicate empty lines and white space from file(s) script goes here:

#!/bin/bash
# Modified: Today by E.L.F.
#
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 2 of the License, or (at your option) any later
## version.
#
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
## FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
#
## You should have received a copy of the GNU General Public License along with
## this program; if not, write to the Free Software Foundation, Inc., 51
## Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
## http://www.gnu.org/copyleft/gpl.html
#
## Script-name - empty.lines.sh
#
## Q: Why not configure tidy to do exactly that!?
## A: I may want to use this on other files of type plain/text,
## of which afaik tidy doesn't parse.
## And why should tidy parse other files of type plain/text anyway?
RED="\033[0;31m"
BLUE="\033[1;34m"
CYAN="\033[1;36m"
YELLOW="\033[1;33m"
NC="\033[0m"
if [ $USER = root ]; then
  echo -e $RED"   Are you Insane!"
  echo -e $CYAN"   Error: In order to use this script, one must NOT be $USER"
  echo -e $YELLOW"    Exiting..."$NC
  exit 0
else
  echo ""
  echo -e $BLUE"    $USER may proceed."
  echo -e $CYAN"    May peace be with you."$NC
fi
clear
  ## Removing nonsense encoding schemes.
  ## Insert your own strings here.
#find -iname '*.html' -exec sed -i 's/<?xml version="1.0" encoding="utf-8" standalone="no"?>//' "{}" \;
#find -iname '*.html' -exec sed -i 's/<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>//' "{}" \;
#find -iname '*.html' -exec sed -i 's@/glossary/@@' "{}" \;
  ## Removing the excess whitespace (if applicable?) for you.
  # sed -i 's/[ \t]*$//' # see note on '\t' at end of file
## Of course I could use a case-select scenario for several filetypes as well.  
find -iname '*.html' -exec sed -i 's/[ \t]*$//' "{}" \;
  ## Removing those empty lines for you.
find -iname '*.html' -exec sed -i '/^$/d' "{}" \;
#   \rm *.bak
exit 0

Compress, remove and link file(s) script?
One can expect a lot of file not found errors and other crap when using the following script!
Because it is FAR from complete but I am using it anyway.
You’ve been properly warned!!

No, it won’t do that anymore. I’ve added a nice “fun roll loop” to the script. Be aware though that the user still needs to know in advance which types of files to choose from (of which I assume that you do.)!

But!

Please note that I already know one can achieve the same thing with the following three lines.

# for i in {*.html,*.js,*.css,*.txt,*.xml}; do gzip -c --best $i > $i.gz;done
# for i in {*.html,*.js,*.css,*.txt,*.xml}; do \rm $i;done
# for i in {*.html.gz,*.js.gz,*.css.gz,*.txt.gz,*.xml.gz}; do ln -s -T "$i" $(basename "$i" .gz);done

Though I can see a form of elegance as opposed to the rather elaborate ‘function’ “my” script now has become. :-) In that it requires no less than three lines to achieve the goal I had set to mind. *YaY* The problem with the above mentioned tactic is, that it is rather “brute force”. “Hence the del’ed warning” and will leave the user with, depending on the amount of files processed, with a lot of empty files and links to those files. Now on my system this won’t have any adverse effects, but I don’t know how this would be on yours (think filesystem for example.)!?

Aside from me wanting to keep things SIMPLE. ;-) It would be neat as well, if this script doesn’t exit when a file simply doesn’t exist (or leaves you with empty files), but simply returns back up something like ‘goto top’ for example??? Will work on that later! Both on the script as well as my formatting skills. ;-) (Note to self: Must close every html element properly. :lol:

Has been moved to there:

#!/bin/bash
## Modified: Today by E.l.f.
#
## Script-name - ziprmlink.sh
#
## Source:
# http://tldp.org/LDP/abs/html/loops1.html
RED="\033[0;31m"
BLUE="\033[1;34m"
CYAN="\033[1;36m"
YELLOW="\033[1;33m"
NC="\033[0m"
if [ $USER = root ]; then
  echo -e $RED"   Are you Insane!"
  echo -e $CYAN"    Error: In order to use this script, one must NOT be $USER"
  echo -e $YELLOW"    Exiting..."$NC
  exit 0
else
  echo ""
  echo -e $BLUE"    $USER may proceed."
  echo -e $CYAN"    May peace be with you."$NC
fi
clear # Clear the screen.
TIME=1
RePeat ()
{ # A somewhat more complex function.
a=0
## Because we only have 5 possible selections.
REPEATS=5
sleep $TIME    # Hey, wait a second!
  while [ $a -lt $REPEATS ]
    do
      echo "-------------------Mass Edit Options List-------------------"
      echo
      echo "    Please choose one of the following options:              "
      echo "                              Or                             "
      echo "    Hit Ctrl+c (^c) to stop now."
      echo "    \"UPPER\", \"lower\" and \"Capitalized\" spelling "
      echo "    are valid forms of input."
      echo
      echo "    Usage: "
      echo "    html"
      echo "    text"
      echo "    js"
      echo "    xml"
      echo "    css"
      echo
      echo "-------------------------------------------------------------"
      echo ""
      read Choice
      case "$Choice" in
      "HTML" | "Html" | "html")
      echo -e $BLUE"    You've chosen ."$Choice" file(s) to work on."$NC
      echo -e $RED"Executing..."$NC
      for i in *.html; do gzip -c --best $i > $i.gz;done
      for i in *.html; do \rm $i;done
      for i in *.html.gz; do ln -s -T "$i" $(basename "$i" .gz);done
      ;;
      "TXT" | "Txt" | "txt" | "TEXT" | "Text" | "text")
      echo -e $BLUE"    You've chosen ."$Choice" file(s) to work on."$NC
      echo -e $RED"Executing..."$NC
      for i in *.txt; do gzip -c --best $i > $i.gz;done
      for i in *.txt; do \rm $i;done
      for i in *.txt.gz; do ln -s -T "$i" $(basename "$i" .gz);done
      ;;
      "CSS" | "Css" | "css")
      echo -e $BLUE"    You've chosen ."$Choice" file(s) to work on."$NC
      echo -e $RED"Executing..."$NC
      for i in *.css; do gzip -c --best $i > $i.gz;done
      for i in *.css; do \rm $i;done
      for i in *.css.gz; do ln -s -T "$i" $(basename "$i" .gz);done
      ;;
      "XML" | "Xml" | "xml")
      echo -e $BLUE"    You've chosen ."$Choice" file(s) to work on."$NC
      echo -e $RED"Executing..."$NC
      for i in *.xml; do gzip -c --best $i > $i.gz;done
      for i in *.xml; do \rm $i;done
      for i in *.xml.gz; do ln -s -T "$i" $(basename "$i" .gz);done
      ;;
      "JS" | "Js" | "js")
      echo -e $BLUE"    You've chosen ."$Choice" file(s) to work on."$NC
      echo -e $RED"Executing..."$NC
      for i in *.js; do gzip -c --best $i > $i.gz;done
      for i in *.js; do \rm $i;done
      for i in *.js.gz; do ln -s -T "$i" $(basename "$i" .gz);done
      ;;
      *)
      echo
      echo "Please choose a valid option."
      echo "\"UPPER\", \"lower\" and \"Capitalized\" spelling are supported"
      echo "    Usage: "
      echo "    html"
      echo "    text"
      echo "    js"
      echo "    xml"
      echo "    css"
      echo
      ;;
      esac
    let "a+=1"
  done
}
## Repeat 5 times which for the user would depend on the need or amount of
## filetypes that need to be processed.
RePeat
exit 0

While the above mentioned script loops at ye 5 times (or until you hit ^c/ctrl+c) and is as such pretty handy. Why not have a “one command to rule them all”. Well here it is (almost). :lol: I got the inspiration from here. Also to repeat what I wrote on top: as if this weren’t obvious by now!? I am still LEARNING and as such will arrive at different conclusions from time to time. See here for more on how I view this or better put how the author defines this!

Basically this adds in extra functionality to bash and keeps things organized at the same time!

# Functions: check for a separate function file, and if we find one
# source it.
if [[ -f ~/.bash_functions ]]; then
    . ~/.bash_functions
fi
massedit()
{
RED="\033[0;31m"
BLUE="\033[1;34m"
CYAN="\033[1;36m"
YELLOW="\033[1;33m"
NC="\033[0m"
if [ $USER = root ]; then
  echo -e $RED"   Are you Insane!"
  echo -e $CYAN"    Error: In order to use this script, one must NOT be $USER"
  echo -e $YELLOW"    Exiting..."$NC
  exit 0
else
  echo ""
  echo -e $BLUE"    $USER may proceed."
  echo -e $CYAN"    May peace be with you."$NC
fi
clear # Clear the screen.
  case "$1" in
  "HTML" | "Html" | "html")
  if [ ! "$(ls *.html 2>/dev/null)" ]
  then
  echo -e $YELLOW"$1 does not exist."$NC; echo
  else
  echo -e $BLUE"    You've chosen \"$(ls *.html | wc -l)\" file(s) to work on."$NC
  echo -e $RED"Executing..."$NC
  for i in *.html; do gzip -c --best $i > $i.gz;done
  for i in *.html; do \rm $i;done
  for i in *.html.gz; do ln -s -T "$i" $(basename "$i" .gz);done
  fi
  ;;
  "TXT" | "Txt" | "txt" | "TEXT" | "Text" | "text")
  if [ ! "$(ls *.txt 2>/dev/null)" ]
  then
  echo -e $YELLOW"$1 does not exist."$NC; echo
  else
  echo -e $BLUE"    You've chosen \"$(ls *.txt | wc -l)\" file(s) to work on."$NC
  echo -e $RED"Executing..."$NC
  for i in *.txt; do gzip -c --best $i > $i.gz;done
  for i in *.txt; do \rm $i;done
  for i in *.txt.gz; do ln -s -T "$i" $(basename "$i" .gz);done
  fi
  ;;
  "CSS" | "Css" | "css")
  if [ ! "$(ls *.css 2>/dev/null)" ]
  then
  echo -e $YELLOW"$1 does not exist."$NC; echo
  else
  echo -e $BLUE"    You've chosen \"$(ls *.css | wc -l)\" file(s) to work on."$NC
  echo -e $RED"Executing..."$NC
  for i in *.css; do gzip -c --best $i > $i.gz;done
  for i in *.css; do \rm $i;done
  for i in *.css.gz; do ln -s -T "$i" $(basename "$i" .gz);done
  fi
  ;;
  "XML" | "Xml" | "xml")
  if [ ! "$(ls *.xml 2>/dev/null)" ]
  then
  echo -e $YELLOW"$1 does not exist."$NC; echo
  else
  echo -e $BLUE"    You've chosen \"$(ls *.xml | wc -l)\" file(s) to work on."$NC
  echo -e $RED"Executing..."$NC
  for i in *.xml; do gzip -c --best $i > $i.gz;done
  for i in *.xml; do \rm $i;done
  for i in *.xml.gz; do ln -s -T "$i" $(basename "$i" .gz);done
  fi
  ;;
  "JS" | "Js" | "js")
  if [ ! "$(ls *.js 2>/dev/null)" ]
  then
  echo -e $YELLOW"$1 does not exist."$NC; echo
  else
  echo -e $BLUE"    You've chosen \"$(ls *.js | wc -l)\" file(s) to work on."$NC
  echo -e $RED"Executing..."$NC
  for i in *.js; do gzip -c --best $i > $i.gz;done
  for i in *.js; do \rm $i;done
  for i in *.js.gz; do ln -s -T "$i" $(basename "$i" .gz);done
  fi
  ;;
  *)
  echo
  echo "Please choose a valid option."
  echo "\"UPPER\", \"lower\" and \"Capitalized\" spelling are supported"
  echo -e $YELLOW"    Usage$NC: "
  echo -e $CYAN"    massedit$NC$BLUE html$NC"
  echo -e $CYAN"    massedit$NC$BLUE text$NC"
  echo -e $CYAN"    massedit$NC$BLUE js$NC"
  echo -e $CYAN"    massedit$NC$BLUE xml$NC"
  echo -e $CYAN"    massedit$NC$BLUE css$NC"
  echo  
  ;;
  esac
}

Yes I know it is still QUITE elaborate! But it serves it purpose. ;-) One simply cd’s into the folder of his her choosing and then one does massedit $arg (as in either html,text,js,xml or css but not all at the same time (yet))

This is also a cute function. For those times you only want to edit ONE file. ;-)
This should be seen as a companion to the tidyup.sh script I wrote about earlier.

## Or maketidy if you prefer.
mtidy()
{
## This is where I keep my tidy config file.
## Your path may differ.
config=$(echo "$HOME/.usr/bin/etc/config.txt")
tidy -config $config $1
}
  • Q: Why the linking of *.html to *.html.gz files?
  • LA:
    Because I use thttpd for serving up static content from within the comforts of my home and because I know thttpd supports sending content in gzip format (one simply appends the .gz extension.).
  • Furthermore:
  • I believe this will greatly supplement my poor upload speed, also because I don’t want to frustrate both search engines as well as the casual visitor(s). With the all to well known 404 not found error. Therefore I’ve decided to make things work “seamlessly”, without you knowing, aside from me telling you now. Does require a bit of tinkering with the config though might I add.
  • Q: Why not use cgi instead, I know thttpd supports that?
  • LA: Hmm… (.redirect comes to mind.) True, but a server running at home! Might need to be VERY secure and
    I am not willing to compromise security for convenience (yet).
    Hence a chroot’ed environment and no executables present withing the view
    of the program, better yet not even outbound access, might I add, for the user
    running the program.
  • SA: Because I choose too. :-D







PS: Constructive criticism/feedback will be (re)posted, all other(s) will be dropped HARD to the floor.