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的主要内容,如果未能解决你的问题,请参考以下文章

Linux操作系统 §3:基本命令Ⅱ(Bash常用功能,文件查询)

cp mv more vim的使用

bash 环境配置及脚本

git bash中不能显示中文

bash shell学习 01(配置和启动脚本)

linux文件管理和 对bash的理解