LDP/LDP/guide/docbook/abs-guide/ex20.sh

32 lines
942 B
Bash
Raw Permalink Normal View History

2001-07-10 14:25:50 +00:00
#!/bin/bash
func1 ()
{
2008-11-23 22:43:47 +00:00
echo This is a function.
2001-07-10 14:25:50 +00:00
}
2001-09-04 13:27:31 +00:00
declare -f # Lists the function above.
2001-07-10 14:25:50 +00:00
echo
declare -i var1 # var1 is an integer.
var1=2367
echo "var1 declared as $var1"
2001-09-04 13:27:31 +00:00
var1=var1+1 # Integer declaration eliminates the need for 'let'.
2001-07-10 14:25:50 +00:00
echo "var1 incremented by 1 is $var1."
2005-06-05 14:39:50 +00:00
# Attempt to change variable declared as integer.
2001-07-10 14:25:50 +00:00
echo "Attempting to change var1 to floating point value, 2367.1."
2001-09-04 13:27:31 +00:00
var1=2367.1 # Results in error message, with no change to variable.
2001-07-10 14:25:50 +00:00
echo "var1 is still $var1"
echo
2001-09-04 13:27:31 +00:00
declare -r var2=13.36 # 'declare' permits setting a variable property
#+ and simultaneously assigning it a value.
echo "var2 declared as $var2" # Attempt to change readonly variable.
var2=13.37 # Generates error message, and exit from script.
2001-07-10 14:25:50 +00:00
2001-09-04 13:27:31 +00:00
echo "var2 is still $var2" # This line will not execute.
exit 0 # Script will not exit here.