20200614 linux shell

写在前面:

昨天答完辩了,感觉好水啊,答辩老师感觉很随意,问的问题也很没技术含量。只是一味跳着论文格式的问题,问了其他同学,大概也是这个样子,希望能过吧。还要接着改论文,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权限
-xx权限
-ww权限
-uSUDI属性
-gSGID属性
-s检测该文件是否存在,且为空文件
比对两个文件test file1 -nl file2
-nt判断file1是否比file2新
-ot是不是更旧的
-ef是不是为同一个文件
比对两个数值test n1 -eq n2
-eq两数值是不是相等
-ne两数值不等
-gtn1大于n2
-ltn1小于n2
-gen1大于等于n2
-len1小于等于n2
test -z string判断字符串是否为空,为空返回true
test -n string判断字符串是否为空,为空返回fale
test str1 == str2判断是不是相等
test str1 != str2不相等
test -r filename -a -x filename 
-aand
-oor
!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

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值