shell学习课程2
变量相关
foo=bar #变量赋值
echo $foo #输出:bar
echo "Value is $foo" #输出:Value is bar
echo 'Value is $foo' #输出:Value is $foo
参数相关
mcd()
{
mkdir -p "$1" #$1是该脚本接受第一个参数,同理$1-$9是指第一道第九个参数
cd "$1"
}
if语句判断
#!/bin/bash
NUM1=100
NUM2=200
if (($NUM1>$NUM2));then
echo "$NUM1 is greater than $NUM2"
else
echo "$NUM1 is not greater than $NUM2"
fi
(())是比较专用!!另外
-f 判断文件是否存在 eg if [ -f filename ]
-d 判断目录是否存在 eg if [ -d dir ]
判断目录是否存在,不存在则重新创建.
#!/bin/bash
DIR=/tmp/20200216
if [ ! -d $DIR ];then
mkdir -p $DIR
echo "This $DIR create successfully."
else
echo "This $DIR exists, please exit."
fi
/bin/bash -n if_dir.sh用来测试该文件有无错误
打印日期
echo `date +%Y%m%d`
20200216
echo $(date +%Y%d%m)
20200216

824

被折叠的 条评论
为什么被折叠?



