Shell 编程入门

2024-07-13 1227阅读

优质博文:IT-BLOG-CN

【1】x.sh文件内容编写: 固定开头:#!/bin/sh;

【2】学习的第一个命令就是echo输出的意思;

【3】其实shell脚本也就是在文件中写命令,但是我们要写的是绝对路径:eg:/bin/pwd;

【4】运行shell脚本:sh文件名;

【5】通过下面脚本进行学习:

#!/bin/sh
/bin/date +%F >> /test/shelldir/ex2.info                                           #data +%F是将日期格式化。>>追加输出
echo "disk info:" >> /test/shelldir/ex2.info
/bin/df -h >> /test/shelldir/ex2.info
echo >> /test/shelldir/ex2.info
echo "online users:" >> /test/shelldir/ex2.info
/usr/bin/who | /bin/grep -v root >> /test/shelldir/ex2.info                        #使用的命令主要来自两个地方:①、/bin/ ②、/usr/bin/  -v:表示排除
echo "memory info:" >> /test/shelldir/ex2.info
/usr/bin/free -m >> /test/shelldir/ex2.info
echo >> /test/shelldir/ex2.info
#write root
/usr/bin/write root  

【6】变量: 是shell传递数据的一种方式,用来代表每个取值的符号。

Shell的变量有两种:①、永久变量 ②、临时变量。临时变量是shell程序内部定义的,其使用范围只限于定义它的程序,其它程序不可见。包括程序自定义变量、位置变量。永久变量是环境变量。其值不随 shell脚本的执行结束而消失。

【永久变量】:

Shell 编程入门

【自定义变量】: 在使用变量时,要在变量前面加前缀:$,一般变量使用大写字母表示,并且是英文字母开头,赋值号“=”两边没有空格,如:NUM=5、STR="A string"。可以将一个命令的执行结果赋值给一个变量:但需要使用命令替换符。NUM=data注意单引号和双引号之间的区别,“”是会将里面的变量值进行输出,‘’会将里面的内容原封不动的进行输出,不会识别里面的变量。使用set查看所有变量。查询$变量。使用unset命令删除指定的变量。

【7】占位变量: 在shell中还有两种常用的变量,一种是占位变量,还有一种是特殊变量,在编写shell到的时候特别常用:

【位置变量】: ls -l file1 file2 file3...(n范围=1-9)在代码里使用$0-9进行替换。也就是说查看file1目录地下的所有文件信息。

#!/bin/sh
DATE=`/bin/date +%Y%m%d`
echo "TODAY IS $DATE"
/bin/ls -l $1
/bin/ls -l $2
/bin/ls -l $3

在命令行输入:sh文件名/test /usr/bin /home。解释:就是让命令中的/test替换脚本中的$1........

【8】特殊变量:

■ $*这个程序的所有参数;

■ $#这个程序的参数个数;

■ $$这个程序的PID;

■ $! 执行上一个程序的PID;

■ $?执行上一个命令的返回值;

■ $(0-9)显示位置变量;

【9】read:键盘输入,命令:read从键盘读取数据,赋给变量。

#!/bin/sh
read f s t
echo "the first is $f"
echo "the second is $s"
echo "the third is $t"

执行命令: sh文件名先执行,在输入变量。如果输入:sh -x文件名执行 shell脚本,当执行到read时会弹出:read f s t,然后我们根据需求输入,例如:10 20 30;

【10】shell的运算: expr命令,对整数进行运算。注意点:

①、expr的计算必须用空格隔开;

②、\*表示转义字符;

③、保持先算乘除后算加减,如果需要优先算法需要加命令替换符;

④、可以对变量进行运算;

【11】test测试命令: 使用test命令可以对文件、字符串等进行测试,一般配合控制语句使用,不应该单独使用。

【12】if语句,语法格式:

if [ -d $1 ]
then
else
fi

【实例展示】

#!/bin/sh
# if test $1 then ... else ... fi
if [ -d $1 ]
then
    echo "this is a directory!"
else
    echo "this is not a directory!"
fi

【if elif语法】

#!/bin/sh
# if test  then ... elif test then ... else ... fi
if [ -d $1 ]
then
    echo "is a directory!"
elif [ -f $1 ]
    then
    echo "is a file!"
else
    echo "error!"
fi

【逻辑 与-a和 或-o】

#!/bin/sh
# -a -o
if [ $1 -eq $2 -a $1 = 1 ]
    then
    echo "param1 == param2 and param1 = 1"
elif [ $1 -ne $2 -o $1 = 2  ]
    then
    echo  "param1 != param2 or param1 = 2"
else
     echo "others"
fi

【for循环】

#!/bin/sh
# for var in [params] do ... done
for var in 1 2 3 4 5 6 7 8 9 10
do 
    echo "number is $var"
done

【select循环】

#!/bin/sh
# select var in [params] do ... done
select var in "java" "c++" "php" "linux" "python" "ruby" "c#"
do
    break
done
echo "you selected $var"

【case循环】

#!/bin/sh
read op
case $op in
        a)
     echo "you selected a";;
        b)
    echo "you selected b";;
    c)
    echo "you selected c";;
    *)
    echo "error"
esac

【while循环】

#!/bin/sh
#while test do ... done
num=1
sum=0
while [ $num -le 100 ]                      #le表示小于等于
do
    sum=`expr $sum + $num`
    num=`expr $num + 1`
done
#sleep 5
echo $sum
VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]