1.在脚本中定义函数
函数有两部分组成:函数名,函数体
2.建立和执行函数
function findit ()
{
if [ $# -lt 1 ]; then
echo "Uage : findit file";
return 1;
fi;
for loop in "$@";
do
find /usr/local/yjg $loop -print;
done
}
[root@localhost yjg]# findit
Uage : findit file
[root@localhost yjg]# findit fun1.txt
/usr/local/yjg
/usr/local/yjg/fun1.txt
/usr/local/yjg/fun2.txt
fun1.txt
[root@localhost yjg]#
3.函数举例
#!/bin/sh
#fun2.txt
function char_name(){
_LETTERS_ONLY=$1
_LETTERS_ONLY=` echo $1|awk '{if($0~/[^a-zA-Z]/) print "1"}'`
if [ "$_LETTERS_ONLY" != "" ]; then
return 1
else
return 0
fi
}
function name_error(){
echo " $@ contains errors,it must contain only letters"
}
while :
do
echo -n "what is your first name:"
read F_NAME
if char_name $F_NAME; then
break
else
name_error $F_NAME
fi
done
while :
do
echo -n "what is your surname:"
read S_NAME
if char_name $S_NAME; then
break
else
name_error $S_NAME
fi
done
~
~
~
~
"fun2.sh" 38L, 563C written
[root@localhost yjg]# ./fun2.sh
what is your first name:123
123
123 contains errors,it must contain only letters
what is your first name:abc
abc
what is your surname:321
321
321 contains errors,it must contain only letters
what is your surname:cde
cde
[root@localhost yjg]#
选择单个字符
read_a_char ()
{
SAVEDSTTY=`stty -g`;
stty cbreak;
dd if=/dev/tty bs=1 count=1 2> /dev/null;
stty cbreak;
stty=$SAVESTTY
}
判断是否是文件夹
[root@localhost yjg]# is_a_directory(){
> if [ $# -lt 1 ]; then
> echo "is_it_a_directory: I need an argument"
> return 1
> fi
> _DIRECTORY_NAME=$1
> if [ ! -d $_DIRECTORY_NAME ]; then
> return 1
> else
> return 0
> fi
> }
提示回答
#!/bin/sh
#continue_prompt.fun
echo "Enter into fun"
function prompt(){
#to call: continue_prompt "string to display" default answer
STR=$1
echo $STR
DEFAULT=$2
echo $DEFAULT
if [ $# -lt 1 ]; then
echo "continue prompt: I need a string to display"
return 1
fi
while :
do
echo -n " $_STR [Y..N] [$_DEFAULT]:"
read ANS
case $ANS in
Y) return 0
;;
N) return 1
;;
esac
case $ANS in
y|Y|Yes|YES)
return 0
;;
n|N|No|NO)
return 1
;;
*) echo "Answer either Y or N,default is $_DEFAULT "
;;
esac
echo $ANS
done
}
从登录ID中,抽取信息
#!/bin/sh
#whois.fun
function whois(){
if [ $# -lt 1 ]; then
echo "whois : need user id's please"
return 1
fi
for loop
do
USER_NAME=` grep $loop /etc/passwd | awk -F: '{print $4}' `
if [ "$USER_NAME" = "" ]; then
echo "whois: Sorry cannot find $loop"
else
echo "$loop is $USER_NAME"
fi
done
}
whois dave yjg superman
列出文件行号
#!/bin/sh
#number_file.fun
function filen(){
FILENAME=$1
if [ $# -ne 1 ]; then
echo "number_file: I need a filename to number:"
return 1
fi
loop=1
while read LINE
do
echo "$loop: $LINE"
loop=` expr $loop + 1 `
done < $FILENAME
}
filen $1
小写转大写
#!/bin/sh
#str_to_upper.fun
function str_to_upper(){
STR=$1
if [ $# -ne 1 ]; then
echo "number_file: I need a string to convert please"
return 1
fi
echo $@ | tr '[a-z]' '[A-Z]'
}
str_to_upper $1
字符串长度
#!/bin/sh
#check_length.fun
function check_len(){
STR=$1
MAX=$2
if [ $# -ne 2 ]; then
echo "check_length: I need a string and max length the string should be"
return 1
fi
LENGTH=` echo $STR | awk '{print length($0)}' `
if [ "$LENGTH" -gt "$MAX" ]; then
return 1
else
return 0
fi
}
check_len $1 $2
联系客服