今晚,练习shell写的几个脚本。

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
hellojinjie
帖子: 1150
注册时间: 2007-09-14 21:03
来自: 浙江

今晚,练习shell写的几个脚本。

#1

帖子 hellojinjie » 2009-03-27 22:10

写的比较简单。 :em11 :em11

代码: 全选

#!/bin/sh
#file: lspids.sh
# import trigger_error
. /home/jj/scripts/trigger_error.sh

# Linux OR BSD ?
case `uname` in 
	Linux)	PSCMD="ps -ef" ;;
	*BSD)	PSCMD="ps -auwx" ;;
	*)		trigger_error "unknow system" "ERROR"
esac

HEADER=false
while getopts h OPTION
do
	case $OPTION in
		h)	HEADER=true; shift;;
		\?)	trigger_error "wrong option" "ERROR"
	esac
done

if [ -z $1 ];then
	trigger_error "you should at least give one argument" ERROR
else
	if [ "$HEADER" = true ];then
		$PSCMD | head -n 1
	fi
	$PSCMD | grep $1 | grep -v grep
fi

代码: 全选

#!/bin/sh
#file:	find_cmd.sh

find_cmd()
{
	FOUND=false
	if [ -z $1 ]; then 
		echo "ERROR"
		return 1
	fi
	OLDIFS="$IFS"
	IFS=:
	for i in $PATH
	do
		if [ -f "$i/$1" ]; then 
			echo "$i/$1"
			FOUND=true
		fi
	done
	
	# see if we have found the cmd
	if [ "$FOUND" = false ];then
		echo "$1 could not found!"
		return 1
	fi
	IFS=$OLDIFS
	return 0
}

代码: 全选

#!/bin/sh
#description:	handle error
#file:			trigger_error.sh

trigger_error()
{
	if [ $# -eq 1 ];then 
		echo $1
	elif [ $# -eq 2 ]; then
		case $2 in
			FATAL) echo "FATAL ERROR : $1"; exit 1;;
			ERROR) echo "ERROR : $1"; exit 1;;
			WARM) echo "WARMING : $1";;
			*) echo "$2 : $1";;
		esac
	else 
		echo $@
	fi
}

代码: 全选

#!/bin/sh
# $CMD_PATH is different from "$CMD_PATH"
# file:	lscmd

CMD_PATH=/bin/

for i in $CMD_PATH*
do
	whatis `basename $i`
done
Say hello to everyday!
jyz19880823
帖子: 91
注册时间: 2008-05-28 20:36

Re: 今晚,练习shell写的几个脚本。

#2

帖子 jyz19880823 » 2009-03-28 10:16

不错哦,继续努力
头像
hellojinjie
帖子: 1150
注册时间: 2007-09-14 21:03
来自: 浙江

Re: 今晚,练习shell写的几个脚本。

#3

帖子 hellojinjie » 2009-03-28 10:22

:em11
Say hello to everyday!
头像
sunningv
帖子: 1818
注册时间: 2006-12-17 13:34
来自: 武汉

Re: 今晚,练习shell写的几个脚本。

#4

帖子 sunningv » 2009-03-28 11:53

字数不少 就是不知道干吗用的
人生长恨水长东;
远近高低各不同.
头像
hellojinjie
帖子: 1150
注册时间: 2007-09-14 21:03
来自: 浙江

Re: 今晚,练习shell写的几个脚本。

#5

帖子 hellojinjie » 2009-03-28 21:26

sunningv 写了:字数不少 就是不知道干吗用的
无毒无害,运行一遍就知道做什么用的。 :em02
Say hello to everyday!
头像
city0011
帖子: 91
注册时间: 2009-03-10 22:17
联系:

Re: 今晚,练习shell写的几个脚本。

#6

帖子 city0011 » 2009-04-03 10:40

mark :em11
if u like
just do it
头像
lerosua
论坛版主
帖子: 8455
注册时间: 2007-11-29 9:41
联系:

Re: 今晚,练习shell写的几个脚本。

#7

帖子 lerosua » 2009-04-03 11:00

:em06 有空看看,再跑跑
回复