Bash 实例

#实例一
#!/bin/sh
#排序三个输入的字符串
echo please input three strings ending with  ENTER
read str1
read str2
read str3

if [  $str1 -gt   $str2  ]
then 
	echo str1 gt str2
	tmp=$str1
	str1=$str2
	str2=$tmp
fi

if [  $str2 -gt   $str3  ]
then 
	echo str2 gt str3
	tmp=$str2
	str2=$str3
	str3=$tmp
fi
# 经过前两次排序比较之后,结果最后一个字符串最大,只需要比较前面两个,类似于冒泡

if [  $str1  -gt  $str2  ]
then 
	echo str1 gt str2
	tmp=$str2
	str2=$str1
	str1=$tmp
fi

echo the strings after compare,the results are below 
echo $str1 $str2 $str3

#!/bin/bash
echo $#
echo please input two variables  ending with ENTER
read  iValue1
read  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`"
echo "the result of '-' is `expr $iValue1 - $iValue2`"

#!/bin/bash

#判断一文件是不是字符设备文件,如果是将其拷贝到 /dev 目录下

echo "请输入你的文件路径"
read filename
echo `basename $filename`
if	[  -e  $filename ]
then
	echo "`basename $filename` 文件存在"  #注意是反引号
	if   [  -c  $filename ]
	then
		echo "`basename $filename` 是字符文件" 
		cp $filename   /dev/
	else
		echo "`basename $filename` 不是字符文件"  
	fi
else
	echo "`basename $filename` 文件不存在" 
fi
#三个简单实例帮助理解使用bash



                                    
posted @ 2015-09-14 20:20  cloudren2020  阅读(74)  评论(0编辑  收藏  举报