咱们要学会用工具解放双手,比方批量给文件夹下某些文件建设软链接,咱们能够写个脚本实现。上面记录这个工程中用到的一些命令。
变量
定义变量时,变量名不加美元符号
your_name="12"
应用变量
应用一个定义过的变量,只有在变量名后面加美元符号即可
your_name="qinjx"echo $your_nameecho ${your_name}
变量名里面的花括号是可选的,加不加都行,加花括号是为了帮忙解释器辨认变量的边界
获取某目录下文件夹/文件的名称
- 如果是须要深度遍历,即输入文件夹曾经文件夹里的文件/文件夹,命令如下
#!/bin/bashcd 目标目录for file in $(ls *)do echo $filedone
- 如果只想获取第一层的文件曾经文件夹,则如下
#!/bin/bashcd 目标目录for file in $(ls )do echo $filedone
文件夹和文件的判断
首先判断的语法是if [ condition ]
两个命令:
- -f "file":判断file是否是文件;
- -d "file":判断file是否是目录(文件夹)。
联合获取文件/夹的语法,比方判断是否为文件夹能够这么写
#!/bin/bashcd 目标目录for file in $(ls )do if [ -d "$file" ]; then echo "$file is a directory " elif [ -f "$file" ]; then echo "$file is a file" fidone
数组是否蕴含某个值
咱们晓得 javascript 蕴含能够间接应用 [].includes('xxx')
。 用 shell能够这么写:
if [[ " ${array[@]} " =~ " ${value} " ]]; then echo truefiif [[ ! " ${array[@]} " =~ " ${value} " ]]; then echo falsefi
流程管制
if 语句
if conditionthen command1 command2 ... commandN fi
if else
if condition then command1 command2else commandfi
if else-if else
if condition1then command1elif condition2 then command2else commandNfi
获取某个文件的绝对路径
- 获取文件的绝对路径能够应用
$(pwd)
- 如果想要获取一个绝对文件的绝对路径,能够这样写
$(cd ${basePath}; pwd)