Shell 编程入门

avatar
作者
猴君
阅读量:2

优质博文: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 

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!