加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

shell基础

发布时间:2020-12-16 01:38:31 所属栏目:安全 来源:网络整理
导读:编写Shell?Script的步骤 1、vim xxx.sh 2、编写脚本内容 3、chmod u+x xxx.sh 4、/xxx.sh # 注释 ? #

编写Shell?Script的步骤

1、vim xxx.sh

2、编写脚本内容

3、chmod u+x xxx.sh

4、/xxx.sh


#     注释

?

#!   选择解释器

#!/bin/bash
#!/bin/sh

?

echo    输出 相当于printf? echo自动换行且不支持转义

      -n 不换行 -e 支持转义?

echo hello    
echo hello‘    //‘’中的内容即为屏幕显示出来的内容
echo "hello"    //在""中可以添加$变量

?

read?    输入,后面可接多个变量

read A B C     //连续输入三个变量

?

执行shell命令时,除命令之外其他部分视为参数

./xxx.sh aaa bbb ccc         //默认认为aaa bbb ccc为三个参数

?

特殊变量

?????$* ?表示此程序的所有参数(不包括$0)

?????$# ?表示此程序的参数个数(不包括$0)

?????$$ ?表示此程序的PID

?????$! ?表示执行上一个后台程序的PID

?????$? ?表示上一个程序的返回值

#!/bin/bash
echo $*
echo $#
echo $$
echo $!
echo $?

输出的结果

[[email?protected] linux_share]$ ./shell.sh aaa bbb ccc
aaa bbb ccc
3
27014

0

?

=?  赋值 无空格

A="hello world"
B=$A      //把A的值赋给B
C=`data`    //把命令data的结果赋给C

?

expr  运算

expr  3 + 4 - 5                    //每个符号都要空格
A=`expr  3 + 4 - 5`            //将结果赋值给变量   
B=`expr 3  *  5`               //乘号*要用转义符
C=`expr 5 + ( 3 + 7 )`     //括号()也要用转义符

?

test?  变量测试

test??str1 = str2 ?????判断字符串相等(这里=两侧必须有空格,确保test能够正确识别测试的规则)

test??str1?!= str2 ????判断字符串不相等

test??str1 ??????????判断字符串不为空

test??-n str1 ????????判断字符串不为空

test ?-z str1 ????????判断字符串为空

?

test ?int1 -eq int2 ????判断整数相等

test??int1 -ge int2 ????判断大于等于

test??int1 -gt?int2 ????判断大于

test ?int1 -le?int2 ????判断小于等于

test??int1 -lt?int2 ????判断小于

test??int1 -ne?int2 ???判断不相等

?

test??-d file ????判断文件为目录文件

test??-f file ????判断文件为普通文件

test??-x file ????判断文件有可执行权限

test??-r file ????判断文件有可读权限

test??-w?file ????判断文件有可写权限

test??-a file ?????判断文件存在

test ?-s file ?????判断文件大小大于0

?

if  选择结构

if  [ 测试条件 ]

then

   条件成立时执行的语句

fi
if  [ 测试条件 ]

then

   条件成立时执行的语句

else 

    条件不成立时的语句

fi
if  [ 测试条件1 ]

then

   条件1成立时执行的语句

elif [ 测试条件2 ]

then

    条件2成立时执行的语句

elif [ 测试条件3 ]

then

    条件3成立时执行的语句

else 

    条件全不成立时的语句

fi
#!/bin/bash
read A B
if [[ A < B ]]
then
    echo "$B big big"
elif [[ A > B ]]
then
    echo "$A big big"
else 
    echo "one sheep big"
fi

上例 为输入两个数,并比较大小

?

逻辑

在判断条件中?使用 -a?表示逻辑与

在判断条件中 使用 -o?表示逻辑或

在判断条件中 使用 ! ?表示逻辑非

#!/bin/bash
read A B
if [[ $A < $B && $A = $B ]]
then
    echo "$B big big"
elif [[ $A > $B ]]
then
    echo "$A big big"
fi
#!/bin/bash
read A B
if [  $A -lt $B  -o  $A -eq $B  ]
then
    echo "$B big big"
elif [[ $A > $B ]]
then
    echo "$A big big"
fi

?

?case选择结构

case 数据 in 
    1)命令1
        ;;
    2)命令2
        ;;
    3)命令3
        ;;
    *)不满足123时候的命令  类似于switch的default
        ;;
esac

?

?

?

for循环

for 变量 in 列表
do
    循环体
done

举个栗子

打印1-10

for A in 1 2 3 4 5 6 7 8 9 10
do 
    echo "$A"
done

列表里可以是数据 可以是变量 也可以是其他命令的执行结果

?

?

?while循环

while 条件
do
    循环体
done

?

?

?

exit(0)

结束脚本

?

?

shift 参数左移

类似于<< 参数整体左移一位,丢失第一个参数,参数总数-1

?

?

?

.

?

$

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读