#! /usr/bin/env python
import os
from stat import *
#!/bin/bash
# printf demo
declare -r PI=3.14159265358979     # Read-only variable, i.e., a constant.
# printf demo
PI=3.14159265358979
declare -r DecimalConstant=31373
# Read-only variable, i.e., a constant.
DecimalConstant=31373
Message1="Greetings,"
Message1="Greetings,"
Message2="Earthling."
Message2="Earthling."
echo
print()
printf "Pi to 2 decimal places = %1.2f" $PI
print( "Pi to 2 decimal places = %1.2f" % (PI) )
echo
print()
printf "Pi to 9 decimal places = %1.9f" $PI  # It even rounds off correctly.
print( "Pi to 9 decimal places = %1.9f" % (PI) )
printf "\n"                                  # Prints a line feed,
# It even rounds off correctly.
print( "\n" )
                                             # Equivalent to 'echo' . . .
printf "Constant = \t%d\n" $DecimalConstant  # Inserts tab (\t).
# Prints a line feed,
# Equivalent to 'echo' . . .
print( "Constant = \t%d\n" % (DecimalConstant) )
printf "%s %s \n" $Message1 $Message2
# Inserts tab (\t).
print( "%s %s \n" % (Message1, Message2) )
echo
print()
# ==========================================#
# Simulation of C function, sprintf().
# Loading a variable with a formatted string.
echo 
# ==========================================#
# Simulation of C function, sprintf().
# Loading a variable with a formatted string.
print()
Pi12=$(printf "%1.12f" $PI)
Pi12=os.popen("printf \"%1.12f\" "+str(PI)).read()
echo "Pi to 12 decimal places = $Pi12"      # Roundoff error!
print("Pi to 12 decimal places = " + str(Pi12))
Msg=`printf "%s %s \n" $Message1 $Message2`
# Roundoff error!
Msg=os.popen("printf \"%s %s n\" "+str(Message1)+" "+str(Message2)).read()
echo $Msg; echo $Msg
print(Msg)
print(Msg)
#  As it happens, the 'sprintf' function can now be accessed
#+ as a loadable module to Bash,
#+ but this is not portable.
exit 0
#  As it happens, the 'sprintf' function can now be accessed
#+ as a loadable module to Bash,
#+ but this is not portable.
exit(0)
ÿ