#! /usr/bin/env python | |
import subprocess | |
from stat import * | |
#!/bin/bash | |
# int-or-string.sh | |
a=2334 # Integer. | |
# int-or-string.sh | |
a=2334 | |
let "a += 1" | |
# Integer. | |
"a += 1" | |
echo "a = $a " # a = 2335 | |
print("a = "+str(a)+" ") | |
echo # Integer, still. | |
# a = 2335 | |
print() | |
b=${a/23/BB} # Substitute "BB" for "23". | |
# Integer, still. | |
b=str(a/23/BB) | |
# This transforms $b into a string. | |
echo "b = $b" # b = BB35 | |
# Substitute "BB" for "23". | |
# This transforms $b into a string. | |
print("b = "+str(b)) | |
declare -i b # Declaring it an integer doesn't help. | |
# b = BB35 | |
_rc = subprocess.call(["declare","-i","b"]) | |
echo "b = $b" # b = BB35 | |
# Declaring it an integer doesn't help. | |
print("b = "+str(b)) | |
let "b += 1" # BB35 + 1 | |
# b = BB35 | |
"b += 1" | |
echo "b = $b" # b = 1 | |
# BB35 + 1 | |
print("b = "+str(b)) | |
echo # Bash sets the "integer value" of a string to 0. | |
# b = 1 | |
print() | |
c=BB34 | |
# Bash sets the "integer value" of a string to 0. | |
c="BB34" | |
echo "c = $c" # c = BB34 | |
print("c = "+str(c)) | |
d=${c/BB/23} # Substitute "23" for "BB". | |
# c = BB34 | |
d=str(c/BB/23) | |
# This makes $d an integer. | |
echo "d = $d" # d = 2334 | |
# Substitute "23" for "BB". | |
# This makes $d an integer. | |
print("d = "+str(d)) | |
let "d += 1" # 2334 + 1 | |
# d = 2334 | |
"d += 1" | |
echo "d = $d" # d = 2335 | |
# 2334 + 1 | |
print("d = "+str(d)) | |
echo | |
# d = 2335 | |
print() | |
# What about null variables? | |
e='' # ... Or e="" ... Or e= | |
# What about null variables? | |
e=r'' | |
echo "e = $e" # e = | |
# ... Or e="" ... Or e= | |
print("e = "+str(e)) | |
let "e += 1" # Arithmetic operations allowed on a null variable? | |
# e = | |
"e += 1" | |
echo "e = $e" # e = 1 | |
# Arithmetic operations allowed on a null variable? | |
print("e = "+str(e)) | |
echo # Null variable transformed into an integer. | |
# e = 1 | |
print() | |
# What about undeclared variables? | |
echo "f = $f" # f = | |
# Null variable transformed into an integer. | |
# What about undeclared variables? | |
print("f = "+str(f)) | |
let "f += 1" # Arithmetic operations allowed? | |
# f = | |
"f += 1" | |
echo "f = $f" # f = 1 | |
# Arithmetic operations allowed? | |
print("f = "+str(f)) | |
echo # Undeclared variable transformed into an integer. | |
# f = 1 | |
print() | |
# | |
# However ... | |
let "f /= $undecl_var" # Divide by zero? | |
# Undeclared variable transformed into an integer. | |
# | |
# However ... | |
"f /= "+str(undecl_var) | |
# let: f /= : syntax error: operand expected (error token is " ") | |
# Syntax error! Variable $undecl_var is not set to zero here! | |
# | |
# But still ... | |
let "f /= 0" | |
# Divide by zero? | |
# let: f /= : syntax error: operand expected (error token is " ") | |
# Syntax error! Variable $undecl_var is not set to zero here! | |
# | |
# But still ... | |
"f /= 0" | |
# let: f /= 0: division by 0 (error token is "0") | |
# Expected behavior. | |
# Bash (usually) sets the "integer value" of null to zero | |
#+ when performing an arithmetic operation. | |
# But, don't try this at home, folks! | |
# It's undocumented and probably non-portable behavior. | |
# Conclusion: Variables in Bash are untyped, | |
#+ with all attendant consequences. | |
exit $? | |
# let: f /= 0: division by 0 (error token is "0") | |
# Expected behavior. | |
# Bash (usually) sets the "integer value" of null to zero | |
#+ when performing an arithmetic operation. | |
# But, don't try this at home, folks! | |
# It's undocumented and probably non-portable behavior. | |
# Conclusion: Variables in Bash are untyped, | |
#+ with all attendant consequences. | |
exit(_rc) | |
ÿ |