shell脚本语法学习

以下内容仅为个人学习使用,如有错误,欢迎指出

持续更新...............

1.首先创建.sh文件,打开并在文件的第一行输入#!/bin/sh

2.变量的写法,及用法

test='我是变量'
echo "$test"
或者
echo $test

3.echo命令

用来输出文字或变量到控制台:用法请看上面

4.read命令

用来读取用户在控制台上输入的内容

echo "please input you content"
#这里content自定义,就是一个变量名
read content echo "you input content is $content"

5.或持续更新命令

先上两小段代码吧

# 执行系统命令语法: `系统命令`
ip=`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'` #ip1=/sbin/ifconfig|sed -n '/inet addr/s/^[^:]*:\\([0-9.]\\{7,15\\}\\) .*/\\1/p' | grep -v 127.0.0.1 while True; do echo "$ip" ipp=`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'` echo "$ipp" if [ "$ip" != "$ipp" ]; then `sudo shutdown -h 16:00` else echo "正常" fi sleep 1s done

这段代码主要是监控ip地址是否发生改变,如何IP地址改变则执行关机

#!/bin/sh
  
echo "please input you name"
read name
echo "please input one number:"
read numbero
echo "please input one number again:"
read numbert
if [[ x$name == 'x' || x$numbero == 'x' || x$numbert == 'x' ]]
then
        echo "you input has null"
elif [[ $numbero -gt $numbert && $name == 'msw' ]]
then
        echo "you input one big"
elif [ $numbero -lt $numbert ]
then
        echo "you input two big"
else
        echo "ne"
fi

这段代码没什么意思,主要是联系我见天学习的几个命令和判断语法

重难点详解

|| 或

&& 和

-gt 大于

-lt 小于

x$name == 'x' 判断输入是否为空,还可以写成其他的

注意:在[[]]中才可使用&& ||语法,如果是[],和是-a, 或是-o

  还有要注意字段间的空格

今天就先到这里吧!

猜你喜欢

转载自www.cnblogs.com/mswyf/p/9965456.html
今日推荐