bash脚本入门

基础语法

  1. 逻辑判断。if-else、case
  2. 操作循环。for、while、utile
  3. 数组的简单使用。declare -a
  4. 输入处理。read
  5. 日期获取

逻辑判断选项

  1. 按照文件类型判断
  2. 按照文件权限
  3. 按照文件信息
  4. 整数比较
  5. 字符串比较
  6. 判断条件之间比较
    ===>> Linux Bash Shell编程(八):条件判断语句与示例

参考


创建删除文件

#!/bin/bash

# 声明数组
declare -a names=("a" "engure" "jjjj")

# 当前路径
cPath=$(cd "$(dirname "$1")";pwd)"/"

# 遍历数组
for name in "${names[@]}"
do
	file=${cPath}${name}".txt"
	if [ -e $file ]; then		# 文件是否存在
		# echo -n 不换行输出
		echo -n  $file' exists.  now delete it.'
		echo ${file}
		rm ${file}
	else
		echo $file' not exists.   now create it and make it executable.'
		touch $file
		chmod a+x $file
	fi
done

获取时间

====》linux bash脚本_如何在Linux终端中显示日期和时间

#!/bin/bash

date_now=`date +%m/%d/%Y`
time_now=`date +%H:%M:%S`

echo "today is" $date_now
echo "time is" $time_now
echo "totally"  $date_now $time_now
沉舟侧畔千帆过,病树前头万木春。
原文地址:https://www.cnblogs.com/engure/p/15597070.html