shell问一下这两行是什么意思

发布于 2021-11-28 05:04:07 字数 4826 浏览 813 评论 5

小弟刚开始学习Shell对语言的一些方法不是很懂 刚才看了朋友DBA的脚本 请问一下下面这几行是在做什么句法对吗,我怎么在手册里面找不到这几个命令那.....别嘲笑我 我是新手

#!/bin/bash
#set -x
PARSED_OPTIONS=$(getopt -n "$0"  -o s:e⭕ --long "rollback,rollout"  -- "$@")

eval set -- "$PARSED_OPTIONS"

if [ "$1" -ne "rollout" ]; then mkdir tmp 2> /dev/null

perl tokens.pl DBUpgradeTokens.properties tmp > /dev/null

全部脚本就是下面这些 好像是给客户提供数据库更新信息用的请高手帮我解释一下上面几行:

#!/bin/bash
#set -x
usage(){
[ $# = 0 ] && echo -e "nUsage: $0 -s <Start Build Number> -e <End Build Number> -o <Outfile Name> [ --rollout | --rollback ]n"
exit 1
}

[ $# -ne 7 ] && usage

# Execute getopt on the arguments passed to this program, identified by the special character $@
PARSED_OPTIONS=$(getopt -n "$0"  -o s:e⭕ --long "rollback,rollout"  -- "$@")
 
#Bad arguments, something has gone wrong with the getopt command.
if [ $? -ne 0 ]; # if last command is not equal  0
then
  exit 1
fi
 
# A little magic, necessary when using getopt.
eval set -- "$PARSED_OPTIONS"
 
 
# Now goes through all the options with a case and using shift to analyse 1 argument at a time.
#$1 identifies the first argument, and when we use shift we discard the first argument, so $2 becomes $1 and goes again through the case.
while true;
do
  case "$1" in
 
    -s)
        v_start=$2;
      shift 2;;
    -e)
        v_end=$2;
      shift 2;;
    -o)
        v_outfile=$2;
      shift 2;;
    --rollback)
	v_ind=rollback;
      shift;;
    --rollout)
	v_ind=rollout;
      shift;;
    --)
      shift
      break;;
  esac
done

#echo "v_ind = $v_ind"

echo "SET ECHO ON;" > $v_outfile.sql
echo "SET SERVEROUTPUT ON;" >> $v_outfile.sql
echo "SET TIMING ON;" >> $v_outfile.sql
echo "SET TIME ON;" >> $v_outfile.sql
echo "SPOOL ${v_outfile}.log APPEND;" >> $v_outfile.sql

mkdir tmp 2> /dev/null
rm tmp/*.sql 2> /dev/null

if [ "$v_ind" = "rollout" ]
then
	cp install/*.sql tmp/
	perl tokens.pl DBUpgradeTokens.properties tmp > /dev/null
	v_startAlter=$(awk -F":" -v var=$v_start '$2 ~ var { print $0; }' Upgrade.properties |head -1|awk -F ":" '{print $3}' |awk -F"=" '{print $1}' |  awk -F"." '{print $2}')
	v_endAlter=$(awk -F":" -v var=$v_end '$2 ~ var { print $0; }' Upgrade.properties |tail -1|awk -F ":" '{print $3}' |awk -F"=" '{print $1}' |  awk -F"." '{print $2}')
    	i=1;
    	for v_file in $(grep alter Upgrade.properties | col -bfx | awk -F"=" '{print $2}'|sed 's/install/tmp/g')
    	do
        	if [[ ( $i -ge $v_startAlter ) && ( $i -le $v_endAlter ) ]]
        	then
                	if [ -f $v_file ]
                	then
                        	head -3 $v_file >> $v_outfile.sql
                        	grep -i "call start_script" $v_file >> $v_outfile.sql
                        	awk '/BEGIN SCRIPT/,/END SCRIPT/' $v_file >> $v_outfile.sql
                        	grep -i "call end_script" $v_file >> $v_outfile.sql
                        	echo -e "commit;n" >> $v_outfile.sql
                	else
                        	echo -e "nFile $v_file does not exist!!!!!n"
                	fi
        	fi;
        	((i=i+1));
    	done
else
	cp rollback/*.sql tmp/
        perl tokens.pl DBUpgradeTokens.properties tmp > /dev/null

	v_startAlter=$(awk -F":" -v var=$v_start '$2 ~ var { print $0; }' Upgrade.properties |head -1|awk -F ":" '{print $3}' |awk -F"=" '{print $1}' |  awk -F"." '{print $2}')
	v_endAlter=$(awk -F":" -v var=$v_end '$2 ~ var { print $0; }' Upgrade.properties |tail -1|awk -F ":" '{print $3}' |awk -F"=" '{print $1}' |  awk -F"." '{print $2}')
    	i=$(grep alter Upgrade.properties |tail -1|awk -F ":" '{print $3}' |awk -F"=" '{print $1}' |  awk -F"." '{print $2}');
    	for v_file in $(grep alter Upgrade.properties | col -bfx | awk -F"=" '{print $2}'|sed 's/install/tmp/g'|cat -n|sort -nr|awk '{ print $2 }'|sed 's/(tmp/[0-9]*)/1_Rollback/g')
    	do
        	if [[ ( $i -ge $v_startAlter ) && ( $i -le $v_endAlter ) ]]
        	then
                	if [ -f $v_file ]
                	then
                        	head -3 $v_file >> $v_outfile.sql
                        	grep -i "call start_script" $v_file >> $v_outfile.sql
                        	awk '/BEGIN SCRIPT/,/END SCRIPT/' $v_file >> $v_outfile.sql
                        	grep -i "call end_script" $v_file >> $v_outfile.sql
                        	echo -e "commit;n" >> $v_outfile.sql
                	else
                        	echo -e "nFile $v_file does not exist!!!!!n"
                	fi
        	fi;
        	((i=i-1));
    	done
fi


echo -e "nSPOOL OFF;" >> $v_outfile.sql



如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

残花月 2021-11-30 05:03:31

这是两行么?数学没学好。

飘过的浮云 2021-11-30 04:00:41

好夸张的两行啊。。完全晕了

柳若烟 2021-11-29 23:39:53

我想问一下 那个perl那哪一行 是不是需要安装一个perl的解析器什么的

后知后觉 2021-11-29 19:16:59

回复
perl绝大多数发行版会自带。如果没有perl就自己装一个就行了

挽清梦 2021-11-28 17:54:51

完全没问题啊,没什么难理解的,找本bash的书好好看看,配合help $COMMAND命令查看bash的内建命令,就会了

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文