1 综合

[root@localhost script]# cat >nopwd

#/bin/bash
echo "no passwd user are :"
echo $(cat /etc/shadow | grep "!!" | awk 'BEGIN { FS=":" }{print $1}')

[root@localhost script]# bash nopwd
no passwd user are:
nscd vcsa rpc mailnull smmsp pcap ntp dbus avahi sshd rpcuser nfsnobody haldaemon avahi-autoipd oprofile xfs gdm sabayon postfix ldap mysql


2 echo

[root@localhost script]# cat >user

#!/bin/bash
echo "hello,$USER"
echo
echo "today 's date is `date`"
echo
echo "the user is :"
who
echo
echo "this is `uname -s`"
echo
echo "that's all folks!"

[root@localhost script]# bash user

Hello, root

Today 's date is Wed Jan  1 12:07:27 CST 2014

the user is :
root     :0           2013-12-27 18:04
root     pts/1        2013-12-27 18:04 (:0)
root     pts/2        2014-01-01 11:17 (192.168.1.109)

this is Linux

that's all folks!


3 case

[root@localhost script]# cat >yesno
#!/bin/bash
echo "enter [y/n]:"
read a
case $a in
  y|Y|yes|YES)
  echo "you enter $a"
  ;;
  n|N|no|NO)
  echo "you enter $a"
  ;;
  *)
  echo "error"
  ;;
esac

[root@localhost script]# bash yesno
enter [y/n]:

error
[root@localhost script]# bash yesno
enter [y/n]:
y
you enter y
[root@localhost script]# bash yesno
enter [y/n]:
n
you enter n

4 case

[root@localhost script]# cat >1-5
#!/bin/bash
echo "please enter a number from 1 to 5:"
read num
case $num in
 1) echo "you enter is 1" ;;
 2) echo "you enter is 2" ;;
 3) echo "you enter is 3" ;;
 4) echo "you enter is 4" ;;
 5) echo "you enter is 5" ;;
 *) echo "error" ;;
esac
[root@localhost script]# bash 1-5
please enter a number from 1 to 5:
4
you enter is 4
[root@localhost script]# bash 1-5
please enter a number from 1 to 5:
3
you enter is 3


5 read

[root@localhost script]# cat >read

#!/bin/bash
echo "please enter your first name and last name"
read first last
echo "your first name is $first"
echo "your last name is $last"
[root@localhost script]# bash read
please enter your first name and last name
feng
your first name is feng
your last name is
[root@localhost script]# bash read
please enter your first name and last name
feng yuan
your first name is feng
your last name is yuan


6 for

[root@localhost script]# cat >for
#!/bin/bash
f=1
for a in `seq 1 10`
do
f=`expr $f \* $a`
done
echo "10! = $f"
[root@localhost script]# bash for
10! = 3628800


7 while

[root@localhost script]# cat >while
#!/bin/bash
while [ "$var" != "end" ]
do
echo -n "please input a number:"
read var
if [ "$var" = "end" ]
then
break
fi
echo "var is $var"
done
[root@localhost script]# bash while
please input a number:3
var is 3
please input a number:2
var is 2
please input a number:32
var is 32
please input a number:end

8 login

[root@localhost script]# bash login
login:fj
passwd:ie
input is error
[root@localhost script]# bash login
login:what
passwd:who
the host and passwd is right
[root@localhost script]# cat login
#!/bin/bash
echo -n "login:"
read name
echo -n "passwd:"
read pwd
if [ $name = "what" -a $pwd = "who" ]
then
echo "the host and passwd is right"
else
echo "input is error"
fi


9 compare

[root@localhost script]# bash compare
please input two number
87
43
no.1 > no.2
[root@localhost script]# cat compare
#!/bin/bash
echo "please input two number"
read a
read b
if [ $a -eq $b ]
then echo "no.1 = no.2"
elif [ $a -gt $b ]
then echo "no.1 > no.2"
else echo "no.1 < no.2"
fi

10

要求输入字符必须只包含字母。如果不用函数实现这一点,要写大量脚本。使用函数可
以将重复脚本删去。这里用awk语言测试字符。以下是取得只有小写或大写字符的测试函数。

首先设置变量$1为一有意义的名字,然后用awk测试整个传送记录只包含字母,此命令输
出(1为非字母,空为成功)保存在变量_LETTERS_ONLY中。
然后执行变量测试,如果为空,则为成功,如果有值,则为错误。基于此项测试,返回
码然后被执行。在对脚本的函数调用部分进行测试时,使用返回值会使脚本清晰易懂。
使用i f语句格式测试函数功能

如果有错误,可编写一个函数将错误反馈到屏幕上。

函数name_error用于显示所有无效输入错误。使用特殊变量$@显示所有参数,这里为变
量FNAME和SNAME值。

注意每个输入的while循环,这将确保不断提示输入直至为正确值,然后跳出循环。当然,
实际脚本拥有允许用户退出循环的选项,可使用适当的游标,正像控制0长度域一样。
运行上述脚本的情况如下:

[root@localhost script]# cat fun-name
#!/bin/bash
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
}
name_error() {
echo "$@ contains errors,it must be 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
[root@localhost script]# bash fun-name
what is your first name :eie
what is your surname: lsls9
lsls9 contains errors,it must be contain only letters
what is your surname: kfj#
kfj# contains errors,it must be contain only letters
what is your surname: feifj