====== اسکریپت نویسی در Bash ======
----
===== Double-Parentheses =====
* [[https://tldp.org/LDP/abs/html/dblparens.html]]
انجام ساده تر محاسبات ریاضی
#!/bin/bash
# c-vars.sh
# Manipulating a variable, C-style, using the (( ... )) construct.
echo +==========================================+
(( a = 23 )) # Setting a value, C-style,
#+ with spaces on both sides of the "=".
echo "a (initial value) = $a" # 23
(( a++ )) # Post-increment 'a', C-style.
echo "a (after a++) = $a" # 24
(( a-- )) # Post-decrement 'a', C-style.
echo "a (after a--) = $a" # 23
(( ++a )) # Pre-increment 'a', C-style.
echo "a (after ++a) = $a" # 24
(( --a )) # Pre-decrement 'a', C-style.
echo "a (after --a) = $a" # 23
echo +==========================================+
# Like C, pre- and post-decrement operators
#+ have different side-effects.
n=1
(( --n )) && echo "True" || echo "False" # False
n=1
(( n-- )) && echo "True" || echo "False" # True
echo +==========================================+
# C-style ternary operator.
(( t = a<45?7:11 ))
# ^ ^ ^
echo "If a < 45, then t = 7, else t = 11." # a = 23
echo "t = $t " # t = 7
echo +==========================================+
# Using C-like syntax.
LIMIT=10
for ((a=1; a <= LIMIT ; a++)) # Double parentheses, and naked "LIMIT"
do
echo -n "$a "
done # A construct borrowed from ksh93.
echo
echo +==========================================+
# Let's use the C "comma operator" to increment two variables simultaneously.
LIMIT=10
for ((a=1, b=1; a <= LIMIT ; a++, b++))
do # The comma concatenates operations.
echo -n "$a-$b "
done
exit
----
===== Numerical Constants =====
* [[https://tldp.org/LDP/abs/html/numerical-constants.html]]
ثوابت عددی و تبدیل مبنا
A shell script interprets a number as decimal (base 10), unless that number has a special prefix or notation.
A number preceded by a 0 is octal (base 8). A number preceded by 0x is hexadecimal (base 16).
A number with an embedded # evaluates as BASE#NUMBER (with range and notational restrictions).
#!/bin/bash
# numbers.sh: Representation of numbers in different bases.
# Decimal: the default
let "a = 32"
echo "decimal number = $a" # 32
# Nothing out of the ordinary here.
# Octal: numbers preceded by '0' (zero)
let "a = 032"
echo "octal number = $a" # 26
# Expresses result in decimal.
# --------- ------ -- -------
# Hexadecimal: numbers preceded by '0x' or '0X'
let "a = 0x32"
echo "hexadecimal number = $a" # 50
echo $((0x9abc)) # 39612
# ^^ ^^ double-parentheses arithmetic expansion/evaluation
# Expresses result in decimal.
# Other bases: BASE#NUMBER
# BASE between 2 and 64.
# NUMBER must use symbols within the BASE range, see below.
let "a = 2#111100111001101"
echo "binary number = $a" # 31181
let "a = 32#77"
echo "base-32 number = $a" # 231
let "a = 64#@_"
echo "base-64 number = $a" # 4031
# This notation only works for a limited range (2 - 64) of ASCII characters.
# 10 digits + 26 lowercase characters + 26 uppercase characters + @ + _
echo
echo $((36#zz)) $((2#10101010)) $((16#AF16)) $((53#1aA)) $((64#__7@))
# 1295 170 44822 3375 16773630