`

BASH脚本-写着玩

阅读更多

#====================================================1
CASE 用法举例
#====================================================
#! /bin/bash
#filename:caseTest
echo _
echo "1"
echo "2"
echo "3"
echo "-n 选择:"
read CHOICE
case "$CHOICE" in
1) echo "你选择了1";;
2) echo "你选择了2";;
3) echo "你选择了3";;
*) echo "对不起,$CHOICE 不是一个有效的选项"
exit 1
esac

#====================================================2
IFS 特殊字符用法举例???? 定制分割函数参数的标记、函数、循环参数
#====================================================
#!/bin/bash
#$IFS deal it si not the same as other characters

output_args_one_per_line()
{
  for arg
  do
    echo "[$arg]"
  done
}

echo;
echo "IFS=\" \""
echo "------"
IFS=" "
var="a b c?? "
output_args_one_per_line $var

echo
echo "IFS=:"
echo "------"
IFS=:
var=":a::b:c:::"
output_args_one_per_line $var

echo
exit 0

#====================================================3
读取输入 read
#====================================================
#! /bin/bash
filename:iftest
echo -n "Do you want to continue:Y or N "
read ANSWER
if [ $ANSWER = N -o $ANSWER = n ];
then
echo "NO"
else
echo " YES OR OTHERS "
fi

#====================================================4
读取输入 特殊字符$REPLY
#====================================================
#!/bin/bash
echo "----------REPLY--------"
START_TIME=$SECONDS

echo -n "What is your favorite vegetable?"
read
echo -n "Your favorite vegetable is $REPLY"

echo
echo -n "What is your favorite fruit?"
read fruit
echo -n "Your favorite fruit is $fruit"

echo -n "but value of \$REPLY is still $REPLY"

echo
echo "----------"
echo "$SECONDS"
echo "$SECONDS - $START_TIME"
exit 0

#====================================================5
限时输入
#====================================================
TMOUT=3
echo "you hava only 3 seconds to answer what is you favorite song?"
read song

if [ -z "$song" ]
then
 song="(no answer)"
fi

echo "Your favorite song is $song."

#====================================================6
测试RPM包可否安装 、将一个代码块的结果保存到文件
#====================================================
#!/bin/bash
#rpm-check.sh

# 这个脚本的目的是为了描述,列表,和确定是否可以安装一个rpm 包.
# 在一个文件中保存输出.
#
# 这个脚本使用一个代码块来展示

SUCCESS=0
E_NOARGS=65

if [ -z "$1" ]
then
echo "Usage:`basename $0` rmp-file"
exit $E_NOARGS
fi

{
echo
echo "Archive Description:"
rpm -qpi $1 # 查询说明
echo
echo "Archive Listing:"
rpm -qpl $1 # 查询列表
echo
rpm -i --test $1 # 查询rpm 包是否可以被安装
if [ "$?" -eq $SUCCESS ]
then
echo "$1 can be installed"
else
echo "$1 cannot be installed"
fi
echo
}>"$1.test"

echo "Results of rpm test in file $1.test"

exit 0

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics