概述
众所周知,shell脚本使用的是非交互式方式,在非交互式模式下alias扩展功能默认是关闭的,此时虽然可以定义alias别名,但是shell不会将alias别名扩展成对应的命令,而是将alias别名本身当作命令执行,如果shell内置命令和PATH中均没有与alias别名同名的命令,则shell会找不到指定的命令。
在shell中开启alias
使用shell内置命令shopt命令来开启alias扩展选项。shopt是shell的内置命令,可以控制shell功能选项的开启和关闭,从而控制shell的行为
常用命令:
Command | Desc |
---|---|
shopt -s opt_name | Enable (set) opt_name. |
shopt -u opt_name | Disable (unset) opt_name. |
shopt opt_name | Show current status of opt_name. |
比如
查看同义词是否开启(交互模式下)
[/xgj/gj]$shopt expand_aliases
expand_aliases on
shopt expand_aliases在交互式模式下 默认是打开的,在非交互式模式下是关闭的,但可以用可shopt来将其开启
shopt -s expand_aliases
实际操作
在我们的项目中某个模块的双机启动脚本(root用户下操作),其中应用的启停使用了alias建立的同义词来操作,如果想要在脚本中使用,必须开启同义词才。
项目启动中会依赖一些环境变量,所以双机启动脚本中需要显式的引入.bash_profile文件。
所以我们将 开启alias的命令放在 .bash_profile中。
source .bash_profile 生效。
脚本如下:
#!/bin/bash
##############################################
## Name: drm_script.ps
## Desc: Duplex switch script of DRM
## Param: None
## Author: Mr Yang
## Date: 2017-07-25
## Version:V1.0
## Comment: /drm/.bash_profile must set "shopt -s expand_aliases" so that the alias in the script works well
##############################################
#atcive the environment variable
source /drm/.bash_profile
#main menu of tomcat and jobserver
tomcat_jobserver_menu=/drm/drm
#main menu of uip and bp1
uip_bp1_menu=/drm/zsmart-uip-8.1.0/bin
#stauts of tomcat(webserver) pgrep -f "\-Dz_app=cvbs"
status_tomcat=$(pgrep -f Dz_app=cvbs | wc -l)
#status of jobserver
status_jobserver=$(pgrep -f Dz_app=jobserver | wc -l)
#status of uip
status_uip=$(pgrep -f Dz_app=uip | wc -l)
#status of bp1
status_bp1=$(pgrep -f Dz_app=bp1 | wc -l)
#pid of tomcat
pid_tomcat=$(pgrep -f Dz_app=cvbs)
#pid of jobserver
pid_jobserver=$(pgrep -f Dz_app=jobserver)
#pid of uip
pid_uip=$(pgrep -f Dz_app=uip)
#pid of bp1
pid_bp1=$(pgrep -f Dz_app=bp1)
#########################################
#
#start all processes including tomcat/jobserver/uip/bp1
#start tomcat/jobserver with drmtool
#start uip/bp1 with uip.sh
#
#########################################
startProcess(){
echo "stop first"
#there are some problems if you execute startProcess twice or more times,so stop process first
stopProcess
#start jobserver and tomcat
echo "begin to start [tomcat,jobserver]"
su - drm -c "cd $tomcat_jobserver_menu; jobstart ; start; exit" > /dev/null 2>&1
echo "sleep 5 seconds..."
sleep 5
echo "begin to start [uip,bp1]"
#start uip and bp1
su - drm -c "cd $uip_bp1_menu; startuip; startbp1; exit" > /dev/null 2>&1
}
#########################################
#
#stop all processes including tomcat/jobserver/uip/bp1
#stop tomcat/jobserver with drmtool
#stop uip/bp1 with uip.sh
#
#########################################
stopProcess(){
#stop tomcat、jobserver
echo "begin to stop process[tomcat,jobserver]"
su - drm -c "cd $tomcat_jobserver_menu ; stop ; jobstop ; exit" > /dev/null 2>&1
echo "process[tomcat,jobserver] stopped sucessfully,sleep 3 seconds"
sleep 3
#stop uip、bp1
echo "begin to stop process[uip,bp1]"
su - drm -c "cd $uip_bp1_menu && stopbp1 && stopuip && exit" > /dev/null 2>&1
echo process[uip,bp1] stopped sucessfully
}
#########################################
#
#print the status of each process
#
#########################################
status(){
#status of tomcat
check_status $status_tomcat tomcat
#status of jobserver
check_status $status_jobserver jobserver
#status of uip
check_status $status_uip uip
#status of bp1
check_status $status_bp1 bp1
}
#########################################
#
#public function
#checking status of each process
#
#########################################
check_status(){
if [ $1 -eq 1 ] ; then
echo "$2 is working..."
elif [ $1 -gt 1 ] ;then
echo -e " \033[31m more than one $2 process has been start,make sure only one process exists \033[0m "
else
echo -e " \033[31m $2 stopped \033[0m "
fi
}
#########################################
#
#stop all processes by force
#find the pid,and kill the pid
#
#########################################
forcedstop(){
#kill tomcat
kill_process $pid_tomcat cvbs
#kill jobserver
kill_process $pid_jobserver jobserver
#kill uip
kill_process $pid_uip uip
#kill bp1
kill_process $pid_bp1 bp1
}
#########################################
#
#public function
#kill each process
#
#########################################
kill_process(){
#check the length of $1 [zero:false,non-zero:true]
#two parameters [$1->pid,$2->processName],if $1 is null,$2 will replace $1,$2 turns to $1,so make sure $1 is number
if [[ -n $1 && $1 -gt 0 ]] ; then
echo "pid: $1,process: $2"
kill -9 $1
if [ $? == 0 ] ; then
echo "pid: $1,process: $2 has been stopped successfully"
sleep 1
fi
else
echo -e " \033[31mpgrep -f Dz_app=$1 without result,$1 has been stopped \033[0m "
fi
}
#########################################
#
# $1 variable
# $1 can only with (start/stop/status/restart/forcedstop)
#
#########################################
case "$1" in
'start')
startProcess
;;
'stop')
stopProcess
;;
'status')
status
;;
'restart')
#stopProcess
#sleep 3
startProcess
;;
'forcedstop')
forcedstop
;;
*)
echo -e "\033[31m Usage: $0 { start | stop | restart | forcedstop | status} \033[0m"
exit 1
esac
exit 0