Shell 编程入门
创始人
2025-01-20 20:03:31
0

优质博文: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 < /test/shelldir/ex2.info && /bin/rm /test/shelldir/ex2.info crontab -e                                                                         #定时执行命令 0 9 * * 1-5 /bin /sh /test/ex2.sh                                                  #表示:每周一到周五的9点执行该脚本。 

【6】变量:shell传递数据的一种方式,用来代表每个取值的符号。
Shell的变量有两种:①、永久变量 ②、临时变量。临时变量是shell程序内部定义的,其使用范围只限于定义它的程序,其它程序不可见。包括程序自定义变量、位置变量。永久变量是环境变量。其值不随 shell脚本的执行结束而消失。

【永久变量】:
点击并拖拽以移动​

【自定义变量】: 在使用变量时,要在变量前面加前缀:$,一般变量使用大写字母表示,并且是英文字母开头,赋值号“=”两边没有空格,如:NUM=5STR="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 

相关内容

热门资讯

终于知道!wepoker有透视... 终于知道!wepoker有透视底牌吗,微信闲来辅助神器,大纲教程(有挂辅助)1、完成微信闲来辅助神器...
科技分享!wepoker怎么拿... 科技分享!wepoker怎么拿到好牌,大唐麻将开挂软件,举措教程(揭秘有挂)1、玩家可以在大唐麻将开...
值得注意的是!线上德州的辅助器... 值得注意的是!线上德州的辅助器是什么,逗娱碰胡开挂,指南书教程(有挂猫腻)1)逗娱碰胡开挂有没有挂:...
分享认知!epoker免费透视... 分享认知!epoker免费透视脚本,微乐小程序辅助开发,方针教程(有挂功能)微乐小程序辅助开发是不是...
为切实保障!wepoker透视... 为切实保障!wepoker透视底牌脚本,福建13水软件辅助,法门教程(的确有挂)1.福建13水软件辅...
据公告内容!约局吧德州真的存在... 据公告内容!约局吧德州真的存在透视吗,天道手游辅助,手册教程(有挂详情)1、用户打开应用后不用登录就...
科技通报!wepoker游戏下... 科技通报!wepoker游戏下载,免费挂机辅助工具,手册教程(有挂教学)1、操作简单,无需免费挂机辅...
今日科普!wepoker底牌透... 今日科普!wepoker底牌透视脚本,微信小程序多乐辅助器,大纲教程(有挂技巧)1.微信小程序多乐辅...
事发当天!德州透视hhpoke... 事发当天!德州透视hhpoker,微信呢途游四川辅助器,绝活教程(有挂详细)1、玩家可以在微信呢途游...
分享一款!hhpoker德州挂... 分享一款!hhpoker德州挂真的有吗,杭州都莱有没有挂,窍门教程(有挂详细);杭州都莱有没有挂能透...