27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

The variable indices will be treated as an array.<br />

−f functions<br />

declare −f<br />

A declare −f line with no arguments in a script causes a listing of all the functions previously<br />

defined in that script.<br />

declare −f function_name<br />

A declare −f function_name in a script lists just the function named.<br />

−x export<br />

declare −x var3<br />

This declares a variable as available for exporting outside the environment of the script itself.<br />

var=$value<br />

declare −x var3=373<br />

The declare command permits assigning a value to a variable in the same statement as setting its<br />

properties.<br />

Example 9−20. Using declare to type variables<br />

#!/bin/bash<br />

func1 ()<br />

{<br />

echo This is a function.<br />

}<br />

declare −f<br />

# Lists the function above.<br />

echo<br />

declare −i var1 # var1 is an integer.<br />

var1=2367<br />

echo "var1 declared as $var1"<br />

var1=var1+1 # Integer declaration eliminates the need for 'let'.<br />

echo "var1 incremented by 1 is $var1."<br />

# Attempt to change variable declared as integer<br />

echo "Attempting to change var1 to floating point value, 2367.1."<br />

var1=2367.1 # Results in error message, with no change to variable.<br />

echo "var1 is still $var1"<br />

echo<br />

declare −r var2=13.36<br />

# 'declare' permits setting a variable property<br />

#+ and simultaneously assigning it a value.<br />

echo "var2 declared as $var2" # Attempt to change readonly variable.<br />

var2=13.37<br />

# Generates error message, and exit from script.<br />

echo "var2 is still $var2"<br />

# This line will not execute.<br />

Chapter 9. Variables Revisited 93

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!