+ Reply to Thread
Results 1 to 2 of 2

Thread: Functions, operators, and options

  1. #1
    DarkLordoftheMonkeys's Avatar
    DarkLordoftheMonkeys is offline Programming Professional
    Join Date
    Oct 2009
    Location
    Massachussets
    Posts
    255
    Blog Entries
    56
    Rep Power
    11

    Functions, operators, and options

    This is my third tutorial on bash shell scripting. Here I will be talking about some topics I did not go over previously, namely functions, arithmetic operators, typed variables, and how to parse command line options.


    Functions:

    A function, as most programmers with experience in other languages know, is a subroutine, a named procedure that can be called repeatedly throughout the script. I personally do not use functions much as my scripts are generally never more than 30 lines long, but programmers writing long scripts that carry out repetitive tasks might find them useful.

    There are two syntaxes for declaring functions. The first is one that programmers in Javascript and PHP will probably be comfortable with:

    Code:
    function myfunc
    {
    	commands;
    }
    Note that in this case the inputs to the function are not specified. Instead, you use the variables $1, $2, etc. to access the inputs. You may also notice that the function declaration syntax differs from that of control structures in that it uses the curly bracket notation to define a block.

    Here is the other notation for functions:

    Code:
    myfunc(inputs)
    {
    	commands;
    }
    This syntax may be more familiar to some programmers as it uses named variables instead of $1, $2, etc. as its inputs.

    A function is called in much the same way that you would call a regular command:

    funcname input1 input2;


    Typed variables:

    Variables can be given types using the declare or typeset builtins. There are four basic data types in Shell: integer, string, array, and function. There is no type for floating point numbers. An operation on two integer type variables that would normally result in a floating point number will be rounded off to an integer.

    declare -a variable=value; - declares an array type
    declare -f func; - declares the function named func as a variable. This is basically a string that is that function declaration.
    declare -i variable=value; - declares an integer type
    declare -r variable=value; - declares a read-only variable, or in other words, a constant


    Operators in Shell:

    Arithmetic operators:

    + Addition
    - Subtraction
    * Multiplication
    / Division
    % Modulus
    ++ Increment by 1
    -- Decrement by 1
    ** Exponent

    Logical operators:

    && Logical AND
    || Logical OR
    ! Logical NOT

    Assignment oparators:
    (used without spaces)
    = Equals
    += Increase by
    -= Decrease by
    *= Multiply by
    /= Divide by
    %= Modulus by
    **= Raise to a power

    Test operators:
    (used with spaces)
    -eq Equal to
    -ne Not equal to
    -gt Greater than
    -lt Less than
    -ge Greater than or equal to
    -le Less than or equal to

    Bitwise operator:
    << Bitshift left
    >> Bitshift right
    <<= Bitshift left by
    >>= Bitshift right by
    & Bitwise AND
    | Bitwise OR
    ~ Bitwise NOT
    ^ Bitwise XOR


    File test operators:

    There are also several operators used to test conditions in files. These are used with control statements like if/then.

    -a File exists
    -d File is a directory
    -e File exists
    -f File is a regular file
    -G You are in the group that owns file
    -h File is a symbolic link
    -N File was modified since last read
    -O You own file
    -r You have read permission
    -s File exists and is not empty
    -w You have write permission
    -x You have execute permission
    -nt Newer than
    -ot Older than

    Example:

    This script deletes all empty files in a directory:

    Code:
    #!/bin/bash
    for file in $(ls $1)
    do
    	if [ ! -s "$1/$file" ];
    	then
    		echo "$1/$file is empty.  Removing file.";
    		if [ -d "$1/$file" ]
    		then
    			rmdir "$1/$file";
    		elif [ -f "$1/$file" ]
    		then
    			rm "$1/$file";
    		fi
    	fi
    done

    Options:

    Options (inputs beginning with a dash) can be parsed separately from the regular inputs by using the shift builtin. This makes it so that $1=$2, $2=$3, etc. Eventually, the script has its inputs in the right place and knows what options have been set. Here is a trivial script for doing this:

    Code:
    #!/bin/bash
    # This script demonstrates how options and inputs
    # to the script can be parsed separately.
    opta="off";
    optd="off";
    optx="on";
    for param in $*
    do
    	case $param in
    		-*[ad]* )
    			case $param in
    				-*a* ) opta="on";;
    			esac
    			case $param in
    				-*d* ) optd="on";;
    			esac
    			shift;;
    		-* )
    			echo "Error: $param is not an option.";
    			printf "\a";
    			shift;;
    	esac
    done
    # Now the options have been stored for later use,
    # while allowing the inputs to be processed.
    filename=$1;
    echo "You entered \"$filename\" as the filename.  Do you wish to proceed?";
    if [ $opta = $optx ]
    then
    	echo "Option a is turned on.";
    else
    	echo "Option a is turned off.";
    fi
    if [ $optd = $optx ]
    then
    	echo "Option d is turned on.";
    else
    	echo "Option d is turned off.";
    fi
    # Now for some "garbage collection".
    unset opta;
    unset optd;
    unset optx;
    Life's too short to be cool. Be a nerd.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Functions, operators, and options

    Very nice. +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 4
    Last Post: 02-06-2011, 01:37 PM
  2. Flash Shoutbox Options
    By Logan in forum General Programming
    Replies: 6
    Last Post: 08-13-2009, 08:59 AM
  3. 2 options
    By MojoMonkey in forum Java Help
    Replies: 3
    Last Post: 01-03-2008, 09:17 PM
  4. Select options
    By catbollu in forum ASP, ASP.NET and Coldfusion
    Replies: 3
    Last Post: 05-23-2007, 01:06 PM
  5. Operators
    By clookid in forum PHP Tutorials
    Replies: 1
    Last Post: 01-11-2007, 06:35 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts