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