一、开头注释代表shell类型shell
#!/bin/bash
二、输入内容到某文件bash
echo begin.>begin.log
三、判断某目录下肯定文件是否存在函数
if [ -f "./begin.log" ];then echo 文件存在 else echo 文件不存在 fi
四、判断某目录下某种后缀文件是否存在code
if ls ./*.txt > /dev/null ;then echo 文件存在 else echo 文件不存在 fi
五、将某目录下的文件移动到另外一个目录中继承
mv ./default/*.txt ./
六、将某目录下多个文件合并为一个文件并输出到某目录下it
cat ./*.txt > ./data/data.txt
七、获取当前时间class
curr_year=`date +%Y` curr_month=`date +%m` curr_day=`date +%d` curr_hour1=`date +%k` curr_hour2=`date +%M` dt=${curr_year}${curr_month}${curr_day} curr_hour=${curr_hour1}${curr_hour2}
八、将输出值保存到变量中test
username=$(echo xiao) echo ${username}
九、判断某目录是否已存在变量
if [ -d "./bak/${dt}" ];then echo 目录已存在 else mkdir ./bak/${dt} fi
十、定义函数date
lab1() { mv ./*.txt ./bak/${dt}/ source ./test1.sh mv ./log/*.log ./bak/${dt}/${curr_hour}.log if ls ./data/*.bad > /dev/null ;then mv ./data/*.bad ./bak/${dt}/${curr_hour}.bak else echo no *.bad file fi source ./test2.sh echo "call back" rm ./begin.log exit } lab2() { rm ./begin.log exit }
须要注意的是函数的定义应放置在脚本的最上方,保证调用的时候已经声明了该函数,调用函数的时候直接写函数的名称就好了,例如
if [ -f "./begin.log" ];then lab2 else lab1 fi
上述例子实现了对于不一样状况下调用不用函数
十一、shell文件调用其余shell文件有三种方式
a、fork:新开一个子 Shell 执行,子 Shell 能够从父 Shell 继承环境变量,可是子 Shell 中的环境变量不会带回给父 Shell。
b、exec:在同一个 Shell 内执行,可是父脚本中 exec
行以后的内容就不会再执行了
c、source:在同一个 Shell 中执行,在被调用的脚本中声明的变量和环境变量, 均可以在主脚本中进行获取和使用,至关于合并两个脚本在执行。