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 mysql2 echo
[root@localhost script]# cat >user
#!/bin/bash
echo "hello,$USER"echoecho "today 's date is `date`"echoecho "the user is :"whoechoecho "this is `uname -s`"echoecho "that's all folks!"[root@localhost script]# bash user
Hello, root
Today 's date is Wed Jan 1 12:07:27 CST 2014the user is :root :0 2013-12-27 18:04root pts/1 2013-12-27 18:04 (:0)root pts/2 2014-01-01 11:17 (192.168.1.109)this is Linuxthat's all folks!3 case
[root@localhost script]# cat >yesno
#!/bin/bashecho "enter [y/n]:"read acase $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 yesnoenter [y/n]:yyou enter y[root@localhost script]# bash yesnoenter [y/n]:nyou enter n4 case
[root@localhost script]# cat >1-5
#!/bin/bashecho "please enter a number from 1 to 5:"read numcase $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-5please enter a number from 1 to 5:4you enter is 4[root@localhost script]# bash 1-5please enter a number from 1 to 5:3you enter is 35 read
[root@localhost script]# cat >read
#!/bin/bash
echo "please enter your first name and last name"read first lastecho "your first name is $first"echo "your last name is $last"[root@localhost script]# bash readplease enter your first name and last namefengyour first name is fengyour last name is[root@localhost script]# bash readplease enter your first name and last namefeng yuanyour first name is fengyour last name is yuan6 for
[root@localhost script]# cat >for
#!/bin/bashf=1for a in `seq 1 10` do f=`expr $f \* $a` doneecho "10! = $f"[root@localhost script]# bash for10! = 36288007 while
[root@localhost script]# cat >while
#!/bin/bashwhile [ "$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 whileplease input a number:3var is 3please input a number:2var is 2please input a number:32var is 32please input a number:end8 login
[root@localhost script]# bash login
login:fjpasswd:ieinput is error[root@localhost script]# bash loginlogin:whatpasswd:whothe host and passwd is right[root@localhost script]# cat login#!/bin/bashecho -n "login:"read nameecho -n "passwd:"read pwdif [ $name = "what" -a $pwd = "who" ]thenecho "the host and passwd is right"elseecho "input is error"fi9 compare
[root@localhost script]# bash compare
please input two number8743no.1 > no.2[root@localhost script]# cat compare#!/bin/bashecho "please input two number"read aread bif [ $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"fi10要求输入字符必须只包含字母。如果不用函数实现这一点,要写大量脚本。使用函数可
以将重复脚本删去。这里用awk语言测试字符。以下是取得只有小写或大写字符的测试函数。首先设置变量$1为一有意义的名字,然后用awk测试整个传送记录只包含字母,此命令输
出(1为非字母,空为成功)保存在变量_LETTERS_ONLY中。 然后执行变量测试,如果为空,则为成功,如果有值,则为错误。基于此项测试,返回 码然后被执行。在对脚本的函数调用部分进行测试时,使用返回值会使脚本清晰易懂。 使用i f语句格式测试函数功能如果有错误,可编写一个函数将错误反馈到屏幕上。
函数name_error用于显示所有无效输入错误。使用特殊变量$@显示所有参数,这里为变
量FNAME和SNAME值。注意每个输入的while循环,这将确保不断提示输入直至为正确值,然后跳出循环。当然,
实际脚本拥有允许用户退出循环的选项,可使用适当的游标,正像控制0长度域一样。 运行上述脚本的情况如下:[root@localhost script]# cat fun-name
#!/bin/bashchar_name() { _LETTERS_ONLY=$1_LETTERS_ONLY=`echo $1|awk '{if($0~/[^a-zA-Z]/) print "1"}'`if [ "$_LETTERS_ONLY" != "" ]then return 1else return 0fi}name_error() { echo "$@ contains errors,it must be contain only letters"}while :
doecho -n "what is your first name :"read F_NAMEif char_name $F_NAMEthen breakelse name_error $F_NAMEfidonewhile :doecho -n "what is your surname: "read S_NAMEif char_name $S_NAMEthen breakelse name_error $S_NAMEfidone[root@localhost script]# bash fun-namewhat is your first name :eiewhat is your surname: lsls9lsls9 contains errors,it must be contain only letterswhat is your surname: kfj#kfj# contains errors,it must be contain only letterswhat is your surname: feifj