脚本简单地说就是一条条的文字命令,这些文字命令是可以看到的(如可以用记事本打开查看、编辑),脚本程序在执行时,是由系统的一个解释器,将其一条条的翻译成机器可识别的指令,并按程序顺序执行。因为脚本在执行时多了一道翻译的过程,所以它比二进制程序执行效率要稍低一些。
- #!/bin/bash
- #Name:compare
- #Description:compare the size of two numbers
- #Author:Linli
- #Version:0.0.1
- #Datatime:06/16/12 14:15:00
- #Usage: ./compare.sh NUM1 NUM2
- ##### scripts ########
- if [ $1 -le $2 ];then
- echo "$2 > $1"
- fi
- ##### scripts ########
- [[email protected] scripts]# ./compare.sh 2 3
- 3 > 2
- ##### scripts ########
- if [ $1 -le $2 ];then
- echo "$2 > $1"
- else
- echo "$2 <= $1"
- fi
- ##### scripts ########
- [[email protected] scripts]# ./compare.sh 3 1
- 1 <= 3
- ##### scripts ########
- if [ $1 -lt $2 ];then
- echo "$2 > $1"
- elif [$1 -gt $2 ]
- echo "$2 < $1"
- else
- echo "$2 = $1"
- fi
- ##### scripts ########
- [[email protected] scripts]# ./compare.sh 3 1
- 1 < 3
- [[email protected] scripts]# ./compare.sh 3 3
- 33 = 3
- #!/bin/bash
- #Name:menu
- #Description:case
- #Author:Linli
- #Version:0.0.1
- #Datatime:06/17/12 15:00:00
- #Usage: ./menu.sh
- ##### scripts ########
- read -p "Please key in an user name: " name
- cat << EOF
- U|u) show UID
- G|g) show GID
- S|s) show SHELL
- Q|q) quit
- EOF
- read -p "Please key in your choice: " choice
- case $choice in
- U|u)
- id -u $name
- ;;
- G|g)
- id -g $name
- ;;
- S|s)
- grep "^$name" /etc/passwd | cut -d: -f7
- ;;
- Q|q)
- echo "quit"
- exit 0
- ;;
- esac
- ##### scripts ########
- [[email protected] scripts]# ./menu.sh
- Please key in an user name: bin
- U|u) show UID
- G|g) show GID
- S|s) show SHELL
- Q|q) quit
- Please key in your choice: u
- 1
- #!/bin/bash
- #Name:user
- #Description:function
- #Author:Linli
- #Version:0.0.1
- #Datatime:06/17/12 15:40:00
- #Usage: ./user.sh
- ##### scripts ########
- myuser() {
- if `grep "$1" /etc/passwd &> /dev/null` ;then
- echo "shell: `grep "^$1" /etc/passwd | cut -d: -f7` UID:`id -u $1`"
- else
- echo "This user is not exist..."
- fi
- }
- read -p "Please key in an user name: " name
- until [ "$name" == "quit" ];do
- myuser $name
- read -p "You can continue key in an user name: " name
- done
- echo "quit"
- ##### scripts ########
- [[email protected] scripts]# ./user.sh
- Please key in an user name: root
- shell: /bin/bash UID:0
- You can continue key in an user name: bin
- shell: /sbin/nologin UID:1
- You can continue key in an user name: gentoo
- shell: /bin/bash UID:502
- You can continue key in an user name: quit
- quit
- [[email protected] scripts]#
- #!/bin/bash
- #Name:sum
- #Description:while
- #Author:Linli
- #Version:0.0.1
- #Datatime:06/17/12 16:10:00
- #Usage: ./sum.sh
- ##### scripts ########
- declare -i sum=0
- declare -i I=1
- while [ $I -le 100 ];do
- sum+=$I
- let I++
- done
- echo "1+2+3+..+100=$sum"
- ##### scripts ########
- [[email protected] scripts]# ./sum.sh
- 1+2+3+..+100=5050
- #!/bin/bash
- #Name:disk
- #Description:until
- #Author:Linli
- #Version:0.0.1
- #Datatime:06/17/12 16:55:00
- #Usage: ./disk.sh
- ##### scripts ########
- read -p "Please key in a disk partition: " disk
- until [ "$disk" == "q" -o "$disk" == "Q" ];do
- fdisk -l $disk
- done
- echo "quit"
- ##### scripts ########
- [[email protected] scripts]# ./disk.sh
- Please key in a disk partition: /dev/sda
- Disk /dev/sda: 343.5 GB, 343597383680 bytes
- 255 heads, 63 sectors/track, 41773 cylinders
- Units = cylinders of 16065 * 512 = 8225280 bytes
- Device Boot Start End Blocks Id System
- /dev/sda1 * 1 13 104391 83 Linux
- /dev/sda2 14 5235 41945715 8e Linux LVM
- /dev/sda3 5236 5366 1052257+ 82 Linux swap / Solaris
- /dev/sda4 5367 41773 292439227+ 5 Extended
- /dev/sda5 5367 5975 4891761 fd Linux raid autodetect
- /dev/sda6 5976 6584 4891761 fd Linux raid autodetect
- /dev/sda7 6585 7193 4891761 fd Linux raid autodetect
- /dev/sda8 7194 7802 4891761 fd Linux raid autodetect
- /dev/sda9 7803 9019 9775521 8e Linux LVM
- /dev/sda10 9020 10236 9775521 8e Linux LVM
- /dev/sda11 10237 10480 1959898+ 83 Linux
- Please key in a disk partition: q
- quit
- #!/bin/bash
- #Name:menu
- #Description:case
- #Author:Linli
- #Version:0.0.1
- #Datatime:06/17/12 17:30:00
- #Usage: ./menu.sh
- ##### scripts ########
- read -p "Please key in an user name: " name
- cat << EOF
- U|u) show UID
- G|g) show GID
- S|s) show SHELL
- Q|q) quit
- EOF
- while true;do
- read -p "Please key in your choice: " choice
- case $choice in
- U|u)
- id -u $name
- ;;
- G|g)
- id -g $name
- ;;
- S|s)
- grep "^$name" /etc/passwd | cut -d: -f7
- ;;
- Q|q)
- echo "quit"
- exit 0
- ;;
- ?)
- echo "Wrong choice"
- continue
- ;;
- esac
- done
- ##### scripts ########
- [[email protected] scripts]# ./menu.sh
- bash menu2.sh
- Please key in an user name: root
- U|u) show UID
- G|g) show GID
- S|s) show SHELL
- Q|q) quit
- Please key in your choice: u
- 0
- Please key in your choice: t
- Wrong choice
- Please key in your choice: s
- /bin/bash
- Please key in your choice: g
- 0
- Please key in your choice: q
- quit
- #!/bin/bash
- #Name:read
- #Description:while read LINE
- #Author:Linli
- #Version:0.0.1
- #Datatime:06/17/12 17:30:00
- #Usage: ./read.sh
- ##### scripts ########
- while read LINE;do
- echo "$LINE"
- echo "OK"
- done < /etc/passwd
- ##### scripts ########
- [[email protected] scripts]# ./line.sh
- root:x:0:0:root:/root:/bin/bash
- OK
- bin:x:1:1:bin:/bin:/sbin/nologin
- OK
- daemon:x:2:2:daemon:/sbin:/sbin/nologin
- OK
- adm:x:3:4:adm:/var/adm:/sbin/nologin
- OK
- ... [显示的其它内容省略了]
- #!/bin/bash
- #Name:shell
- #Description:for
- #Author:Linli
- #Version:0.0.1
- #Datatime:06/17/12 17:20:00
- #Usage: ./shell.sh
- ##### scripts ########
- for I in `cut -d: -f1 /etc/passwd`;do
- echo "$I : `grep "^$I" /etc/passwd | cut -d: -f7`"
- done
- ##### scripts ########
- [[email protected] scripts]# ./oddsum.sh
- root : /bin/bash
- bin : /sbin/nologin
- ...[其他显示内容省略了]
来自http://linlilinux.blog.51cto.com/4373510/910508