Linux shell基本语法

1.shell变量
一般shell的变量赋值的时候不用带“$”,而使用或者输出的时候要带“ $ ”。加减乘除的时候要加两层小括号。括号外面要有一个“ $ ”,括号里面的变量可以不用“$”。需要注意的是,变量赋值,变量使用的时候不能有空格,否则会被解析成命令,报错无此命令。

#!/bin/bash
a=1
b=2
c=$((a+b))
echo $c
echo "a = "$a #输出a的值

2 .shell变量表达式
在这里插入图片描述

#!/bin/bash
str="a,b,c,d,e,f,g,h,i,j"


echo "the source string is "${str}                         #源字符串
echo "the string length is "${#str}                        #字符串长度
echo "the 6th to last string is "${str:5}                  #截取从第五个后面开始
到最后的字符
echo "the 6th to 8th string is "${str:5:2}                 #截取从第五个后面开始
的2个字符
echo "after delete shortest string of start is "${str#a*f} #从开头删除a到f的字符
echo "after delete widest string of start is "${str##a*}   #从开头删除a以后的字>符
echo "after delete shortest string of end is "${str%f*j}   #从结尾删除f到j的字符
echo "after delete widest string of end is "${str%%*j}     #从结尾删除j前面的所>有字符包括j

结果如下:

the source string is a,b,c,d,e,f,g,h,i,j
the string length is 19
the 6th to last string is ,d,e,f,g,h,i,j
the 6th to 8th string is ,d
after delete shortest string of start is ,g,h,i,j
after delete widest string of start is
after delete shortest string of end is a,b,c,d,e,
after delete widest string of end is

3.shell测试判断test或[]

需要注意的是使用[]的时候必须要每个变量之间都要有空格,和左右中括号也要有空格,否则报错

test 文件运算符

利用这些运算符,您可以在程序中根据对文件类型的评估结果执行不同的操作:

   -b file 如果文件为一个块特殊文件,则为真

  -c file 如果文件为一个字符特殊文件,则为真

  -d file 如果文件为一个目录,则为真

  -e file 如果文件存在,则为真

  -f file 如果文件为一个普通文件,则为真

  -g file 如果设置了文件的 SGID 位,则为真

  -G file 如果文件存在且归该组所有,则为真

  -k file 如果设置了文件的粘着位,则为真

  -O file 如果文件存在并且归该用户所有,则为真

  -p file 如果文件为一个命名管道,则为真

  -r file 如果文件可读,则为真

  -s file 如果文件的长度不为零,则为真

  -S file 如果文件为一个套接字特殊文件,则为真

  -t fd 如果 fd 是一个与终端相连的打开的文件描述符(fd 默认为 1),则为真

  -u file 如果设置了文件的 SUID 位,则为真

  -w file 如果文件可写,则为真

  -x file 如果文件可执行,则为真

4.shell条件分支结构语句
1).单分支判断语句

格式:if 条件 ; then 结果 fi ,最后面一定要有fi,在shell脚本里面,控制分支结构结束都要和开头的单词相反,例如,if <–> fi,case <–> esac。

#!/bin/bash

echo "Please input a filename"
read filename
if [ -f $filename ];then
echo "this file is a ordinary file."
fi

2).双分支判断语句

echo "Please input a filename"
read filename
if [ -f $filename ];then
echo "this file is a ordinary file."
else
echo "this file is not a ordinary file."
fi

3).多分支判断语句

if [ "$A" == "$B" ] || [ "$C" == "$B" ];
then
    ......
elif [ "$D" == "$E" ];
then
    .....
else
    .....
fi

case语句

case $A in
    "A")
        ....
    ;;
    "B")
        ....
    ;;
    esac

5.循环表达式:

while [ .... -a .... ]
do
    ....
done

for循环

for $A in A B C
do
    ....
done

for (( i=1;i<50;i=i+1 ))
do
    s=$(($A+$B))
done

6、定义函数:

function print()
{
    echo $1
}

7、break和continue:

break跳出循环。break n(默认为1跳出本次循环,2则是跳出上次循环)

if [ ... ];then
    break
fi

continue终止本次循环,开始下次循环。

if [ ... ];then
    continue
fi
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值