parsed.org

Tips by tag: bash

Convert Imagesize and Output Html-Code by -Flo- on Feb 19, 2008 09:12 AM
I wrote a little Bash-script, that
  • Creates Thumbnails to a specific size
  • Converts original Images to a specific size
  • Creates Html-Code that will show the thumbnail and link to the image (Folders can be included)

Have fun with it:

#!/bin/bash

######################
# Set Variables
######################
ENDING=jpg;
IMG_SIZE=800;
THUMB_SIZE=150;
DIR=folder/folder/images/;
ID=/usr/bin/identify;

######################
# Create Thumbnail
######################
ls -1 *.$ENDING | grep -v thb |while read file;
do convert -thumbnail $THUMB_SIZEx$THUMB_SIZE "$file" "`basename _thb_"$file"`";
        echo "---> _thb_$file created";
done

######################
# Convert image
######################
ls -1 *.$ENDING | grep -v thb | while read file;
do convert -resize $IMG_SIZEx$IMG_SIZE "$file" "`basename "$file"`";
        echo "---> $file converted";
done

######################
# Output HTML-Code
######################
echo "######################";
echo "HTML-CODE:";
echo "######################";
ls -1 *.$ENDING | grep -v thb | while read file;
do
        h=$($ID -format \"%h\" "`basename _thb_"$file"`");
        w=$($ID -format \"%w\" "`basename _thb_"$file"`");
        echo "<a href=\""$DIR$file"\">";
        echo "<img alt=\"\" src=\""$DIR""`basename _thb_"$file"`"\" width=$w height=$h /></a>";
done
echo "######################";
bashconverthtmlimagethumbnail
Correcting Command by xinu on Feb 03, 2005 03:30 PM

If you've run a command that you discover needs a path, you can do something like this on the following line:

$ psql -U postgres mydb_here
bash: psql: command not found

$ /usr/local/pgsql/bin/!!
/usr/local/pgsql/bin/psql -U postgres mydb_here

Welcome to psql 7.3.5, the PostgreSQL interactive terminal.
mydb_here=#
bashcommandsneat
Halting a Script After Command Failure by cygnus on Dec 12, 2005 10:59 AM

A shell script which runs a sequence of commands will run them all even if some of them fail. This is rarely desirable, as later commands will usually depend on the success of prior ones.

You can use && between commands to implement this dependency, of course, or you can use the -e switch on the shebang line (works with bash, may work with other shells) to cause the interpreter to halt execution after any command returns a non-zero exit code:

#!/.../bash -e
bashhaltlogicoperatorsshell
No Clobber File! by xinu on Feb 27, 2007 12:02 PM

Have you ever entered a command that involved redirection and just stared at it, palms sweating, heart racing, hoping against hope that, after all the variables interpolate, it doesn't destroy any of your crucial system files sitting nearby?

Enter: noclobber

If you're using the Bash shell, you can set this option to prevent overwriting of existing files. To see your current settings:

bash-2.05$ set -o
allexport       off
braceexpand     on
errexit         off
hashall         on
histexpand      on
keyword         off
monitor         on
noclobber       off
...

To set noclobber:

bash-2.05$ set -o noclobber
bashnoclobbershell
Quick history search by felipec on Jul 10, 2007 04:24 PM

Add the following to your ~/.inputrc:

"\e[5~": history-search-backward
"\e[6~": history-search-forward

And of course, in order to use your ~/.inputrc you need to set your INPUTRC environment variable in ~/.bash_profile:

export INPUTRC=$HOME/.inputrc

The next time you login you will be able to run "gvim <pageup>" and the last entry that started with "gvim " will appear, <pageup> again will bring the next one up, and so on.

Note that escape codes for PageUp and PageDown vary depending on your terminal type; check out this tip for a technique on how to find out what your terminal expects.

bashcommandshistoryinputrcshell
Remembered Paths by xinu on Jan 21, 2005 07:32 PM

Have you noticed that after you've moved a program your shell still insists that it's in the old location? Most of the time we just blink at the shell, log out, log back in and all is fine again. The culprit is the shell's hash implementation:

$ hash
hits    command
   1    /usr/bin/which
   1    /bin/stty
   8    /bin/ls
bashhash
Simple swap monitor by mandric on Sep 25, 2007 06:32 PM

This is a simple swap monitor script:

#!/bin/bash
# Notify me one time if my swap is over MAXSWAP and log the
# swap usage as well in SWAPLOG.
# Usage: ./monitor.sh &

SWAPLOG=~/logs/monitor/swap
MAXSWAP=5
INTERVAL=30

send_sms_msg () {
  if [ "$1" ]; then STRING=$1; fi
  if [ $SMS_SENT ]; then
    return 0
  else
    echo $STRING | mailx -s 'monitor msg' 5101234567@cingularme.com
    SMS_SENT=1
  fi
}

while true; do
    # parse free output, get current swap value
    swaptest=`free -m |grep Swap|perl -pe 's/Swap:\s+\S+\s+(\S+).*/$1/'`
    if [ $swaptest -ge $MAXSWAP ]; then
        echo `date` Swap is: $swaptest >> $SWAPLOG
        send_sms_msg "Swap is: $swaptest"
    fi;
    sleep $INTERVAL
done;
bashmemorymonitorscriptshellswaptools
Variable Indirection by xinu on Jan 31, 2006 10:06 AM

Sometimes you need to iterate over variable names and access their values. In shell script, you would do something like this to get the values of FOO and BAR:

$ FOO=apple
$ BAR=orange
$ VARS="FOO BAR"
$ for v in $VARS ; do echo ${!v} ; done

The above works in bash. Use this in Zsh:

$ for v in $VARS ; do echo ${(P)v} ; done

(The 'P' flag on ${v} causes a further variable lookup before ${v} is evaluated.)

Thanks to Cliff for the tip!

bashcommandsenvironmentevaluationiterationloopshellzsh
RSS