The usefulness of bash_aliases and bash_functions.


The usefulness of bash_aliases and bash_functions.

Today I’m going to write a somewhat more ‘techno’ post. 😆

Insert TL;DR warning here. 😆

Hey look, a butterfly.

Now that I’ve lost the crowd’s attention just about now. 😆 Let me continue!

Sharing means Caring.

First we start by pointing out other people who have done the same.:

Gallery here:

Slideshow here:

This slideshow requires JavaScript.

How to add color to my commands?

## Insert a new line into '$HOME/.bashrc'
echo >> $HOME/.bashrc
## Or simply hit 'ctrl+d' instead of using '<<_EOF_' here.
# Define some colors first:
# 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
export Black="\033[0;30m"
export Red="\033[0;31m"
export Green="\033[0;32m"
export Yellow="\033[1;33m"
export Blue="\033[1;34m"
export Magenta="\033[1;35m"
export Cyan="\033[1;36m"
export White="\033[1;37m"
export Nc="\033[0m" # No Color
# --> Nice. Has the same effect as using "ansi.sys" in DOS.
_EOF_

How to enable the use of a “$HOME/.bash_functions” file:
(Don’t forget to either (re)source this file or to start a new terminal,
for changes to take effect.)

## Insert a new line into '$HOME/.bashrc'
echo >> $HOME/.bashrc
## Or simply hit 'ctrl+d' instead of using '<<_EOF_' here.
## The following will enable to use the function as described below.
cat >> $HOME/.bashrc<<_EOF_
# Functions: check for a separate function file, and if we find one
# source it.
if [ -f ~/.bash_functions ]; then
    . ~/.bash_functions
fi
_EOF_

Initially I was _only_ going to point out this feature:

# Requires 'openssl' - Install openssl
## ^ Usually already is installed. ; - )
## 'gpg, gnupg, pgp, openpgp' &lt;- confused?
## https://help.ubuntu.com/community/GnuPrivacyGuardHowto
## https://help.ubuntu.com/community/OpenSSL
crypto()
{
if [ "$USER" = root ];then echo -e "\tError: \""$USER"\" isn't allowed to use this script!";exit 0
else echo -e "\t${USER} may proceed.\n\tMay peace be with you.\n";fi
Color_Test=$(grep -i '\\033\[' "$HOME"/.bashrc)
if [ -n "$Color_Test" ];then echo -e ${Magenta}"\tOk, colors are defined."${Nc}
else
Black="\033[0;30m"
Red="\033[0;31m"
Green="\033[0;32m"
Yellow="\033[1;33m"
Blue="\033[1;34m"
Magenta="\033[1;35m"
Cyan="\033[1;36m"
White="\033[1;37m"
Nc="\033[0m" # No Color
echo -e ${White}"\tIf colors weren't defined, then they are now."${Nc}
fi
if [ -z "$1" ]; then
echo -e ${Blue}"\tPlease choose a valid option!\n${Yellow}\tUsage:"${Nc}
echo -e ${Cyan}"\tcrypto"${Nc}" {"${Cyan}"--encrypt"${Nc}","${Cyan}"-e"${Nc}"}\t${Blue} Your-file "${Nc}
echo -e ${Cyan}"\tcrypto"${Nc}" {"${Cyan}"--decrypt"${Nc}","${Cyan}"-d"${Nc}"}\t${Blue} Your-file.aes "${Nc}
echo -e ${Cyan}"\tcrypto"${Nc}" {"${Cyan}"--sign"${Nc}","${Cyan}"-s"${Nc}"}\t${Blue} Your-file "${Nc}
echo -e ${Cyan}"\tcrypto"${Nc}" {"${Cyan}"--verify"${Nc}","${Cyan}"-v"${Nc}"}\t${Blue} Your-file.asc "${Nc}
elif [ -z "$2" ]; then
echo -e ${Yellow}"\tError: Providing a filename is MANDATORY!   "${Nc}
echo -e ${Red}"\tExiting..."${Nc}
elif [ -d "$2" ]; then
echo -e ${Yellow}"\tError: File is a directory!"${Nc}
echo -e ${Red}"\tExiting..."${Nc}
else
case "$1" in
"--encrypt" | "-e")
## to encrypt (-e):
openssl aes-256-cbc -a -e -salt -in "$2" -out "$2".aes
;;
"--decrypt" | "-d")
## to decrypt (-d):
openssl aes-256-cbc -a -d -salt -in "$2" -out $(basename "$2" .aes)
;;
"--sign" | "-s")
## to sign (-s):
echo -e ${Cyan}"\tgpg -a --detach-sign \""$2"\""${Nc}
gpg -a --detach-sign "$2"
;;
"--verify" | "-v")
## to verify (-v):
echo -e ${Cyan}"\tgpg --verify \""$2"\" \"$(basename "$2" .asc)\""${Nc}
gpg --verify "$2" $(basename "$2" .asc)
;;
*)
echo
echo -e ${Yellow}"\tNothing done!"${Nc}
echo
;;
esac
fi
}
crypto

Which is a feature I often use to for example. to sign my post by emails to wordpress.com. Though the wordpress server removes the ‘asc’ attachment for reasons I completely understand, I’d like to make it clear that an authenticated post ensures precisely that. 😉 Fyi: This post (due to the amount of source code postings was done “the old-fashioned way”)

Anyway to stay on topic. I felt like pointing out while writing whatever it is I’d love to write about. I am still the lazy type, who prefers not to have to type too much. Hence all further source codes postings should be regarded as an useful aid to this lazy person. Of course, I’m certain there are better shortcuts to use/create, but that’s besides the point. If one were to imagine for just a second, how much time it would take to write everything (including but not limited to the use of all the html, which one doesn’t see because it’s in the background.) in this post by hand and multiply this by say 77, then I for one would be flabbergasted by the shear amount of effort this would take one to make! <- Now I don’t mind to work (hard), REALLY! But I do prefer to be efficient and for the remainder of my life will try to streamline this process (I’m certain though this will require a whole lot more of these so called “huge detours”). But you know what they say: It isn’t the destination but the journey that matters, right!?

Indeed the computer is the new c00l t00L.

Nuff said!

My $HOME/.bash_aliases:

# enable color support of ls and also add handy aliases
if [ "$TERM" != "dumb" ] && [ -x /usr/bin/dircolors ]; then
    eval "`dircolors -b`"
    alias l='ls --color=auto'
    alias l.='ls -d  --color=auto .*'
    alias la='ls -a --color=auto'
    alias ll='ls -l --color=auto'
    alias ld='ls -d --color=auto */'
    alias lh='ls -lh --color=auto'
    alias lx='ls -X --color=auto'
    alias lha='ls -lha --color=auto'
    alias list='ls -lhtFG --group-directories-first --color=auto'
    alias rlist='ls -lrhtFG --group-directories-first --color=auto'
    alias dir='dir --color=auto -clth'
    alias grep='grep --color=auto'
    alias grepi='grep -i --color=auto'
    alias grepin='grep -in --color=auto'        
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi
## Source: http://embraceubuntu.com/2005/09/07/adding-a-startup-script-to-be-run-at-bootup/
## Creating Daemons that start and stop on both system start and stop.
## Create:
# /etc/init.d/FOO
## Then:
# update-rc.d FOO defaults
## Or for a 'dry' run (note the -n switch):
# update-rc.d -n FOO defaults
## Finally do:
# chmod +x FOO

## This gives me Kind of a recursive feeling.  😉
alias resource='. $HOME/.bashrc'

## How to kill that annoying app.
alias kill9='kill -s 9' # PID DIE!
alias khup='kill -s 1' # PID HUP
alias gkill='kill -USR1' # PID Gently kill e.g. thttpd
alias kint='kill -s 2' # PID Violently kill e.g. thttpd

## First find its PID.
alias procid='ps aux | grep -i'

## Clear and reset the terminal in just one command
alias cnr='clear && reset'

## Clear, reset terminal and lock screen
alias lock='clear && reset && gnome-screensaver-command --lock'

## You have installed a new font in for example ~/.fonts \?
alias Sfcfonts='sudo fc-cache -f -v'

alias p='cd -' # previous directory
alias s='cd ..' # short for cd ..
alias cd..='cd ..' # allow omission of space in cd ..
alias rd='rmdir' # Remove a directory
alias md='mkdir' # Make a directory

## some other useful remove, copy and replace aliases.
alias rm='rm -i'
alias rmR='rm -R' # Use with caution\!
alias rmf='rm -f' # Use with caution\!
alias cp='cp -i'
alias mv='mv -i'

## My most common tar usage.
alias ztar='tar cvzf ' # Name.tgz folder/
alias utar='tar xvzf ' # Name.tgz folder/
alias gzip='gzip --best -v ' # Name of file or files when using a loop.

## Media related.
alias sniff='ffmpeg -i' #Name-of-file.
alias ultrafast='x264 --threads 0 --profile high --preset ultrafast --crf 18 --tune film --fps 25 --sar 4:3 -o ultrafast.mkv'
alias placebo='x264 --threads 0 --preset placebo --crf 18 --tune film --fps 25 --sar 4:3 -o placebo.mkv'

## Disk usage.
alias du='du -sh'
alias df='df -h'

## Did we already block this ip before\?
alias checkip='cat /etc/iptables.rules | grep'

## Enhanced copying.
alias rsync='rsync -avz --progress --delete'

## Local
#alias scripts='cd $HOME/.usr/bin/ && rlist'
alias pdf='cd $HOME/PDF/ && rlist'
alias gnupg='cd $HOME/.gnupg/ && rlist'
alias music='cd $HOME/Music/ && rlist'
alias videos='cd $HOME/Videos/ && rlist'
alias downloads='cd $HOME/Downloads/ && rlist'
alias documents='cd $HOME/Documents/ && rlist'
alias public='cd $HOME/Public/ && rlist'
#alias ssource='cd $HOME/.usr/src/ && rlist'
alias pictures='cd $HOME/Pictures/ && rlist'
alias macromedia='cd $HOME/.macromedia/Flash_Player/macromedia.com/support/flashplayer/sys && rlist'
alias sharedobjects='cd $HOME/.macromedia/Flash_Player/#SharedObjects && rlist'
alias Firefox='cd $HOME/.mozilla/firefox && rlist'

## Remote
alias mediad='cd /media/disk/ && rlist'
alias logfiles='cd /var/log/ && rlist'
alias www='cd /var/www/ && rlist'

## tofrodos (1.7.8.debian.1-2) unstable; urgency=low
alias unix2dos='/usr/bin/fromdos -u'
alias dos2unix='/usr/bin/fromdos -d'

## Macromedia flash specific
## So how many 'flash' cookies are there anyway.
alias findsol='find -iname '*.sol' | wc -l'
## This will remove all of them.
alias rmsol='find -iname '*.sol' -exec rm "{}" \;'
## This will prevent any site from setting any.
alias nosol='chmod -Rc 0500 ~/.macromedia/Flash_Player/#SharedObjects/ ~/.macromedia/Flash_Player/macromedia.com/support/flashplayer/sys/'
## And it's opposite of course.
alias allowsol='chmod -Rc 0750 ~/.macromedia/Flash_Player/#SharedObjects/ ~/.macromedia/Flash_Player/macromedia.com/support/flashplayer/sys/'

#Search and show available packages.
alias aptsearch='apt-cache search '
alias aptshow='apt-cache show '

## Link files that reside in the same folder or below.
## Useful for chrooted environments.
#alias link='ln -s -T' # target  name

## Timestamp your files and folders, if applicable.
alias dtouch='find . -type d -exec touch -t 201010100101.01 {} \;'
alias ftouch='find . -type f -exec touch -t 201010100101.01 {} \;'

## Rights and ownership managment.
alias pfchmod='find . -type f -exec chmod 600 {} \;'
alias prchmod='find . -type f -exec chmod 400 {} \;'
alias pxchmod='find . -type f -exec chmod u+x {} \;'
alias puchmod='find . -type f -exec chmod u-x {} \;'
alias pdchmod='find . -type d -exec chmod 1700 {} \;'
alias gfchmod='find . -type f -exec chmod 640 {} \;'
alias gdchmod='find . -type d -exec chmod 1750 {} \;'
alias ofchmod='find . -type f -exec chmod 644 {} \;'
alias odchmod='find . -type d -exec chmod 755 {} \;'
alias mexec='chmod u+x'
alias uexec='chmod u-x'
## The 'default' user upon installation.
## You could change this to your $USER name.
## sudo is required when for example the files are owned by root.
alias ch2admin='sudo chown 1000:1000 '
## If you've a 'daily user'.
alias ch2user='sudo chown 1001:1001 '
## Using perl is an option, but not required.
# alias ch2normal='perl -MFile::Find -e 'find sub { -f && chmod 0644, $_; -d && chmod 00755, $_ },"."''
# alias ch2web='perl -MFile::Find -e 'find sub { -f && chmod 0644, $_; -d && chmod 01755, $_ },"."''
# alias ch2personal='perl -MFile::Find -e 'find sub { -f && chmod 0600, $_; -d && chmod 01700, $_ },"."''
## And yet another very fast alternative.
# alias xargsf='find . -type f -print0 | xargs -0 chmod 644'
# alias xargsd='find . -type d -print0 | xargs -0 chmod 1750'

## Secure deletion tools.
# Requires 'secure-delete' - Install secure-delete
alias Smem='sudo smem -v'
alias qdel='srm -lv'
alias qrdel='srm -lvr'
alias qhist='srm -lv $HOME/.bash_history && touch $HOME/.bash_history'

## What have we logged today.
alias ltcpo='grep -i denied\ outgoing\ tcp /var/log/syslog'
alias ludpo='grep -i denied\ outgoing\ udp /var/log/syslog'
alias licmpo='grep -i denied\ outgoing\ icmp /var/log/syslog'
alias ltcp='grep -i denied\ tcp /var/log/syslog'
alias ludp='grep -i denied\ udp /var/log/syslog'
alias licmp='grep -i denied\ icmp /var/log/syslog'
alias ltcp6='grep -i denied\ ipv6\ tcp /var/log/syslog'
alias ludp6='grep -i denied\ ipv6\ udp /var/log/syslog'
alias licmp6='grep -i denied\ ipv6\ icmp /var/log/syslog'

## Firefox optimizations
alias optimzeff='killall firefox && find $HOME/.mozilla/ \( -name "*.sqlite" \) -exec sqlite3  {} "vacuum" \;'
## '12345678' ? is the 8 character gibberish which precedes your possible firefox profile-name ('default' in this case).
alias optdef='sqlite3 $HOME/.mozilla/firefox/12345678.default/places.sqlite "vacuum"'

## Show my open connections
alias lsofu='lsof -i -n -P'

## Comes out as: Dow, Month Day, Year
## e.g.: Saturday, July 21, 2012
alias todayis='date +"%A, %B %-d, %Y"'

## Neatly format text files, or any other text based file.
alias fmt='fmt --w 80 -u'
alias nano='nano -Amu'
###############################################################################
## aliases that Require Root privileges\!
## List firewall rules.
alias Arptables='sudo arptables -L -n -v'
alias Ip6tables='sudo ip6tables -L -n -v'
alias Iptables='sudo iptables -L -n -v'

## Block a single Ip-address.
# E.g.: BlIp 1.2.3.4
alias Blip='sudo iptables -I blacklist 1 -j REJECT --reject-with icmp-host-prohibited -s'

## Block 'whole net'.
## E.g.: BlNet 1.2.3.0/24
alias Blnet='sudo iptables -I blacklist 1 -j REJECT --reject-with icmp-net-prohibited -s'
## This one deletes a rule
# iptables -D blacklist &lt;rulenr&gt;

## Show open connections from other users.
alias Lsof1='sudo -u user1 lsof -i -n -P'
alias Lsof2='sudo -u user2 lsof -i -n -P'

## Mount helpers
alias Bind='sudo mount -o bind' # /sourcefolderurl /destinationfolderurl
alias Iso='sudo mount -o loop' # /path/to/iso /media/iso/name_of_folder
alias Umount='sudo umount' # /destinationfolderurl

## [S]start|[K]stop|[U]status service(s)
## Requires 'boinc' - Install boinc
# alias Sboinc='sudo service boinc-client start'
# alias Kboinc='sudo service boinc-client stop'
# alias Uboinc='sudo service boinc-client status' # Update/Status

## Keeping the system up to date
## man apt-get.
alias Aptall='sudo apt-get update && sudo apt-get -y upgrade && sudo apt-get autoclean && sudo apt-get autoremove'
alias Update='echo "sudo apt-get update " && sudo apt-get update'
alias Upgrade='echo "sudo apt-get upgrade " && sudo apt-get upgrade'
alias Autoclean='echo "sudo apt-get autoclean " && sudo apt-get autoclean'
alias Clean='echo "sudo apt-get clean " && sudo apt-get clean'
alias Autoremove='echo "sudo apt-get autoremove " && sudo apt-get autoremove'
alias Remove='echo "sudo apt-get remove " && sudo apt-get remove'
alias Purge='echo "sudo apt-get purge " && sudo apt-get purge'
alias Install='echo "sudo apt-get install " && sudo apt-get install'

My $HOME/.bash_functions:
(I’ve updated the following for the last time 8/4/2012)

## The mandatory: "Hello World" ; - )
hello()
{
  Who="So, who am I!?"
  echo -en ${Blue}"\t${Who}\t:\t"${Nc}
  read Answer
  What="Who's but the form following the function of what and what I'm is a man in a mask."
  if [ "$Answer" = "What" ]; then echo -e ${Cyan}"\tYou my main man, just passed this test. ;-)\n\t${Yellow}Because: ${What}"${Nc}
  else
  echo -e ${Red}"\tYou SHALL NOT pass!"${Nc}
  fi
}
## Reset flash
reset.flash()
{
# $HOME/.macromedia/Flash_Player/#SharedObjects
# $HOME/.macromedia/Flash_Player/macromedia.com/support/flashplayer/sys
\rm -r $HOME/.macromedia/Flash_Player/macromedia.com/support/flashplayer/sys/\#* || echo "Nothing found!"
\rm -r $HOME/.macromedia/Flash_Player/#SharedObjects/* || echo "Nothing found!"
}
## Tidy up any html file with 'tidy'
mtidy()
{
config="$HOME/.usr/etc/config.txt"
tidy -config $config $1
}
rtidy()
{
config="$HOME/.usr/etc/config.txt"
find -iname '*.html' -exec tidy -config "$config" '{}' \;
}
link()
{
## Link files that reside in the same folder or below.
## Useful for chrooted environments.
URLENc=$(echo "$1" | sed -e "s_%20_\\ _g")
if [ -n "${URLENc}" ];then Arg=${URLENc/file:\/\//};else Arg="${Arg}";fi
MOVED=$(echo "${Arg}" | sed -e "s_\\ _._g")
if [ -f "${MOVED}" ];then Arg="${Arg}";else mv "${Arg}" "${MOVED}" 2>/dev/null;Arg=${MOVED};fi
ln -s -T "$Arg" "$2" # target  name
}
## Rip from wmv to xvid in avi container directly.
## Please note amongst things the -s switch here!
## I.e.: Please, do a...
# ffmpeg -i "$YOUR.INPUT" to show you the resolution, rate, aspect ratio and other useful telltale signs. ; - )
wmv2xvid.hd720()
{
WD=./ripped
FOLDER="$WD"/$(basename "$1" .wmv)
[ ! -d "$FOLDER" ] && mkdir -p "$FOLDER"
ffmpeg -y -threads 2 -i "$1" -f avi -r 25 -vcodec libxvid -vtag XVID -s hd720 -aspect 16:9 -maxrate 1800k -b 1400kb -qmin 3 -qmax 5 -bufsize 4096 -mbd 2 -bf 2 -flags +4mv -trellis -aic -cmp 2 -subcmp 2 -g 300 -an $(basename "$1" .wmv).xvid.avi
}
## Rip from wav to mp3.
wav2mp3()
{
ffmpeg -y -threads 2 -i "$1" -f mp3 -acodec libmp3lame -ac 2 -ab 320k $(basename "$1" .wav).mp3
}
## Rip from wav to ogg.
wav2ogg()
{
oggenc "$1" -q 9
}
## "panavision" : - )
upscale()
{
mencoder "$1" -endpos 00:00:20 -oac copy -ovc xvid -xvidencopts \
chroma_opt:vhq=4:bvhq=1:quant_type=mpeg:bitrate=1400 \
-vf scale=1280:720 \
-o $(basename "$1" .avi).mp.HD.avi
}
## Evolvotron specific.
# Requires 'evolvotron' - Install evolvotron
#evolve()
#{
#evolvotron_render -f 96 -r 8 -j -a -m 4 -s 720 576 -spheremap "$1".ppm &lt; $HOME/Downloads/vid.edit/evolved0.xml
#}
#evolvehd()
#{
#ffmpeg -y -b 3000k -r 8 -f image2 -i "$1".f%6d.ppm -qscale 4 -qblur 1.0 -aspect 16:9 -g 300 -s hd720 "$1".avi
#}
#evolvepal()
#{
#ffmpeg -y -b 3000k -r 8 -f image2 -i "$1".f%6d.ppm -qscale 4 -qblur 1.0 -aspect 16:9 -g 300 "$1".avi
#}
## This function will simulate the effect the "mod_deflate" module for Apache has, for the same named files.
## Only to be used with a server who understands the .gz format out of the box: I.e. thttpd
## Also works in a chroot'ed environment. ; - )
massedit()
{
if [ $USER = root ]; then
echo -e ${Red}"\tAre you Insane!\n${Cyan}\tError: In order to use this script, one must NOT be $USER.\n${Yellow}\tExiting..."${Nc}
exit 0
else
echo -e ${Blue}"\t${USER} may proceed.\n${Cyan}\tMay peace be with you."${Nc}
fi
clear
for files in {*.css,*.js,*.html,*.txt,*.xml,*.doc,*.odt,*.pdf};
do
if [ -f "$files" ];then
ls "$files" 2>/dev/null;
gzip --best -v "$files"
ln -s -T "$files".gz "$files"
fi
done
}
## This one removes the empty lines and unnecessary spaces,
## of the text based type of file,
## you gave it to chew on. ; - )
squeeze()
{
if [ $USER = root ]; then
echo -e ${Red}"\tAre you Insane!\n${Cyan}\tError: In order to use this script, one must NOT be $USER.\n${Yellow}\tExiting..."${Nc}
exit 0
else
echo -e ${Blue}"\t${USER} may proceed.\n${Cyan}\tMay peace be with you."${Nc}
fi
clear # Clear the screen.
if [ ! -e "$1" ]
then
echo -e ${Yellow}"$1 does not exist."${Nc}; echo
else
## /^$/d Removing those empty lines for you as well.
## s/[ \t]*$// This removes the trailing spaces in your doc.
sed -e '/^$/d' -e 's/[ \t]*$//' -i "$1"
fi
}
crypto()
{
if [ -z "$1" ]; then
echo -e ${Blue}"\tPlease choose a valid option!\n${Yellow}\tUsage:"${Nc}
echo -e ${Cyan}"\tcrypto"${Nc}" {"${Cyan}"--encrypt"${Nc}","${Cyan}"-e"${Nc}"}\t${Blue} Your-file "${Nc}
echo -e ${Cyan}"\tcrypto"${Nc}" {"${Cyan}"--decrypt"${Nc}","${Cyan}"-d"${Nc}"}\t${Blue} Your-file.aes "${Nc}
echo -e ${Cyan}"\tcrypto"${Nc}" {"${Cyan}"--sign"${Nc}","${Cyan}"-s"${Nc}"}\t${Blue} Your-file "${Nc}
echo -e ${Cyan}"\tcrypto"${Nc}" {"${Cyan}"--verify"${Nc}","${Cyan}"-v"${Nc}"}\t${Blue} Your-file.asc "${Nc}
elif [ -z "$2" ]; then
echo -e ${Yellow}"\tError: Providing a filename is MANDATORY!   "${Nc}
echo -e ${Red}"\tExiting..."${Nc}
elif [ -d "$2" ]; then
echo -e ${Yellow}"\tError: File is a directory!"${Nc}
echo -e ${Red}"\tExiting..."${Nc}
else
case "$1" in
"--encrypt" | "-e")
## to encrypt (-e):
openssl aes-256-cbc -a -e -salt -in "$2" -out "$2".aes
;;
"--decrypt" | "-d")
## to decrypt (-d):
openssl aes-256-cbc -a -d -salt -in "$2" -out $(basename "$2" .aes)
;;
"--sign" | "-s")
## to sign (-s):
echo -e ${Cyan}"\tgpg -a --detach-sign \""$2"\""${Nc}
gpg -a --detach-sign "$2"
;;
"--verify" | "-v")
## to verify (-v):
echo -e ${Cyan}"\tgpg --verify \""$2"\" \"$(basename "$2" .asc)\""${Nc}
gpg --verify "$2" $(basename "$2" .asc)
;;
*)
echo
echo -e ${Yellow}"\tNothing done!"${Nc}
echo
;;
esac
fi
}
## Requires 'autowhite' and 'phatch'\!
#batch()
#{
#case "$1" in
#"png")
#for png in *.png;do convert "$png" -quality 90% $(basename "$png" .png).jpg;done
#\rm *.png
#for jpg in *.jpg;do autowhite -m 1 -p 1 "$jpg" $(basename "$jpg" .jpg).mp11.jpg;done
## do convert -enhance -auto-level -unsharp 0x1,414+2.0+0.0078 "$jpg" $(basename "$jpg" .mp11.jpg).jpg;done
#for jpg in *.mp11.jpg;do \mv "$jpg" $(basename "$jpg" .mp11.jpg).jpg;done
## \rm *.mp11.jpg
#for jpg in *.jpg;do mogrify -resize 1900x "$jpg";done
#;;
#"jpg")
#for jpg in *.jpg;do autowhite -m 1 -p 1 "$jpg" $(basename "$jpg" .jpg).mp11.jpg;done
## do convert -enhance -auto-level -unsharp 0x1,414+2.0+0.0078 "$jpg" $(basename "$jpg" .mp11.jpg).jpg;done
#for jpg in *.mp11.jpg;do \mv "$jpg" $(basename "$jpg" .mp11.jpg).jpg;done
## \rm *.mp11.jpg
#for jpg in *.jpg;do mogrify -resize 1900x "$jpg";done
#;;
#"*")
#echo -e ${YELLOW}"\tPlease provide a valid option"${NC}
#;;
#esac
#case "$2" in
#"warm")
#phatch $HOME/.local/share/phatch/actionlists/warm.up.phatch *.jpg
#;;
#"perspective")
#phatch $HOME/.local/share/phatch/actionlists/perspective.phatch *.jpg
#;;
#"reflections")
#phatch $HOME/.local/share/phatch/actionlists/reflections.phatch *.jpg
#;;
#"*")
#exit 0
#;;
#esac
#if [ -z "$3" ];then exit 0;fi
#if [ -z "$4" ];then echo -e ${YELLOW}"\tPlease provide a name for the pdf file."${NC}; exit 0;fi
#case "$3" in
#"pdf")
#convert *.jpg "$4".pdf
#;;
#"*")
#exit 0
#;;
#esac
#}
png2pdf()
{
clear
convert *.png -quality 100 output.pdf
# -page A4
}
pdf2png()
{
clear
for pdf in *.{pdf,pdf.gz}; do convert -set -units PixelsPerCentimeter -density 120x120 -quality 90 $pdf $(basename $pdf .pdf).png;done
}
strip()
{
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${Cyan}\tMay peace be with you.\n"${Nc};fi
Color_Test=$(grep -i '\\033\[' "$HOME"/.bashrc)
if [ -n "$Color_Test" ];then echo -e ${Magenta}"\tOk, colors are defined."${Nc}
else
Black="\033[0;30m"
Red="\033[0;31m"
Green="\033[0;32m"
Yellow="\033[1;33m"
Blue="\033[1;34m"
Magenta="\033[1;35m"
Cyan="\033[1;36m"
White="\033[1;37m"
Nc="\033[0m" # No Color
echo -e ${White}"\tThey weren't, but now they are."${Nc}
fi
## ♪ ♫ Here's little mod_rewrite I wrote. ♫ ♪ You might want to sing it ♪ for ♪ ...
clear
sed_txt="$HOME/.usr/etc/sed.txt"
if [ -z "$1" ];then
echo -e ${Blue}"\t(Hit \"${Yellow}ctrl+shift+v${Blue}\" to paste.)\n"${Nc}
echo -en ${Yellow}"\tPlease enter your url: "${Nc}
read Name
  if [ -z "$Name" ];then echo -e ${Red}"\tPlease provide me with a string to work on."${Nc}
  else
  if [ -n "$Name" ]; then Name="$Name";Name="$(echo "$Name" | sed -f "$sed_txt")";fi
  fi
fi
case "$Name" in
  "$(echo "$Name" |grep 'youtube.com')" )
  Watch="$(echo "$Name" |sed -e 's_http://_https://_' -e 's_watch?\(.*\)v=_watch?v=_' -e 's_\&\(.*\)__')"
  echo -e ${Magenta}"\t$Watch"${Nc}
  ;;
  "$(echo "$Name" |grep 'facebook.com')")
  Secure="$(echo "$Name" |sed -e 's_http://_https://_')"
  echo -e ${Magenta}"\t"$Secure""${Nc}
  ;;
  "$(echo "$Name" |grep 'google.com')" | "$(echo "$Name" |grep 'google.nl')")
  Secure="$(echo "$Name" |sed -e 's_http://_https://_')"
  echo -e ${Magenta}"\t"$Secure""${Nc}
  ;;
  "$(echo "$Name" |grep 'wordpress.com')")
  Secure="$(echo "$Name" |sed -e 's_http://_https://_')"
  echo -e ${Magenta}"\t"$Secure""${Nc}
  ;;  
  "$(echo "$Name" |grep 'yahoo.com')")
  Not_secure="$(echo "$Name" |sed -e 's_http://_https://_')"
  echo -e ${Cyan}"\t"$Not_secure""${Nc}
  ;;  
  "$(echo "$Name" |grep 'bit.ly')" | "$(echo "$Name" |grep 'bitly.com')")
  Bitly=$(echo "$Name" | sed -e 's_http://_https://_' -e 's_bit.ly_bitly.com_')
  wget "$Bitly+"
  Item=$(grep 'item_title' *+)
  Item=$(echo "$Item" | sed -e 's_\(.*\)a href="__')
  Url=$(echo "$Item" | sed -e 's_">\(.*\)__')
  echo -e ${White}"\t"$Url""${Nc}
  \rm *+
  ;;
  "*")
  echo -e ${Yellow}"\t$1"${Nc}
  ;;
esac
}
falias()
{
if [ -z "$1" ]; then echo -e ${Yellow}"\tPlease give me something to work with here. :-)"${Nc}
else
grep -i "$1" $HOME/.bash{rc,_aliases,_functions,_history} 2>/dev/null
fi
}
gnu2nt()
{
fmt --w 80 -u "$1">$(basename "$1" .txt).fmt
fromdos -vebu $(basename "$1" .txt).fmt
\mv $(basename "$1" .txt).fmt "$1"
}
sniff()
{
## We might not want to run this as root!?
if [ $USER = root ]; then
echo -e ${Red}"\tAre you Insane!\n${Cyan}\tError: In order to use this script, one must NOT be $USER.\n${Yellow}\tExiting..."${Nc}
exit 0
else
echo -e ${Blue}"\t${USER} may proceed.\n${Cyan}\tMay peace be with you."${Nc}
fi
clear
## If no argument given, then assume the user wants to run things interactively.
if [ -z "$1" ];then
echo -e ${Blue}"\t(Hit \"${Yellow}ctrl+shift+v${Blue}\" to paste.)\n"${Nc}
echo -en ${Yellow}"\tPlease enter your url/file: "${Nc}
read Arg
  if [ -z "$Arg" ];then echo -e ${Red}"\tPlease provide me with a string to work on."${Nc}
  else 
  [ -n "$Arg" ];export Arg="$Arg"
  fi
fi
## Let's for now assume that the first argument passed is the filename.
## I'll work on a somewhat more flexible 'small program'
## (which a function actually is) later. ; - )
if [ -n "$1" ];then Arg="$1";fi
## If the 'Arg' is copied from nautilus and then pasted into 'gnome-terminal':
## Then this leads to: an 'argument' with 'ugly' spaces.
## And 2: A trailing space at the end of the 'argument'.
#
## Let's change that:
## First we will remove the 'file:///' part from your string and give you '/' in its stead.
## Because Bash or perhaps 'gnome-terminal' doesn't understand this.
Arg="${Arg/file:\/\//}"
#
## Second: If you're lazy like me, then you're most likely to have forgotten
## to remove the trailing space.
## Doesn't happen when using 'konsole'!
## The following 'undo's this particular 'damage'. ; - )
Arg=${Arg%\ }
#
## I use the following folder/directory for convenience's sake.
## E.g. the files used by sed are placed here.
if [ ! -d "$HOME/.usr/etc" ];then mkdir -p "$HOME"/.usr/etc;fi
## Let's define those files then.
sed_in=$HOME/.usr/etc/sed.in.txt
sed_out=$HOME/.usr/etc/sed.out.txt
## And create them, if necessary.
if [ ! -f "$sed_in" ];then
cat >>"$sed_in"<<_EOF_
s/%20/\ /g
s/%23/\#/
s/%26/\&/g
s/%2B/\+/
s/%2C/\,/g
s/%3A/\:/
s/%3B/\;/g
s/%3D/\=/g
s/%3F/\?/g
s/%5B/\[/g
s/%5D/\]/g
s/%7B/\{/g
s/%7D/\}/g
s/-/\-/g
s/(/\(/g
s/)/\)/g
_EOF_
fi
if [ ! -f "$sed_out" ];then
cat >>"$sed_out"<<_EOF_
s/ - /./g
s/ /./g
s/.\&//g
s/\&//g
s/.{/.[/g
s/}./]./g
s/.(/_/g
s/)./_/g
s/,_/_/g
s/</[/g
s/>/]/g
s/\],/\]/g
s/\.\.\[/\.\[/g
s/\._/_/g
s/_\./_/g
_EOF_
fi
#
## All the above implies, that we can now work with,
## e.g. files that contain 'ugly' spaces. YaY
## (i.e. files with embedded '%20's)
#
## And files that have names like:
# 'my-file with spaces and {curly}, [square], <chevron> and (parantheses) which are - of _type_ bracket.mkv'
#
## Let's get to work shall we.
## This will 'ready' the string.
Arg=$(echo "$Arg" | sed -f "$sed_in")
## The following:
## Might be(come) useful, when we're going to 'quote' or 'scale' the input later on.
# e.g. 'my-file with spaces and {curly}, [square], &lt;chevron&gt; and (parantheses) which are - of _type_ bracket.mkv'
# Will have: 'my-file.with.spaces.and.[curly].[square].[chevron].and_parantheses_which.are.of_type_bracket.mkv',
# as possible 'Output' name.
Output=$(echo "$Arg" | sed -f "$sed_out")
## Since we only want to use a few 'fragments' of ffmpeg's output
## (which is 'standard error' in this case).
duration="$(ffmpeg -i "${Arg}" 2>&1| sed -n "s/^.*Duration: \([^,]*\),.*/\1/p"|head -n 1)"
audio="$(ffmpeg -i "${Arg}" 2>&1| sed -n "s/^.*Audio: \([^,]*\),.*/\1/p"|head -n 1)"
video="$(ffmpeg -i "${Arg}" 2>&1| sed -n "s/^.*Video: \([^,]*\),.*/\1/p"|head -n 1)"
size="$(ffmpeg -i "${Arg}" 2>&1 | grep -i 'video' | awk '{print $6}' | cut -d, -f1 | tail -n 1)"
## Surely there must be a more elegant solution to this?
Fps="$(ffmpeg -i "${Arg}" 2>&1 | grep "[[:digit:]]*.[[:digit:]]*\ tbr" | sed -e 's/tbr\(.*\)//' -e 's/\(.*\),//' | awk -F ' ' '{print $1}')"
## Now let's break the 'Arg' up into useful pieces for later use.
File=$(basename "$Arg")
## E.g.:
# If File='my-file.avi.avi', then
## 1 % matches shortest range.
# ${File%.*} becomes my-file.avi
## 2 %% matches longest range.
# ${File%%.*} becomes my-file
Base=$(basename "${File%.*}")
Ext=${File/"$Base"}
## You might want to experiment a bit here!?
## I'm a proponent of using the 'matroska' container
## (if not in all cases). ; - )
#
## This implies the resulting 'Output' to be posted into the working directory.
## Which is what you'd most likely want.
## I.e. the 'Output' has become a safe copy to work on.
Output=$(basename "$Output" "$Ext").mkv
## This might seem somewhat 'redundant' and perhaps it is?
## Feel free to adjust things to your own needs!
## I'd say: 'Gotta catch them all...' : - D
if [ -z "${Arg}" ]; then echo -e ${Yellow}"\tProviding a filename is mandatory."${Nc}
elif [ ! -f "${Arg}" ]; then echo -e ${Yellow}"\tFile doesn't exist."${Nc}
elif [ -z "$video" ] && [ -z "$audio" ]; then echo -e ${Red}"\tThis is NOT a media file."${Nc}
else
  hours=$(echo $duration | cut -d: -f1)
  if [[ "$hours" = 00 ]];then hours="0";else hours=$(( $(echo $hours | cut -d'0' -f2) * 3600 ));fi
  minutes=$(echo $duration | cut -d: -f2)
  if [[ "$minutes" = 00 ]];then minutes="0"
  elif [[ "$minutes" < 10 ]];then minutes=$(( $(echo $minutes | cut -d'0' -f2) * 60 ))
  else minutes=$(($minutes * 60))
  fi
  seconds=$(echo $duration | cut -d: -f3)
  seconds=$(echo $seconds | cut -d. -f1)
  mseconds=$(echo $duration | cut -d. -f2)
  if [[ "$seconds" = 00 ]];then seconds=0
  elif [[ "$seconds" < 10 ]];then seconds=$(echo $seconds | cut -d'0' -f2)
  else seconds="$seconds"
  fi
  total=$((hours + minutes + seconds))."$mseconds"
  split=$((hours + minutes + seconds))
  ## Set a limit, to the amount we can 'quote', for later on.
  export Max_duration="$split"
  ## And set a minimum for the same purpose.
  export Min_duration="00:00:00.00"  
  echo
  echo -e ${Magenta}"\tThe argument's full path:\n\t${Cyan}"$Arg""${Nc}
  echo -e ${Magenta}"\tFilename:\n\t${Cyan}"$File""${Nc}
  echo -e ${Magenta}"\tWhen stripped from its suffix:\n\t${Cyan}"$Base""${Nc}
    echo -e ${Magenta}"\tThe file is stored in the {Container,Format}:\n\t${Cyan}"$Ext""${Nc}
  echo -e ${Magenta}"\tPotential outfile:\n\t${Cyan}"$Output"\n\r"${Nc} 
  echo -e ${Magenta}"\tFrames/s:\t${Cyan}"$Fps""${Nc}
  echo -e ${Magenta}"\tIts resolution:\t${Cyan}"$size""${Nc}  
  echo -e ${Magenta}"\tIts duration:\t${Cyan}"$duration" or "$total seconds""${Nc}
  if [ -z "$video" ];then echo -e ${Red}"\tVideo-codec:\tNo video stream found."${Nc};export result=audio_only
  else echo -e ${Magenta}"\tVideo-codec:\t${Cyan}"$video""${Nc}
  fi
  if [ -z "$audio" ];then echo -e ${Red}"\tAudio-codec:\tNo audio stream found."${Nc};export result=video_only
  else echo -e ${Magenta}"\tAudio-codec:\t${Cyan}"$audio""${Nc}
  fi
  echo
fi
## The following might be useful to
## 'repackage' files created with e.g. gtk-recordMyDesktop?
## Your mileage may vary though!?
## This also requires both mkvmerge && gtk-recordmydesktop to be installed though.
# sudo apt-get install mkvtoolnix mkvtoolnix-gui gtk-recordmydesktop
#case "$2" in
#"repack")
#if [ "$Ext" = ".ogv" ];then
#mkvmerge -o \
#"${Output}" \
#--language 1:eng --track-name 1:"${Base}." \
#--forced-track 1:no --display-dimensions 1:"${size}" \
#--language 2:eng --track-name 2:"${Base}." \
#--forced-track 2:no \
#-a 2 -d 1 -S -T --no-global-tags --no-chapters \
#"${Arg}" --track-order 0:1,0:2 --title "${Base}. ™"
#fi
#;;
#*)
#;;
#esac
}

The tidy config as mentioned up above:

// 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

How did this lovely post came to be!?: 😉
This ‘global.xml’ snippet will only work in Gedit though.
(But I felt like sharing anyway. :lol:)

<?xml version='1.0' encoding='utf-8'?>
<snippets>
  <snippet>
    <text><![CDATA[[sourcecode language="bash"]
$1
[/sourcecode]
$0]]></text>
    <tag>wpb</tag>
    <description>sourceb</description>
  </snippet>
  <snippet>
    <text><![CDATA[> $1

$0]]></text>
    <tag>bq</tag>
    <description>blockquote</description>
  </snippet>
  <snippet>
    <text><![CDATA[[$1]($2 "$3") $0]]></text>
    <tag>ref</tag>
    <description>ahref</description>
  </snippet>
  <snippet>
    <text><![CDATA[<a target="_blank" title="$1" href="$2">$3</a>$0]]></text>
    <tag>ablank</tag>
    <description>ablank</description>
  </snippet>
  <snippet>
    <text><![CDATA[**$1** $0]]></text>
    <tag>b</tag>
    <description>strong</description>
  </snippet>
  <snippet>
    <text><![CDATA[_$1_ $0]]></text>
    <tag>i</tag>
    <description>em</description>
  </snippet>
  <snippet>
    <text><![CDATA[*  $1
*  $2
*  $3
*  $4

$0]]></text>
    <tag>ul</tag>
    <description>ul</description>
  </snippet>
  <snippet>
    <text><![CDATA[“$0”]]></text>
    <tag>dq</tag>
    <description>dquo</description>
  </snippet>
  <snippet>
    <text><![CDATA[1.  $1
2.  $2
3.	$3
4.	$4

$0]]></text>
    <tag>ol</tag>
    <description>ol</description>
  </snippet>
  <snippet>
    <text><![CDATA[<sup>$1</sup>$0]]></text>
    <tag>sup</tag>
    <description>sup</description>
  </snippet>
  <snippet>
    <text><![CDATA[<sub>$1</sub>$0]]></text>
    <tag>sub</tag>
    <description>sub</description>
  </snippet>
  <snippet>
    <text><![CDATA[#### $1

$0
]]></text>
    <tag>chap</tag>
    <description>chapter</description>
  </snippet>
  <snippet>
    <text><![CDATA[<a title="$1">$2</a>$0]]></text>
    <tag>atitle</tag>
    <description>atitle</description>
  </snippet>
  <snippet>
    <text><![CDATA[$1
*[$1]: $2

$0]]></text>
    <tag>abbr</tag>
    <description>abbr</description>
  </snippet>
  <snippet>
    <text><![CDATA[<acronym title="$1">$2</acronym>$0]]></text>
    <tag>acro</tag>
    <description>acro</description>
  </snippet>
  <snippet>
    <text><![CDATA[<p>$1</p>]]></text>
    <tag>p</tag>
    <description>p</description>
  </snippet>
  <snippet>
    <text><![CDATA[<table summary="$1" border="$2">
$3
</table>
$0]]></text>
    <tag>table</tag>
    <description>table</description>
  </snippet>
  <snippet>
    <text><![CDATA[<tr>
	$1
</tr>
$0]]></text>
    <tag>tr</tag>
    <description>tr</description>
  </snippet>
  <snippet>
    <text><![CDATA[<td>$1</td>
$0]]></text>
    <tag>td</tag>
    <description>td</description>
  </snippet>
  <snippet>
    <text><![CDATA[«$1»$0]]></text>
    <tag>aq</tag>
    <description>aquo</description>
  </snippet>
  <snippet>
    <text><![CDATA[[$1]: $2 "$3"
![Alt $3][$1]

$0]]></text>
    <tag>img</tag>
    <description>img</description>
  </snippet>
  <snippet>
    <text><![CDATA[$1  
$0]]></text>
    <tag>br</tag>
    <description>br</description>
  </snippet>
  <snippet>
    <text><![CDATA[##### $1

$0]]></text>
    <tag>sec</tag>
    <description>section</description>
  </snippet>
  <snippet>
    <text><![CDATA[[audio $1]$0]]></text>
    <tag>audio</tag>
    <description>audio</description>
  </snippet>
  <snippet>
    <text><![CDATA[***

$0]]></text>
    <tag>hr</tag>
    <description>hr</description>
  </snippet>
  <snippet>
    <text><![CDATA[ 1
$
$0]]></text>
    <tag>latex</tag>
    <description>LaTeX</description>
  </snippet>
  <snippet>
    <text><![CDATA[<pre class="prettyprint">
<code>
$1
</code>
</pre>
$0]]></text>
    <tag>pp</tag>
    <description>prettyprint</description>
  </snippet>
  <snippet>
    <text><![CDATA[<a name="_top"></a>
$0
<em><a href="#_top">Back to top.</a></em>]]></text>
    <tag>anchor</tag>
    <description>anchor</description>
  </snippet>
  <snippet>
    <text><![CDATA[[sourcecode language="text" light="true"]
$1
[/sourcecode]
$0]]></text>
    <tag>wpt</tag>
    <description>text</description>
  </snippet>
  <snippet>
    <text><![CDATA[$1… $0]]></text>
    <tag>dot</tag>
    <description>hellip</description>
  </snippet>
  <snippet>
    <text><![CDATA[»$1«$0]]></text>
    <tag>iq</tag>
    <description>iquo</description>
  </snippet>
  <snippet>
    <text><![CDATA[[^$1] $2 
[^$1]: [$3]($4 "$3")

$0
]]></text>
    <tag>cite</tag>
    <description>cite</description>
  </snippet>
  <snippet>
    <text><![CDATA[$1
:  $2

$0]]></text>
    <tag>dl</tag>
    <description>define</description>
  </snippet>
  <snippet>
    <text><![CDATA[<$1>$0]]></text>
    <tag>ab</tag>
    <description>Angle brackets</description>
  </snippet>
  <snippet>
    <text><![CDATA[<!--
$1
-->
$0]]></text>
    <tag>cm</tag>
    <description>comment (html)</description>
  </snippet>
  <snippet>
    <text><![CDATA[—$1—$0]]></text>
    <tag>em</tag>
    <description>mdash</description>
  </snippet>
  <snippet>
    <text><![CDATA[https://duckduckgo.com/?q=$1 $0]]></text>
    <tag>qddg</tag>
    <description>Query</description>
  </snippet>
  <snippet>
    <text><![CDATA[‘$1’$0]]></text>
    <tag>sq</tag>
    <description>squo</description>
  </snippet>
  <snippet>
    <text><![CDATA[$1’$0]]></text>
    <tag>q</tag>
    <description>quo</description>
  </snippet>
  <snippet>
    <text><![CDATA[$1 \
$0]]></text>
    <tag>bs</tag>
    <description>backslash</description>
  </snippet>
</snippets>
Act, don't think.

“ Act, don’t think … (Drink!) ! ”


That’s all folks! 😆

Kindest regards,

Alex

ღ Ƹ̵̡ Ӝ̵̨̄ Ʒ ☆ ˜ ” * ° • . ¸ł¸ . • ° * ” ☆ ★ ☆ ę ˜

♫ d(。◕‿◕。)b ♪♪

Use the contact-form below, for feedback purposes.







Back to top.

2 thoughts on “The usefulness of bash_aliases and bash_functions.

  1. We absolutely love your blog and find the majority of your post’s to be precisely what I’m looking for. Would you offer guest writers to write content for you? I wouldn’t mind creating a post or elaborating on a lot of the subjects you write regarding here. Again, awesome web log!

    Like

Comments are closed.