How to Test for the Number of Arguments
| ||||
| Language | Examples | Comments | ||
| Bourne Shell | if [ $# = 0 ] then echo "Usage: $0 [arg-list]" exit 1 fi | |||
| C Shell | if ($#argv == 0) then echo "Usage: $0 [arg-list]" exit 1 endif x | |||
| Perl | if (@ARGV < 1) { printf "\nUSAGE: $0 [arg-list]\n"; exit 1; } | |||
| C | int main(int argc, char **argv) { if (argc <= 1) { printf("USAGE: argv[0] [arg-list]\n"); exit(1); } } | |||
| Visual Basic | ||||
| Applescript | ||||
How to Assign Arguments to a Variable
| ||||
| Language | Examples | Comments | ||
| Bourne Shell | foo=$1 | |||
| C Shell | set foo=$1 setenv DUMMY $1 | Normal variables. Environment variables. | ||
| Perl | $dummy = $_[0]; $dummy = $ARGV[0]; $dummy = $QUERY->param('foo'); | First argument to a command-line Perl script. First argument to a subroutine. Argument named "foo" to a Perl/CGI script. | ||
| C | strcpy(filename, argv[1]); | |||
| Visual Basic | ||||
| Applescript | on run argv set foo to item 1 of argv end run | |||
How to Test if a Variable is Set
| ||||
| Language | Examples | Comments | ||
| Bourne Shell | if [ "$foo" ] then echo "'$foo' is set." fi | |||
| C Shell | if ($?foo != 0) then echo \$foo is set. endif | |||
| Perl | if ($foo) { print "\$foo is $foo\n"; } | |||
| C | ||||
| Visual Basic | ||||
| Applescript | ||||
How to Test if a Variable is Not Set
| ||||
| Language | Examples | Comments | ||
| Bourne Shell | if [ -z "$foo" ] then echo \$foo is null or not set. fi | You cannot tell whether it is null or simply not set. See this example to detect if it's null. | ||
| C Shell | if ($?foo == 0) then echo \$foo is not set. endif | |||
| Perl | In "strict" mode, you will get an error if you try to test a variable that is not set (undefined). | |||
| C | ||||
| Visual Basic | ||||
| Applescript | ||||
How to Test if a Variable is Null
| ||||
| Language | Examples | Comments | ||
| Bourne Shell | if [ ! "$foo" ] then echo "'$foo' is null." fi | |||
| C Shell | if ($foo == "") then echo "'$foo' is null." endif | You will get an error if $foo is not set! See this example to check if a variable is set. | ||
| Perl | if (!($foo)) { print "\$foo is null\n"; } | You will get an error in "strict" mode if $foo is not set (undefined)! | ||
| C | ||||
| Visual Basic | ||||
| Applescript | ||||
How to Set a Default Value for an Argument/Variable
| ||||
| Language | Examples | Comments | ||
| Bourne Shell | foo=${1:-"Default"} | |||
| C Shell | set foo=$1 if ($?foo == 1) then if ($foo == "") then set foo="Default" endif endif | |||
| Perl | my $foo = $ARGV[0] || "Default"; | |||
| C | ||||
| Visual Basic | ||||
| Applescript | ||||
How to Test for Successful Completion of a Command
| ||||
| Language | Examples | Comments | ||
| Bourne Shell | if backup_script then echo "Backup succeeded." fi | |||
| C Shell | if { backup_script } then echo "Backup succeeded." endif | |||
| Perl | if (print "hello\n") { print "it worked\n"; } system "ls > /dev/null"; my $systemResponse = $? >> 8; if (!($systemResponse)) { print "it worked\n"; } else { print "it didn't work\n"; } | |||
| C | ||||
| Visual Basic | ||||
| Applescript | ||||
How to Define and Call Functions/Subroutines
| ||||
| Language | Examples | Comments | ||
| Bourne Shell | Backup() { return `cp $1 /backups > /dev/null 2>&1` } Success() { echo "$1 was backed up. } if Backup ".login" then Success ".login" else echo "Backup failed!" fi | Functions and Subroutines gets their args through a local $1, $2, etc. A function uses a return statement to indicate success or failure. | ||
| C Shell | ||||
| Perl | sub Albums { my $artist = shift; my @albums = @{$_[0]}; print "$artist: "; print join(", ", @albums); return scalar(@albums); } my @cnt = Albums("INXS", ["Kick", "X"]); print "\n@cnt albums\n"; | Line-by-line, this shows the sub declaration, shifting in the first arg from @_, dereferencing the next arg from @_, a simple print, using join to quickly print an array, return the number of items in an array, and finally, something complex: using an anonymous array reference! | ||
| C | ||||
| Visual Basic | ||||
| Applescript | -- test this! on sub1() set date to (do shell script "date") return date end sub1 set myDate to sub1() script sub2 display dialog "hi" end script tell sub2 to run | |||
How to Get Input from the User
| ||||
| Language | Examples | Comments | ||
| Bourne Shell | read foo | |||
| C Shell | set foo=$< | |||
| Perl | my $input = <STDIN>; | It's common to use "<>" as a shortcut, but that will first look for arguments on the command-line before looking at the terminal (STDIN), so use "<>"with care. | ||
| C | ||||
| Visual Basic | ||||
| Applescript | ||||
How to Capture the Output of a System Call
| ||||
| Language | Examples | Comments | ||
| Bourne Shell | filelist=`ls` | |||
| C Shell | set filelist=`ls` | |||
| Perl | my $filelist = `ls`; | |||
| C | ||||
| Visual Basic | ||||
| Applescript | ||||
How to construct for loops
| ||||
| Language | Examples | Comments | ||
| Bourne Shell | filelist=`ls` for file in $filelist do cp $file $file.bak done | |||
| C Shell | set filelist=`ls` foreach file ($filelist) cp $file $file.bak end | |||
| Perl | for (my $i = 0; $id <= $#myArray; $id++) ... foreach my $item (@myArray) ... foreach my $key (keys %myHash) ... | |||
| C | ||||
| Visual Basic | ||||
| Applescript | ||||
How to construct while loops
| ||||
| Language | Examples | Comments | ||
| Bourne Shell | while [ $FLAG = "TRUE" ] do ... done | |||
| C Shell | while ($FLAG == "TRUE") ... end | |||
| Perl | while ($attempt < $MAX_ATTEMPTS) { ... } | |||
| C | ||||
| Visual Basic | ||||
| Applescript | ||||
How to construct case statements
| ||||
| Language | Examples | Comments | ||
| Bourne Shell | case $foo in "Hello") echo "Hello there!" ;; *) echo "Default case" ;; esac | |||
| C Shell | switch ($foo) case "Hello": ... # Use breaksw here, if desired default: ... endsw | |||
| Perl | use Switch; switch ($val) { case 1 { print "Too low\n" } case 2 { print "Just right\n" } else { print "Too high\n" } } | Case (or "switch") statements aren't built into Perl 5 - you'll need the Switch module from CPAN for this to work. Perl 6 provides a switch construct (using the terms "given", "when", and "default", in place of "switch", "case", and "else"). The Switch module provides a Perl 6 compatibility mode ("use Switch 'Perl6';"), but the "default" keyword seems broken when I try to use it. | ||
| C | ||||
| Visual Basic | ||||
| Applescript | ||||
How to use goto commands
| ||||
| Language | Examples | Comments | ||
| Bourne Shell | n/a | |||
| C Shell | if ($#argv <= 0)then goto noswitch endif ... noswitch: echo "Must supply argument(s)." exit | |||
| Perl | if ($#ARGV <= 0) { goto switches; } print "Must supply argument(s)."; exit 1; switches: foreach my $arg (@ARGV) ... | I've never actually used a goto in Perl, and hopefully I never will! | ||
| C | ||||
| Visual Basic | ||||
| Applescript | ||||
How to do Mathematical Calculations
| ||||
| Language | Examples | Comments | ||
| Bourne Shell | count=1 count=`expr $count + 1` | |||
| C Shell | set count=1 set count=`expr $count + 1` | |||
| Perl | $i++; $i = $i + 2; | |||
| C | ||||
| Visual Basic | ||||
| Applescript | ||||
How to Redirect Standard Out and Standard Error
| ||||
| Language | Examples | Comments | ||
| Bourne Shell | backupscript > /dev/null backup_script 2> /dev/null backup_script > logfile 2>&1 | Redirects Standard Out only Redirects Standard Error only Redirects Standard Out and Error | ||
| C Shell | backupscript > /dev/null | Redirects Standard Out and Standard Error | ||
| Perl | open (F, ">/var/logs/out"); print F "Process completed\n"; close(F); | This is really just a method for printing directly to a file instead of "Standard Out". When a Perl script is called from a shell, you can use the shell's methods for redirecting Standard Out and/or Standard Error. | ||
| C | ||||
| Visual Basic | ||||
| Applescript | ||||
Links
| ||||
| General Links (edit)
| ||||
| Language | Links | |||
| Bourne Shell | Add links | |||
| C Shell
(edit these links) |
||||
| Perl
(edit these links) |
||||
| C | Add links | |||
| Visual Basic
(edit these links) |
||||
| Applescript
(edit these links) |
||||