shell腳本實例

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章