eg1、让用户输入first name与last name,最后在屏幕上显示“Your full name is :"
#!/bin/bash
#Program:
# USER input his fist name and last name ,
# the program print his full name
#2011/12/13 Jason First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin/:/usr/local/bin:/usr/local/sbin/:~/bin
export PATH
read -p "Please input your first name:" firstname
read -p "Please input your last name:" lastname
echo "Your full name is $firstname $lastname !"
eg2、让用户输入文件名,程序做如下动作:
1、这个文件是否存在,若不存在则给予一个“Filename does not exist"的信息,并中断程序;
2、若这个文件存在,则判断它是个文件或记录,结果输出”Filename is regular file “ 或 "Filename is directory” ;
3、判断一下,执行者的身份对这个文件或者目录所拥有的权限,并输出权限数据。
#!/bin/bash
#Program:
# User input one filename, then the program judges it's a file or directory
# and also the program will tell the user what's popedom he has to the file or directory.
#2011/12/15 Jason First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin/:/usr/local/bin:/usr/local/sbin/:~/bin
export PATH
read -p "Please input the filename:" filename
test -z $filename && echo "You must input a filename!" && exit 0
test ! -e $filename && echo "This filename $filename dose not exist!" && exit 0
test -f $filename && filetype="regular file"
test -d $filename && filetype="directory"
test -r $filename && popedom="readonly"
test -w $filename && popedom="$popedom writable"
test -x $filename && popedom="$popedom executable"
echo "The $filename is $filetype , and you have the $popedom to the $filetype."
eg3、掷骰子小程序,可以让用户选择1~10,或者1~100的随机数范围
#!/bin/bash
#Program:
# Use this program you can get one random number,
# and also you can choose the number's bound.
# There are two bounds: 1~10 and 1~100
#2011/12/14 Jason First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin/:/usr/local/bin:/usr/local/sbin/:~/bin
export PATH
read -p "Please choose the number's bound , if you want the bound is 1~10 please input 10 , if you want 1~100 please input 100:" boundnumber
if [ $boundnumber==10 ] || [ $boundnumber==100 ];then
declare -i number=($RANDOM+1)*$boundnumber/32768
echo "OK , and your number is $number!"
else
echo "Please input 10 or 100!"
fi