2001-07-10 14:25:50 +00:00
|
|
|
#!/bin/bash
|
|
|
|
# printf demo
|
|
|
|
|
2008-11-23 22:43:47 +00:00
|
|
|
declare -r PI=3.14159265358979 # Read-only variable, i.e., a constant.
|
|
|
|
declare -r DecimalConstant=31373
|
|
|
|
|
2001-07-10 14:25:50 +00:00
|
|
|
Message1="Greetings,"
|
|
|
|
Message2="Earthling."
|
|
|
|
|
|
|
|
echo
|
|
|
|
|
|
|
|
printf "Pi to 2 decimal places = %1.2f" $PI
|
|
|
|
echo
|
2001-09-04 13:27:31 +00:00
|
|
|
printf "Pi to 9 decimal places = %1.9f" $PI # It even rounds off correctly.
|
2001-07-10 14:25:50 +00:00
|
|
|
|
2001-09-04 13:27:31 +00:00
|
|
|
printf "\n" # Prints a line feed,
|
2005-03-21 13:51:11 +00:00
|
|
|
# Equivalent to 'echo' . . .
|
2001-07-10 14:25:50 +00:00
|
|
|
|
2005-03-21 13:51:11 +00:00
|
|
|
printf "Constant = \t%d\n" $DecimalConstant # Inserts tab (\t).
|
2001-07-10 14:25:50 +00:00
|
|
|
|
|
|
|
printf "%s %s \n" $Message1 $Message2
|
|
|
|
|
|
|
|
echo
|
|
|
|
|
2001-09-04 13:27:31 +00:00
|
|
|
# ==========================================#
|
2005-02-09 20:53:43 +00:00
|
|
|
# Simulation of C function, sprintf().
|
2001-09-04 13:27:31 +00:00
|
|
|
# Loading a variable with a formatted string.
|
|
|
|
|
|
|
|
echo
|
|
|
|
|
|
|
|
Pi12=$(printf "%1.12f" $PI)
|
2007-11-08 15:52:23 +00:00
|
|
|
echo "Pi to 12 decimal places = $Pi12" # Roundoff error!
|
2001-09-04 13:27:31 +00:00
|
|
|
|
|
|
|
Msg=`printf "%s %s \n" $Message1 $Message2`
|
|
|
|
echo $Msg; echo $Msg
|
|
|
|
|
2005-02-09 20:53:43 +00:00
|
|
|
# As it happens, the 'sprintf' function can now be accessed
|
2005-03-21 13:51:11 +00:00
|
|
|
#+ as a loadable module to Bash,
|
|
|
|
#+ but this is not portable.
|
2001-09-04 13:27:31 +00:00
|
|
|
|
2001-07-10 14:25:50 +00:00
|
|
|
exit 0
|