|
|
#1 |
|
Pewlius Caesar
bit-tech Staff
Join Date: Nov 2001
Location: Ascot, Berks
Posts: 18,021
![]() ![]() ![]() ![]() ![]() |
BASHing Through Scripts
http://www.bit-tech.net/bits/2007/11...ough_scripts/1
Our resident Linux guru Ken Gypen is back to teach you a thing or three about one of the most nebulous but powerful features of 'Nix - using scripts to automate common tasks. So jump in and get ready to learn the basics, then write your own image resize and watermark script. Digg for Win: http://digg.com/linux_unix/BASHing_Through_Scripts
__________________
Last edited by CardJoe; 26th Nov 2007 at 08:50. |
|
|
|
|
|
#2 |
|
I pwn all your storage
Join Date: Jul 2005
Location: Southampton
Posts: 13,933
![]() ![]() ![]() ![]() |
small typo:
by issuing ((i++)) on page 2, should be ((n++)) like the example. Also telling people how to run the scripts on page 2 would be better then waiting untill page 4. But a good basic guide to BASH/ |
|
|
|
|
|
#3 |
|
So Say We All
Join Date: Sep 2006
Location: New Zealand
Posts: 1,657
![]() |
nicely done, didnt relise vim had colour support, is it by default and multiple languages? if so another incentive to learn how to use it (no more jext and its minute long starts). one slight error thou BASHWatermark.png cannot be found on the server
__________________
Hell hath no fury like a hippo with a machine gun.
|
|
|
|
|
|
#4 | |
|
/dev/null
Join Date: Aug 2005
Location: Belgium
Posts: 4,102
![]() ![]() |
Quote:
If for some reason your VIm doesn't highlight, try typing: Code:
:syntax enable
__________________
There Are 10 Types Of People, Those Who Know Binary and Those Who Don't |
|
|
|
|
|
|
#5 | |
|
Pewlius Caesar
bit-tech Staff
Join Date: Nov 2001
Location: Ascot, Berks
Posts: 18,021
![]() ![]() ![]() ![]() ![]() |
Quote:
__________________
|
|
|
|
|
|
|
#6 |
|
/dev/null
Join Date: Aug 2005
Location: Belgium
Posts: 4,102
![]() ![]() |
Are the userrights set correctly?
__________________
There Are 10 Types Of People, Those Who Know Binary and Those Who Don't |
|
|
|
|
|
#7 | ||
|
Pewlius Caesar
bit-tech Staff
Join Date: Nov 2001
Location: Ascot, Berks
Posts: 18,021
![]() ![]() ![]() ![]() ![]() |
Quote:
__________________
|
||
|
|
|
|
|
#8 |
|
What's a Dremel?
Join Date: Jan 2007
Posts: 18
![]() |
A few remarks:
* You don't need to install Linux to get the benefits of Unix scripting - I for one run Cygwin on all my Windows desktops, and have written my own equivalent bash/ImageMagick scripts, amongst many others. * Be careful when working with variables in bash scripts, in particular watch out for quoting. Writing ${i} is no different from writing $i, and provides no extra quoting. Use "$i" etc. in case the file names you're working with have spaces or other characters that need escaping. The braces syntax is useful for bash's special variable features, such as array variables and indexing (e.g. ${i[1]}), replacement (e.g. ${i/foo/bar}), etc. * Backticks ` are fine for simple scenarios, but if you are in a situation where you may want to nest command execution, and to provide better visibility into nesting, you might want to use the $() syntax instead. Also, be careful with quoting here too. To get the result as a single word, you need to put quotes outside the backtick syntax. * For numeric statements (rather than expressions) such as increment, I personally use the 'let' command, rather than (()) on a single line; for looping in the C style, I use "for ((i=0; i<10; ++i)); do done"; and for numeric tests in if / while / etc., I use (()) rather than the [ command. |
|
|
|
|
|
#9 |
|
Brett Thomas
Join Date: Aug 2004
Location: Cleveland, OH USA
Posts: 3,906
![]() |
awesome points, barrkel - thanks for those additions!
__________________
"Frankly that seems overkill. iluvtrees2 arguing with spec is the intellectual equivalent of a bunny rabbit taking on a pissed-off lion." - Nexxo |
|
|
|
|
|
#10 |
|
What's a Spode?
Join Date: Jan 2004
Location: UK
Posts: 21
![]() |
Although writing BASH scripts is much more portable than anything else, if this is only for your home server - remember that PHP can be installed as an executable binary, as well as an apache module. So if you can understand a C/PHP syntax better - it might make your life a little easier.
Equally, if you put the same scripts into a web folder with remote access - it means you could start scripts manually over the web - very useful when a firewall/proxy doesn't allow you to use SSH. Be careful about security in those cases though (especially if you allow access outside of the site root), and remember that you will probably need to increase the execution time of PHP for anything that will take over 30 seconds.
__________________
http://www.thinkbikes.com |
|
|
|
|
|
#11 |
|
Multimodder
Join Date: Nov 2005
Location: Bristol/Portsmouth(Uni)
Posts: 197
![]() |
Love it when im mentioned in a front page article
__________________
Project: Little Rascal - Mini-ITX Gaming Rig, available @ - Wizd Forums - Bit-Tech - The Mod Nation - SFFTech |
|
|
|
|
|
#12 |
|
What's a Dremel?
Join Date: Sep 2006
Location: Slovakia
Posts: 14
![]() |
I work as an editor too - for largest IT server in Slovakia
BASH scripting is very powerful, but those simple scripts mentioned in article work well only if your photos are perfect. I, for example, have to do cropping manually and then i can let scripts do resizing and watermarking. If my photos were perfect - no need for cropping or levels changing - i would use scripts only. In fact, in Photoshop there is automatic function for cropping, but mostly, it works with white background only - so it is not very useful 4 me. Anyway, great article ))
__________________
Your Life - Your Rules ! my case in progress: http://msxyz.szm.sk/image/plexi.jpg |
|
|
|
|
|
#13 | |
|
Neither Patrick nor Sparta
Join Date: Jun 2007
Location: Tallahassee, FL
Posts: 1,789
![]() ![]() ![]() ![]() ![]() ![]() |
Thanks for the great article... now if i could stop poking around in my slackware box and killing i tin the process
__________________
Quote:
|
|
|
|
|
|
|
#14 |
|
Hypermodder
Join Date: May 2007
Posts: 887
![]() |
Why didn't you mention elseif at all?
Other then that, good simple guide ![]() p.s. can you get syntax highlighting in putty, never seems to work in ubuntu/debian for me ... |
|
|
|
| completemadness |
| View Public Profile |
| Find More Posts by completemadness |
|
|
#15 |
|
/dev/null
Join Date: Aug 2005
Location: Belgium
Posts: 4,102
![]() ![]() |
Hmm, I could have added that... However, I find it a lot easier to just throw in a case structure instead of 100 elseifs... Personal taste I guess.
If you get highlighting in a physical terminal, it should work in a SSH terminal too... So yes, you should get it in putty.
__________________
There Are 10 Types Of People, Those Who Know Binary and Those Who Don't |
|
|
|
|
|
#16 | |
|
Hypermodder
Join Date: May 2007
Posts: 887
![]() |
Quote:
sometimes you want a few completely different comparisons ... but those situations are rare i guess If you get highlighting in a physical terminal, it should work in a SSH terminal too... So yes, you should get it in putty.[/quote] Hmmm ... maybe just because the servers don't have a gui, they are just using some basic shell, meh, ive lived with it for 5 years, it doesn't really bother me - its only useful if your coding something complicated |
|
|
|
|
| completemadness |
| View Public Profile |
| Find More Posts by completemadness |
|
|
#17 |
|
DUHHHH
Join Date: Jan 2002
Location: Montreal, Canada
Posts: 2,585
![]() ![]() ![]() ![]() |
BASH scripting gave me nightmares last semester...
I need to read this article though
__________________
Phenom X3 720 Black Edition @ 3.4Ghz - Asus M3N72-D SLI - 4GB PC6400 - MSI GTX260 216 - RAID0 37gb Raptors - 2x 500GB - Corsair TX850 - Samsung 2233RZ 120hz cheesecake - Windows 7 Ultimate 64Bits Kids ! The Samsung 2233RZ is the solution to all your problems in life ! That is, of course, if your biggest concern is not sucking at FPS games |
|
|
|
|
|
#18 |
|
Minimodder
Join Date: Jun 2006
Location: New Zealand
Posts: 20
![]() |
The real fun starts when you discover the script you've just spent several hours perfecting has to run on a shell that requires strict posix compliance (in my case it was ksh). I found this was the most helpful source of common "bashisms" and solutions when I googled quickly: https://wiki.ubuntu.com/DashAsBinSh
By the time I discovered the requirement of course I had a large number of bash-specifics, mostly to do with `` command substitution and file redirection) Lessons learned: * Backticks are awesome, but they'll stab you in the back (no pun intended) * {command <<< "input"} is an awesome shortcut for sending short text strings to the input of a running program (I needed to send "yes" to a confirmation prompt), but also bash only. {command << EOF \n input \n EOF} at least works, but doesn't do much for readability. * Very few shells care for attempts to dispose of standard output and error simultaneously {&> /dev/null} or to combine stderr with stdout {2>&1} * Coding for 80x25 char terminals sucks * ${#var} returns the length of ${var}. Amazingly that one is posix standard Some other functions I cooked up that day which people might find useful: http://qupada.orcon.net.nz/bash.sh Reading variables - 3 variants, min length, no length constraint, default value (ie blank entry = default) Just do (for example) {get_var_minlength "foo" "Enter at least 6 characters" 6 "I said at least 6"}. When the user enters enough text at the prompt, ${foo} will be set to their response. Command-line processing - as the article pointed out, $1 is the first argument to the script (if you're looking for it, $0 is the filename of the script that was run), with the combination of the while loop and the 'shift' command we can process as many arguments as the user gives. With extra 'shift' statements commands of the form {--comand "value"} can be processed too {--command|-c) shift; VAR="$1" ;;}. I hope someone gets something useful out of that lot. |
|
|
|
|
|
#19 | ||
|
Supermodder
Join Date: Oct 2005
Location: India
Posts: 406
![]() |
Pfft, my GUI + CLI OS is Emacs.
![]() The article was decent, but I have one request. Instead of this: Quote:
Quote:
__________________
It sure took me a long time to realize I hadn't changed my forum settings since returning from Canadia-land. |
||
|
|
|
![]() |
| Thread Tools | |
|
|