More Bash techniques
Posted 普通学习者
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了More Bash techniques相关的知识,希望对你有一定的参考价值。
(1) formated print
## print 1 into 001
printf '%03d' 1
## print 0.5 into 0.50
printf '%.2f' 0.5
(2) 'for' loop
## loop through 1 to 5
for i in {1..5}
do
echo ${i}
done
## another way
for ((i=1; i<=5; i++))
do
echo ${i}
done
(3) declare/call a function
## declare a 'hello world + name' function
sayHello () {
echo 'hello world, $1'
}
## call the custom function with a name parameter
sayHello Alex
(4) floating number operation
## floating number comparison
echo "10.5>2.1" | bc
## floating number addition
echo "10.5+2.1" | bc
source:
(1) https://www.cyberciti.biz/faq/bash-for-loop/
(2) https://stackoverflow.com/questions/6212219/passing-parameters-to-a-bash-function
(3) https://stackoverflow.com/questions/11541568/how-to-do-float-comparison-in-bash
以上是关于More Bash techniques的主要内容,如果未能解决你的问题,请参考以下文章