Linux之shell编程学习
星期二 10 7月 2012
1、打印工作目录文件夹实例
for i in *
do
if [ -d "$i" ]
then
echo "$i"
fi
done
2、打印命令行参数
for arg in $@
do
echo "$arg"
done
3、打印用户信息
#! /bin/bash
if [ $# -eq 0 ]
then
echo "Useage:demo user1 user2..." 1>&2 #print error message to standard error
exit 1
fi
for user in $@
do
awk -F : '{print $1,$5}' /etc/passwd | grep -i "$user"
done
4.打印数字
#! /bin/bash
number=0
while [ $number -lt 10 ]
do
echo -n "$number"
((number += 1))
done
echo
5、控制终端
#! /bin/bash
trap '' 1 2 3 18
stty -echo
echo -n "Key: "
read key_1
echo
echo -n "Again: "
read key_2
echo
key_3=
if [ "$key_1" = "$key_2" ]
then
tput clear
until [ "$key_3" = "$key_2" ]
do
read key_3
done
else
echo "locktty:keys do not match " 1>&2
fi
stty echo
6、here文档
here文档允许把shell脚本的内容重定向为shell脚本的输入。