Bash 实例

来源:互联网 发布:一级建造师网络班 编辑:程序博客网 时间:2024/05/06 10:39
#实例一
#!/bin/sh#排序三个输入的字符串echo please input three strings ending with  ENTERread str1read str2read str3if [  $str1 -gt   $str2  ]then echo str1 gt str2tmp=$str1str1=$str2str2=$tmpfiif [  $str2 -gt   $str3  ]then echo str2 gt str3tmp=$str2str2=$str3str3=$tmpfi# 经过前两次排序比较之后,结果最后一个字符串最大,只需要比较前面两个,类似于冒泡if [  $str1  -gt  $str2  ]then echo str1 gt str2tmp=$str2str2=$str1str1=$tmpfiecho the strings after compare,the results are below echo $str1 $str2 $str3

#!/bin/bashecho $#echo please input two variables  ending with ENTERread  iValue1read  iValue2echo "the result of '*' is `expr $iValue1 \* $iValue2`"echo "the result of '/' is `expr $iValue1 / $iValue2`"echo "the result of '+' is `expr $iValue1 + $iValue2`"echo "the result of '-' is `expr $iValue1 - $iValue2`"

#!/bin/bash#判断一文件是不是字符设备文件,如果是将其拷贝到 /dev 目录下echo "请输入你的文件路径"read filenameecho `basename $filename`if[  -e  $filename ]thenecho "`basename $filename` 文件存在"  #注意是反引号if   [  -c  $filename ]thenecho "`basename $filename` 是字符文件" cp $filename   /dev/elseecho "`basename $filename` 不是字符文件"  fielseecho "`basename $filename` 文件不存在" fi
#三个简单实例帮助理解使用bash

                                             
0 0