bash编程入门 |
发布时间: 2012/9/15 18:03:46 |
1. ping 主机
#!/bin/bash
HOSTNAME=$1 #注意位置参数的使用 if ping -c2 $HOSTNAME;then
echo "The $HOSTNAME is online."
else
echo "The $HOSTNAME is down."
fi
==================================================================
2.找文件—》找到就备份—》没找到就显示其他信息
#!/bin/bash
DIR=$1
BACKUP=$2
#destination of the backup.
if [ ! -d $2 ] ;then
echo "The directory does not exist!"
elif [ $# -eq 2 ];then
echo "$DIR is backuped to $BACKUP... "
else
echo -e "Usage: $0 dir backupfile\n for example:$0 /etc/passwd /tmp/bash"
fi
==================================================================
3.找用户—>并统计行数—>
#!/bin/bash
USERNAME=$1
RES=$(cat /etc/passwd | grep $USERNAME | wc -l)
if [ "$RES" == "1" ] ;then
echo "The user you want to find is $USERNAME."
elif [ $RES -eq 0 ];then
echo "The user you want to find doesnot exist."
else
echo "Some other errors happen"
fi
==================================================================
4.查看文件是否存在并且可写
#!/bin/bash
if [ -f $1 -a -w $1 ];then
echo "The file is writable."
else
echo "This a directory."
fi
==================================================================
5.简单的if-else嵌套
#!/bin/bash
HOST=$1
if ping -c2 $HOST &> /dev/null ;then
echo "$HOST is online. "
else
echo "$HOST is down."
fi
==================================================================
6.第5个程序可改写如下:
#!/bin/bash
HOST=$1
ping -c2 $HOST &> /dev/null && echo "$HOST is online " || echo "$HOST is down"
==================================================================
7.查看文件夹是否可写可执行
#!/bin/bash
DIR=$1
[ -r $DIR ] && echo "The file is readable" || echo "The directory is not readable"
[ -w $DIR ] && echo "The file is writable" || echo "The file is not writable"
[ -x $DIR ] && echo "The file is excutable" || echo "The file is not excutable"
==================================================================
8.简单的菜单选项设置
#!/bin/bash
echo "======================="
echo -e "\t\t1.Linux"
echo -e "\t\t2.Unix"
echo -e "\t\t3.Windows"
echo "======================="
read NUMBER
if [ $NUMBER -eq 1 ];then
echo "You have chosen RedHad Linux 6.0"
elif [ $NUMBER -eq 2 ];then
echo "You have chosen IBM AIX"
elif [ $NUMBER -eq 3 ];then
echo "You have chosen Windows!"
else
echo "You have chosen a wrong number!"
fi
==================================================================
9.简单退出值的设定
#!/bin/bash
FILENAME=$1
if [ -d $FILENAME ];then
echo "this is a directory!"
exit 10
elif [ ! -w $FILENAME ];then
exit 20
else
exit 1
fi
==================================================================
10.case的用法(模拟服务的启动)
把写好的脚本加入到 /etc/rc.d/init.d并注意加上着色几行,并用命令
Chkconfig —add SERVICENAME
#!/bin/bash
# chkconfig: 235 90 12
# description:
if [ $# -lt 1 ];then
echo "Usages: services $1 {start|stop|restart|status}"
else
case "$2" in
start)
echo "The service $1 is starting"
;;
stop)
echo "The service $1 is stopping"
;;
restart)
echo -e "The servie $1 is stopping\nThe service is starting"
;;
status)
echo "The service $1's status is..."
;;
esac
fi
==================================================================
11.用source和bash调用程序的时候,返回值不同。
○ 1
#!/bin/bash
bash ./b.sh
RES=$?
echo "The $0 pid is $$"
case "$RES" in
10)
echo "Network problem"
;;
20)
echo "Directory does not exist"
;;
esac
○ 2
#!/bin/bash
echo " The $0 pid is $$"
exit 10
==================================================================
12.for循环中的变量字符集
#!/bin/bash
for HOST in 172.24.254.254 172.24.254.200 172.24.123.123
do
ping -c2 $HOST &> /dev/null && echo "$HOST is online" \
|| echo "$HOST is down"
done
==================================================================
13.for循环变量字符集
#!/bin/bash
for HOST in 172.24.0.{1..10}
do
ping -c2 $HOST &> /dev/null && echo "$HOST is online" \
|| echo "$HOST is down"
Done
==================================================================
14.for循环变量文件集
#!/bin/bash
for FILE in /etc/*.conf
do
cp $FILE /tmp/tmp
done
#!/bin/bash
DEST=/var/tmp
for FILENAME in /lib64/*.* /etc/*.conf
do
cp $FILENAME $DEST &> /dev/null
logger "backup $FILENAME now..."
echo "Backup $FILENAME TO $DEST" >> ~/backup.log
done
==================================================================
15.for循环指令集
#!/bin/bash
for USERINFO in $(cat /etc/passwd | grep bash$ | cut -d: \
-f1,3)
do
_USENAME=$(echo $USERINFO | cut -d: -f 1)
_UID=$(echo $USERINFO | cut -d: -f 2)
if [ $_UID -lt 500 -a $_UID -ne 0 ];then
logger "$_USERNAME is a system user ,but can \
log now"
fi
done
==================================================================
16.输出9*9乘法表
#!/bin/bash
for NUM1 in {1..9}
do
for NUM2 in {1..9}
do
RES=$((NUM1 * NUM2))
echo -en "$NUM1 * $NUM2 = $RES\t"
done
echo
done
==================================================================
17.输出9*9乘法表
#!/bin/bash
for ((i=1;i<10;i++))
do
for ((p=1;p<=i;p++))
do
RES=$(($p * $i))
echo -en "$p * $i = $RES\t"
done
echo
done
echo
==================================================================
18.输出黑白象棋
#!/bin/bash
for HANG in {1..9}
do
for LIE in {1..9}
do
RES=$((HANG + LIE))
if [ $(($RES % 2)) -eq 0 ];then
echo -en "\033[47m \033[0m"
else
echo -en "\033[40m \033[0m"
fi
done
echo
done
==================================================================
19.输出黑白象棋
#!/bin/bash
for HANG in {1..9}
do
for LIE in {1..9}
do
RES=$((HANG + LIE))
if ((($RES % 2) == 0)) ;then
echo -en "\033[47m \033[0m"
else
echo -en "\033[40m \033[0m"
fi
done
echo
done
==================================================================
20.输出数字形状
#!/bin/bash
echo "Please type your number:"
read a
for ((i=1;i<=a;i++))
do
for ((p=1;p<=i;p++))
do
echo -n "$p"
done
echo
done
echo
==================================================================
21.计算器(加减乘除)
echo "..............x"
echo "............../"
echo "..............q"
echo "Please type your word:(e.g.1 + 2)"
read a b c
do
case $b in
+) echo " $a + $c =" `expr $a + $c`;;
-) echo " $a - $c =" `expr $a - $c`;;
x) echo " $a x $c =" `expr $a \* $c`;;
/) echo " $a / $c =" `expr $a \/ $c`;;
esac
case $a in
q) break ;;
esac
done
==================================================================
22.输出直角三角形
#!/bin/bash
echo "Please type a number:"
read num
for ((i=1;i<num;i++))
do
for ((j=0;j<num-i;j++))
do
echo -n ""
done
for ((j=0;j<2*i-1;j++))
do
echo -n "*"
done
==================================================================
23.输出翻转三角形
#!/bin/bash
echo "Please type a number:"
read num
for ((i=1;i<num;i++))
do
for ((j=0;j<num-i;j++))
do
echo -n ""
done
for ((j=0;j<2*i-1;j++))
do
echo -n "*"
done
echo ""
done
for ((i=1;i<=num;i++))
do
for ((j=0;j<i;j++))
do
echo -n ""
done
for ((j=0;j<2*(num-i)-1;j++))
do
echo -n "*"
done
echo ""
done
==================================================================
24.菜单的设定
!/bin/bash
while [[ "$CHOOSE" != "q" && "$CHOOSE" != "Q" ]]
do
clear
echo "1.Linux"
echo "2.Unix"
echo "q.quit"
read CHOOSE
case $CHOOSE in
1)
echo "You have chosen Linux"
exit 10
;;
2)
echo "You have chosen Uinx"
exit 20
;;
esac
done
==================================================================
25.简单函数,函数可接受位置参数,此函数名字为24.sh
#!/bin/bash
clean_tmp_file()
{
echo "clean $1..."
}
#$1 is a dabase name and $2 is a folder
backup_database()
{
echo "backup database $1 to $2"
}
check_file_md5()
{
echo "check $1 sum md5 value"
}
clean_tmp_file /tmp/empty
==================================================================
26.调用函数库中的函数
#!/bin/bash
source ./24.sh
clean_tmp_file /etc/passwd
check_file_md5 /etc/group
backup_database /mys
==================================================================
27.路径的设置
#!/bin/bash
setPath ()
{
PATH=/bin:/usr/bin
if [ $UID -eq 0 ];then #如果是root用户
PATH=$PATH:/usr/bin/sbin:/sbin #路径追加sbin的目录
fi
if (($2 == "after"));then #根据位置参数2,决定如何添加新路径
PATH=$PATH:$1 #在原PATH后面追加
else
PATH=$1:$PATH #在原PATH之前添加
fi
}
本文出自:亿恩科技【www.enkj.com】 |