shell编程的几个例子来了解shell脚本
2018-01-10 10:33
239 查看
以前没有接触过shell脚本,春节期间在老家看国嵌的视频,上面介绍了shell脚本的几个例子,就动手操作了一遍,挺有感觉,所以记录下来,供以后参考。
第一个:比较两个数的大小
在shell编程中,如果多个关键字写在一行的话,关键字之间应该用分号隔开,否则会出现错误无法运行。
比如下面的程序:
#!/bin/bash
#将第一个命令行参数传递给变量a,第二个命令行参数传递给变量b
a=$1
b=$2
#判断a或者b变量是否为空, 只要有一个为空就打印提示语句并退出
if [ -z $a ] || [ -z $b ]
then
echo "please enter 2 no"
exit 1
#判断a和b的大小,并根据判断结果打印语句
fi
if [ $a -eq $b ] ;then #这个的分号必须存在
echo "number a = number b"
else if [ $a -gt $b ]
then
echo "number a>number b"
elif [ $a -lt $b ]
then
echo "number a<number b"
fi
fi
如果把分号删去的话,会出现这样的错误:
# ./tmp 4 4
./tmp: line
d692
16: syntax error near unexpected token `else'
./tmp: line 16: `else if [ $a -gt $b ] '
所以为了美观起见,应该分行写:
#!/bin/bash
#将第一个命令行参数传递给变量a,第二个命令行参数传递给变量b
a=$1
b=$2
#判断a或者b变量是否为空, 只要有一个为空就打印提示语句并退出
if [ -z $a ] || [ -z $b ]
then
echo "please enter 2 no"
exit 1
#判断a和b的大小,并根据判断结果打印语句
fi
if [ $a -eq $b ]
then
echo "number a = number b"
else if [ $a -gt $b ]
then
echo "number a>number b"
elif [ $a -lt $b ]
then
echo "number a<number b"
fi
fi
第二个:统计文件夹中文件的数目
#!/bin/bash
#变量counter用于统计文件的数目
counter=0
#变量files遍历一遍当前文件夹
for files in *
do
#判断的files是否是文件,如果是就将counter变量的值加一再赋
#给自己
if [ -f "$files" ]
then
counter=`expr $counter + 1`
fi
done
#输出结果
echo "There are $counter files in `pwd`"
第三个例子:讲一个数字逆序
#!/bin/bash
#提示用户输入
echo -n "Pleasw enter number : "
read n #读入输入的值放到变量n中
sd=0
rev=""
on=$n #将变量n的值保存到变量on中,方便以后用到
echo "You put number is $n"
while [ $n -gt 0 ]
do
sd=$(( $n % 10 )) #求余
n=$(( $n / 10 )) #去掉当前的最后一位数后剩下的数
rev="$rev$sd" #将当前的最后一位数放到字符串之后
done
echo "$on in a reverse order $rev"
第四个例子:移动一文件,如果目标位置存在该文件,则监视该文件,直到文件被删除后才移动该文件
#!/bin/bash
#判断命令行是否代带有两个文件名的参数
if [ "$1" = "" ] || [ "$2" = "" ]
then
echo "Please enter file name"
exit 1
fi
#判断目标文件是否存在
if [ -e $2 ]
then
echo "The file already exists"
until [ ! -f $2 ] #监视该文件是否被删除
do
sleep 1
done
echo "The file have been deleted"
fi
#执行源文件移动为目标文件的命令
if [ ! `mv $1 $2` ]
then
echo "mv sucessful"
else
echo "mv error"
fi
第一个:比较两个数的大小
在shell编程中,如果多个关键字写在一行的话,关键字之间应该用分号隔开,否则会出现错误无法运行。
比如下面的程序:
#!/bin/bash
#将第一个命令行参数传递给变量a,第二个命令行参数传递给变量b
a=$1
b=$2
#判断a或者b变量是否为空, 只要有一个为空就打印提示语句并退出
if [ -z $a ] || [ -z $b ]
then
echo "please enter 2 no"
exit 1
#判断a和b的大小,并根据判断结果打印语句
fi
if [ $a -eq $b ] ;then #这个的分号必须存在
echo "number a = number b"
else if [ $a -gt $b ]
then
echo "number a>number b"
elif [ $a -lt $b ]
then
echo "number a<number b"
fi
fi
如果把分号删去的话,会出现这样的错误:
# ./tmp 4 4
./tmp: line
d692
16: syntax error near unexpected token `else'
./tmp: line 16: `else if [ $a -gt $b ] '
所以为了美观起见,应该分行写:
#!/bin/bash
#将第一个命令行参数传递给变量a,第二个命令行参数传递给变量b
a=$1
b=$2
#判断a或者b变量是否为空, 只要有一个为空就打印提示语句并退出
if [ -z $a ] || [ -z $b ]
then
echo "please enter 2 no"
exit 1
#判断a和b的大小,并根据判断结果打印语句
fi
if [ $a -eq $b ]
then
echo "number a = number b"
else if [ $a -gt $b ]
then
echo "number a>number b"
elif [ $a -lt $b ]
then
echo "number a<number b"
fi
fi
第二个:统计文件夹中文件的数目
#!/bin/bash
#变量counter用于统计文件的数目
counter=0
#变量files遍历一遍当前文件夹
for files in *
do
#判断的files是否是文件,如果是就将counter变量的值加一再赋
#给自己
if [ -f "$files" ]
then
counter=`expr $counter + 1`
fi
done
#输出结果
echo "There are $counter files in `pwd`"
第三个例子:讲一个数字逆序
#!/bin/bash
#提示用户输入
echo -n "Pleasw enter number : "
read n #读入输入的值放到变量n中
sd=0
rev=""
on=$n #将变量n的值保存到变量on中,方便以后用到
echo "You put number is $n"
while [ $n -gt 0 ]
do
sd=$(( $n % 10 )) #求余
n=$(( $n / 10 )) #去掉当前的最后一位数后剩下的数
rev="$rev$sd" #将当前的最后一位数放到字符串之后
done
echo "$on in a reverse order $rev"
第四个例子:移动一文件,如果目标位置存在该文件,则监视该文件,直到文件被删除后才移动该文件
#!/bin/bash
#判断命令行是否代带有两个文件名的参数
if [ "$1" = "" ] || [ "$2" = "" ]
then
echo "Please enter file name"
exit 1
fi
#判断目标文件是否存在
if [ -e $2 ]
then
echo "The file already exists"
until [ ! -f $2 ] #监视该文件是否被删除
do
sleep 1
done
echo "The file have been deleted"
fi
#执行源文件移动为目标文件的命令
if [ ! `mv $1 $2` ]
then
echo "mv sucessful"
else
echo "mv error"
fi
相关文章推荐
- 几个Shell脚本的例子
- 两年前放在百度文库的几个初学shell脚本时写的几个简单例子
- *Linux Shell 高级编程技巧4----几个常用的shell脚本例子
- shell脚本中,子串匹配的例子
- shell脚本学习-1.5(小例子)
- 通过几个例子来了解Handle Runnable Thread HandleThread的关系.
- 采集oracle资源shell脚本例子
- shell脚本编写小例子——工作笔记
- 第二部分 Linux Shell高级编程技巧——第四章 几个脚本例子——终结篇
- shell脚本几个常见问题
- shell脚本例子
- 5个Shell脚本编程入门练习例子
- cgi简介以及一个用shell脚本写的shell例子分析
- VBS脚本,中间用到了遍历文件夹,正则匹配。使用了 WScript.Shell 对象的几个方法。
- 简单的几个shell脚本一
- Shell脚本实现自动发送邮件的例子
- 5个Shell脚本编程入门练习例子
- shell脚本几个练习
- 一个shell脚本例子
- 几个不错的Shell脚本