写在前面:
昨天答完辩了,感觉好水啊,答辩老师感觉很随意,问的问题也很没技术含量。只是一味跳着论文格式的问题,问了其他同学,大概也是这个样子,希望能过吧。还要接着改论文,mmp。原本说给两天改论文的时间,但是这是第四天了,也没说啥时候再收。同学们大多15号都返校了,其实我也想15号返校,因为air pods pro左耳出问题了,有电流声音,想着去西安换一下,毕竟长春没几个店,而且不太好,吉林更是没有店。可是,我跟导员说是吉林市的,他就暗示我不要回去了,虽然没明说,但是我有点反感这样暧昧不明的话,同时又佩服导员能说出这种有水平的话,我想我打死也说不这种话。
618终于决定买kindle了,但是突然听说小米阅读器,系统是安卓的可玩性比kindle高,就买了它了。
我又看了看linux的shell 脚本,可能是因为只是看过,并没有实际编写过linux脚本,所以总是忘记写法。
demo1
注意赋值没空格,整数计算$(())
#!/bin/bash
echo -e "input 2 integer number \n"
read -p "first number" firstnu
read -p "second number" secnu
total=$((${firstnu}+${secnu})) # 注意着的赋值,等号两边没有空格
echo "result ${total}"
source sh区别
source | 就一个进程,在父进程中执行 |
sh | 开启一个子进程,在子进程中执行 |
test 命令
-e | 该文件名是否存在 |
-f | 改文件名是不是存在且为文件 |
-d | 改文件名是不是存在且为目录 |
-r | 检测文件是不是用r权限 |
-x | x权限 |
-w | w权限 |
-u | SUDI属性 |
-g | SGID属性 |
-s | 检测该文件是否存在,且为空文件 |
比对两个文件 | test file1 -nl file2 |
-nt | 判断file1是否比file2新 |
-ot | 是不是更旧的 |
-ef | 是不是为同一个文件 |
比对两个数值 | test n1 -eq n2 |
-eq | 两数值是不是相等 |
-ne | 两数值不等 |
-gt | n1大于n2 |
-lt | n1小于n2 |
-ge | n1大于等于n2 |
-le | n1小于等于n2 |
test -z string | 判断字符串是否为空,为空返回true |
test -n string | 判断字符串是否为空,为空返回fale |
test str1 == str2 | 判断是不是相等 |
test str1 != str2 | 不相等 |
test -r filename -a -x filename | |
-a | and |
-o | or |
! | test ! -x file 相反状态 |
demo2
#!/bin/bash
read -p "input a filename" filename
test -z ${filename} && "You must input a filename." && exit 0
test ! -e ${filename} && echo "The filename do not exits" && exit 0
[]判断符号
注意 ["$HOME"=="$MALL"] 注意空格。
#!/bin/bash
read -p "input {Y/N}:" yn
[ "${yn}" == "y" -o "${Y}" ] && echo "ok" && exit 0
脚本的默认变量
$0 | 脚本的名字 |
$1 | 第一个变量 |
$2 | 第二个变量 |
$# | 表示变量的个数 |
$@ | 代表["$1""$2"] |
#!/bin/bash
[ "$#" -lt 2 ] && echo "The number of parameter is less than 2.Stpp here" && exit 0
echo "whole parameter is $@"
echo "the script name is $0"
echo "first parameter is $1"
条件判断式
&& ||
[ "${yn}" == "Y" -o "${yn} == "y" ] 等价于下面的逻辑
[ "${yn}" == "Y" ] || ["${yn}" == "y"]
if ... then
if [ `command -v command` ]; then
# body
fi
#!/bin/bash
read -p "input (y)" yn
if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then
echo "ojbk"
exit 0
fi
if [ `command -v command` ]; then
# body
elif condition ;then
# body
else
# body
fi
case...esac
case $VAR in
1) echo 1
;;
2|3) echo 2 or 3
;;
*) echo default
;;
esac
#!/bin/bash
case ${1} in
"hello")
echo "hello"
;;
"")
echo "You must input someting"
;;
*)
echo "Usage ${0} {hello}"
;;
esac
利用function功能
function 一定要定义在最前面
function name()
{
}
#!/bin/bash
function printit()
{
echo "Your choice is ${1}" # 这个是函数的参数,不是执行脚本传进来的参数
}
case ${1} in
"one")
printit 1 # 给这个函数传了一个参数
;;
"two")
printit 2
;;
"three")
printit 3
;;
*)
echo "Usage ${0} {one|two|three}"
esac
循环:
但条件成立时一直循环
while [ condition ]
do
# body
done
当条件不成立时,退出循环
until [ condition ]
do
# body
do
#!/bin/bash
i=0
s=0
until [ ${i} -ge 100 ] # 只能是-gt 这种格式, >这种符号不行
do
i=$((${i}+1))
s=$((${s}+${i}))
done
echo "${s}"
for...do...done
记住seq,它是连续的意思。
for i in con1 con2
do
# body
done
#!/bin/bash
for i in dog cat ant
do
echo $i
done
#!/bin/bash
s=0
for i in $(seq 1 100) # seq 是连续的意思
do
s=$(($s+$i))
done
echo "$s"
for (( 初始值;限制值;赋值运算))
do
# body
done
#!/bin/bash
read -p "num" nu
s=0
for ((i=1;i<=${nu};i=i+1))
do
s=$((${s}+${i}))
done
echo "${s}"
shll脚本测试
-n 不执行脚本,仅仅检查语法问题
-v 执行脚本前,将脚本文件的内容输出到屏幕上
-x 将使用到的脚本内容显示到屏幕上。
sh -n test.sh