function usage(){
echo "Usage Example: ./install_start.sh -d /opt/modules/daemon -s 2012 -i 192.168.0.1 -p 8080"
echo "-d install directory path";
echo "-s daemon boot start port";
echo "-i cronhub center server's ip is used for download daemon's jar and jdk and jsvc and so on"
echo "-p cronhub center server's port used for download daemon's jar and jdk and jsvc and so on"
}
if [ $# -ne 8 ];#如果参数没有8个,算上-s啥的,那么就从用户输入读入
then
echo "please input the directory path that you want to install";
read install_path;
echo "please input the cronhub center server's ip..."
read center_server_ip;
echo "please input the cronhub center server's port..."
read center_server_port;
echo "please input the daemon boot start port..."
read daemon_port;
:<<BLOCK
usage
BLOCK
exit -1
else
whilegetopts":d:i:h:p:s:" flag #传入参数,第一个冒号表示,后面的每个比如s后的冒号表示传入-s 2012,后面带参数
如果选项字符串不以:(冒号)开头,名称 将会被设置为 ?(问号)字符,OPTARG. 将被取消设置,并且诊断消息将被写入到标准错误中。
如果选项字符串以 :(冒号)开头,名称 将被设为 ? (问号)字符,这是对未知的选项来说的,或者为缺少的所需选项设为:(冒号)字符,OPTARG 将被设置为已查找到的选项字符,并且 标准错误中将不写入任何输出。
invalid option时,varname会被设成?,$OPTARG是出问题的option;
miss option argument时,varname会被设成:,$OPTARG是出问题的option。
如果optstring不以”:“开头,invalid option错误和miss option argument错误都会使
varname被设成?,$OPTARG是出问题的option。
细则2:当optstring中的字母跟”:“时,表明该option可接参数,参数(argument)放在$OPTARG中;
如果缺参数,且optstring是以”:“开头,则varname的值会是:,$OPTARG是该option,
否则varname的值是?,$OPTARG是该option。(参照细则1)
do
case $flag in
d)
echo "install directory path: "$OPTARG
install_path=$OPTARG
;;
i)
echo "cronhub center server's ip: "$OPTARG
center_server_ip=$OPTARG
;;
p)
echo "cronhub center server's port: "$OPTARG
center_server_port=$OPTARG
;;
s)
echo "daemon boot start port's port: "$OPTARG
daemon_port=$OPTARG
;;
?) usage
;;
esac
done
fi
mkdir -p $install_path
cd $install_path;
jdk_bin="jdk.bin";
jdk_bin_path=$install_path"/"$jdk_bin;
java_home=$install_path"/jdk1.6.0_30";
base_url="http://${center_server_ip}:${center_server_port}/download/";
jdk_download_url=$base_url"jdk-6u30-linux-x64.bin"
jsvc_zip="jsvc.zip";
jsvc_dir="jsvc";
jsvc_url=$base_url$jsvc_zip;
jar_name="DispatchSystemDaemon.jar";
daemon_url=$base_url$jar_name;
daemon_jar_path=$install_path"/"$jar_name;
jsvc_dir=$install_path"/"$jsvc_dir;
jsvc_target_bin=$jsvc_dir"/jsvc";
#install jdk
if [ ! -e $jdk_bin_path ];
then
wget $jdk_download_url -O $jdk_bin_path;
chmod 755 $jdk_bin_path;
$jdk_bin_path;
fi;
#install jsvc
if [ ! -e $install_path"/"$jsvc_zip ];
then
wget $jsvc_url;
fi
if [[ -e $install_path"/"$jsvc_zip && ! -e $jsvc_target_bin ]];
then
unzip $jsvc_zip;
fi;
if [ ! -e $jsvc_target_bin ];
then
cd $jsvc_dir;
chmod +x configure;
chmod +x make;
./configure --with-java=$java_home;
make;
cd $install_path;
fi;
#install daemon jar
if [ ! -e $daemon_jar_path ];
then
wget $daemon_url;
fi;
ps_num=`ps -ef|grep $jar_name|grep $daemon_port|grep -v grep|grep -v sh|wc -l`;
if [ $ps_num -gt 0 ];
then
kill_pid=`ps -ef|grep $jar_name|grep $daemon_port|grep -v grep|grep -v sh|awk -F ' ' '{print $2}'`;
echo "kill "$kill_pid" .process numer is "$ps_num;
kill $kill_pid;
echo "killing service to reboot service...";
sleep 2;
fi;
#add to system service
cmd="$jsvc_target_bin -home $java_home -Xmx2000m -pidfile $install_path/$daemon_port.pid -cp $daemon_jar_path com.baofeng.dispatchexecutor.boot.DaemonBoot -p $daemon_port";
echo -e "#description:cronhub_daemon
#chkconfig:231 80 80
case \"\$1\" in
start)
\t${install_path}/jsvc/jsvc -home ${install_path}/jdk1.6.0_30 -Xmx2000m -pidfile ${install_path}/$daemon_port.pid -cp ${install_path}/DispatchSystemDaemon.jar com.baofeng.dispatchexecutor.boot.DaemonBoot -p $daemon_port
\t;;
esac
" > /etc/init.d/cronhub_daemon
/sbin/chkconfig --add cronhub_daemon
#start daemon
echo "adding to service done,now start daemon service..."
chmod +x /etc/init.d/cronhub_daemon
/sbin/service cronhub_daemon start
echo "all done"
#the final start cmd
#$jsvc_target_bin -home $java_home -Xmx2000m -pidfile $install_path"/"$daemon_port".pid" -cp $daemon_jar_path com.baofeng.dispatchexecutor.boot.DaemonBoot -p $daemon_port
【转载】shell的getopts范例
getopts对命令行中给出的选项进行解释的步骤如下。
1.getopts检查所有的命令行参数,查找以-为字符开头的参数。
2.当发现一个以-字符开头的参数时,它比较-之后紧接着的字符与给定的option-string中所有字符。
3.如果发现该字符在option-string中有匹配,则把该选项字符赋予变量var;否则,var的值设置为?字符。
4.重复步骤1~3,直到命令行中给出的所有选项都被检查完。
5.当选项解析完成后,getopts返回一个非0的退出码--这使得getopts很容易地被使用在循环中。另外,当getopts结束时,它被变量OPTION的值设置为最后一个参数的下标。
getopts的另一个特点是它能够指出哪些选项要求带参数,这是通过在选项后面附加一个冒号(:)字符来实现的。在这种情况下,解析一个选项后,附带的参数就会设置为变量OPTARG的值。
最近在学shell,刚看到getopts,搜到这个贴。上面getopts的用法来自<精通shell编程>这本书。对着书看上面的脚本。很容易看懂
# ftpuseradd
usage()
{
echo "Usage: `basename $0` [-w] [-m maxrate] username password"
exit 1
}
WRITABLE="no" # can't upload
MAXRATE="64000" # default max rate is 64Kb
OPT="no"
while getopts :wm: OPTION
do
case $OPTION in
w) WRITABLE="yes"
OPT="yes"
;;
m) MAXRATE=$OPTARG
OPT="yes"
;;
\?) usage
;;
esac
done
shift `expr $OPTIND - 1`
if [ $# -ne 2 ]
then
usage
fi
USERNAME=$1
PASSWORD=$2
useradd -d /dev/null -g ftp -s /bin/false $USERNAME
if [ $? -eq 0 ]
then
setpasswd.exp $USERNAME $PASSWORD > /dev/null
echo "anon_world_readable_only=NO" > /etc/vsftpd/$USERNAME
echo "anon_max_rate=$MAXRATE" >> /etc/vsftpd/$USERNAME
if [ "$WRITABLE" = "yes" ]
then
echo "write_enable=YES" >> /etc/vsftpd/$USERNAME
echo "anon_upload_enable=YES" >> /etc/vsftpd/$USERNAME
echo "anon_mkdir_write_enable=YES" >> /etc/vsftpd/$USERNAME
echo "anon_other_write_enable=YES" >> /etc/vsftpd/$USERNAME
fi
echo "$USERNAME" >> /etc/vsftpd.user_list