shell实例(十一) ----循环,select结构
2011-03-09 23:58
357 查看
1.列出系统上所有用户
#! /bin/sh
PASSWORD_FILE=/etc/passwd
n=1
for name in $(awk 'BEGIN{FS=":"} {print $1}' < "$PASSWORD_FILE" )
do
echo "USER #$n = $name"
let "n += 1"
done
exit 0
2.在目录的所有文件中查找源字串
#! /bin/sh
directory=/usr/bin/
fstring="Free Software Foundation"
for file in $( find $directory -type f -name '*' | sort )
do
strings -f $file | grep "Sfstring" | sed -e "s%$directory%%"
done
exit 0
3.列出目录中所有有符号链接的文件
#! /bin/sh
directory=${l- `pwd`}
ARGS=1
if [ $# -ne "$ARGS" ]
then
directory=`pwd`
else
directory=$1
fi
echo "symbolic links in directory /"$directory/""
for file in "$( find $directory -type l )"
do
echo "$file"
done | sort
exit 0
4.多条件的while循环(以最后一个条件作为判断是否退出)
#! /bin/sh
var1=unset
previous=$var1
while echo "previous-variable = $previous"
echo
previous=$var1
[ "$var1" != "end" ]
do
echo "Input variable #1 (end to exit)"
read var1
echo "variable #1 = $var1"
done
exit 0
5.until循环
#! /bin/sh
END_CONDITION=end
until [ "$var1" = "$END_CONDITION" ]
do
echo "Input variable #1 "
echo "($END_CONDITION to exit)"
read var1
echo "variable #1 = $var1"
echo
done
exit 0
6.使用select来创建菜单
#! /bin/sh
PS3='Choose your favorite vegetable: '
echo
select vegetable in "beans" "carrots" "potatoes" "onions" "rutabagas"
do
echo
echo "Your favorite veggie is $vegetable."
echo "Yuck"
echo
break
done
exit 0
7.使用函数中select结构创建菜单
#! /bin/sh
PS3='Choose your favorite vegetable: '
echo
choice_of(){
select vegetable
do
echo
echo "You favorite veggie is $vegetable."
echo "Yuck!"
echo
break
done
}
choice_of beans rice carrots radishes tomatoes spinach
exit 0
#! /bin/sh
PASSWORD_FILE=/etc/passwd
n=1
for name in $(awk 'BEGIN{FS=":"} {print $1}' < "$PASSWORD_FILE" )
do
echo "USER #$n = $name"
let "n += 1"
done
exit 0
2.在目录的所有文件中查找源字串
#! /bin/sh
directory=/usr/bin/
fstring="Free Software Foundation"
for file in $( find $directory -type f -name '*' | sort )
do
strings -f $file | grep "Sfstring" | sed -e "s%$directory%%"
done
exit 0
3.列出目录中所有有符号链接的文件
#! /bin/sh
directory=${l- `pwd`}
ARGS=1
if [ $# -ne "$ARGS" ]
then
directory=`pwd`
else
directory=$1
fi
echo "symbolic links in directory /"$directory/""
for file in "$( find $directory -type l )"
do
echo "$file"
done | sort
exit 0
4.多条件的while循环(以最后一个条件作为判断是否退出)
#! /bin/sh
var1=unset
previous=$var1
while echo "previous-variable = $previous"
echo
previous=$var1
[ "$var1" != "end" ]
do
echo "Input variable #1 (end to exit)"
read var1
echo "variable #1 = $var1"
done
exit 0
5.until循环
#! /bin/sh
END_CONDITION=end
until [ "$var1" = "$END_CONDITION" ]
do
echo "Input variable #1 "
echo "($END_CONDITION to exit)"
read var1
echo "variable #1 = $var1"
echo
done
exit 0
6.使用select来创建菜单
#! /bin/sh
PS3='Choose your favorite vegetable: '
echo
select vegetable in "beans" "carrots" "potatoes" "onions" "rutabagas"
do
echo
echo "Your favorite veggie is $vegetable."
echo "Yuck"
echo
break
done
exit 0
7.使用函数中select结构创建菜单
#! /bin/sh
PS3='Choose your favorite vegetable: '
echo
choice_of(){
select vegetable
do
echo
echo "You favorite veggie is $vegetable."
echo "Yuck!"
echo
break
done
}
choice_of beans rice carrots radishes tomatoes spinach
exit 0
相关文章推荐
- shell实例(十) ----双圆括号结构与循环
- Shell 脚本之循环语句(for,while,until)以及case,select,break,continue实例详解
- shell判断结构和循环结构
- shell的编程结构体(函数、条件结构、循环结构)
- Shell脚本学习笔记-循环结构
- Shell中的循环语句for、while、until实例讲解
- shell循环结构之for循环
- thinkphp循环结构用法实例
- shell 里的选择结构、循环
- shell流程控制与循环结构
- 对Linux下shell编程之for循环的实例讲解
- C++编程实例-多重循环结构
- Shell中的循环语句实例
- shell中的while循环实例
- shell循环,判断介绍,以及实例
- shell循环结构之while循环
- 通过shell例子来学习循环结构的语法 推荐
- shell脚本编程之循环控制结构
- shell 编程 for 循环详解及应用实例
- shell中的循环结构