Sam's Notes | Sam Blog

梦想还是要有的,万一实现了呢

0%

Spring MVC 4.2.1.RELEASE , 容器 tomcat 8.21, nginx : 客户端在服务端响应前就关闭(socket),比如下载文件一半关闭浏览器,
服务端会有如下异常, 该异常服务器从业务上不需要处理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
org.apache.catalina.connector.ClientAbortException: java.io.IOException: 断开的管道
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:393) ~[catalina.jar:8.0.18]
at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:426) ~[tomcat-util.jar:8.0.18]
at org.apache.catalina.connector.OutputBuffer.doFlush(OutputBuffer.java:342) ~[catalina.jar:8.0.18]
at org.apache.catalina.connector.OutputBuffer.flush(OutputBuffer.java:317) ~[catalina.jar:8.0.18]
at org.apache.catalina.connector.CoyoteOutputStream.flush(CoyoteOutputStream.java:110) ~[catalina.jar:8.0.18]
......
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516) [catalina.jar:8.0.18]
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086) [tomcat-coyote.jar:8.0.18]
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659) [tomcat-coyote.jar:8.0.18]
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223) [tomcat-coyote.jar:8.0.18]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558) [tomcat-coyote.jar:8.0.18]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515) [tomcat-coyote.jar:8.0.18]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_31]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_31]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-util.jar:8.0.18]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_31]
Caused by: java.io.IOException: 断开的管道
at sun.nio.ch.FileDispatcherImpl.write0(Native Method) ~[na:1.8.0_31]
at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:47) ~[na:1.8.0_31]
at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93) ~[na:1.8.0_31]
at sun.nio.ch.IOUtil.write(IOUtil.java:65) ~[na:1.8.0_31]
at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:470) ~[na:1.8.0_31]
at org.apache.tomcat.util.net.NioChannel.write(NioChannel.java:127) ~[tomcat-coyote.jar:8.0.18]
at org.apache.tomcat.util.net.NioBlockingSelector.write(NioBlockingSelector.java:101) ~[tomcat-coyote.jar:8.0.18]
at org.apache.tomcat.util.net.NioSelectorPool.write(NioSelectorPool.java:173) ~[tomcat-coyote.jar:8.0.18]
at org.apache.coyote.http11.InternalNioOutputBuffer.writeToSocket(InternalNioOutputBuffer.java:139) ~[tomcat-coyote.jar:8.0.18]
at org.apache.coyote.http11.InternalNioOutputBuffer.addToBB(InternalNioOutputBuffer.java:197) ~[tomcat-coyote.jar:8.0.18]
at org.apache.coyote.http11.InternalNioOutputBuffer.access$000(InternalNioOutputBuffer.java:41) ~[tomcat-coyote.jar:8.0.18]
at org.apache.coyote.http11.InternalNioOutputBuffer$SocketOutputBuffer.doWrite(InternalNioOutputBuffer.java:320) ~[tomcat-coyote.jar:8.0.18]
at org.apache.coyote.http11.filters.IdentityOutputFilter.doWrite(IdentityOutputFilter.java:93) ~[tomcat-coyote.jar:8.0.18]
at org.apache.coyote.http11.AbstractOutputBuffer.doWrite(AbstractOutputBuffer.java:256) ~[tomcat-coyote.jar:8.0.18]
at org.apache.coyote.Response.doWrite(Response.java:503) ~[tomcat-coyote.jar:8.0.18]
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:388) ~[catalina.jar:8.0.18]
... 76 common frames omitted

主要内容

  • 色彩

色彩

shell脚本中echo显示内容带颜色显示,echo显示带颜色,需要使用参数-e开启转义

  • 格式

    echo -e "\033[字背景颜色;文字颜色m字符串\033[0m"
    

\033[ 是固定语法; 0m 是清除所有格式

例:

其中42的位置代表底色,31的位置代表的是字的颜色

注:
1、字背景颜色和文字颜色之间是英文的”;”
2、文字颜色后面有个m
3、字符串前后可以没有空格,如果有的话,输出也是同样有空格

  • 颜色定义

    可以自己来尝试找出不同颜色搭配

  • 最后面控制选项说明

    \33[0m 关闭所有属性
    \33[1m 设置高亮度
    \33[4m 下划线
    \33[5m 闪烁
    \33[7m 反显
    \33[8m 消隐
    \33[30m — \33[37m 设置前景色
    \33[40m — \33[47m 设置背景色
    \33[nA 光标上移n行
    \33[nB 光标下移n行
    \33[nC 光标右移n行
    \33[nD 光标左移n行
    \33[y;xH设置光标位置
    \33[2J 清屏
    \33[K 清除从光标到行尾的内容
    \33[s 保存光标位置
    \33[u 恢复光标位置
    \33[?25l 隐藏光标 
    \33[?25h 显示光标
    

主要内容

nginx

更新历史
2019-12-30 增加源码安装的自动日志归档
2020-04-20 add timeout config

阅读全文 »

主要内容

  • 函数
  • 文件包含
  • 转码工具

函数

函数可以将一个复杂功能划分成若干模块,让程序结构更加清晰,代码重复利用率更高。函数必须先定义后使用。

函数定义

定义格式如下:
function function_name () {
list of commands
[ return value ]
}

function_name () {
list of commands
[ return value ]
}

function 可以省略,清晰起见,还是建议加上。

函数返回值

  • 函数可以显式增加return语句;如果不加,会将最后一条命令运行结果作为返回值。
  • Shell 函数返回值只能是整数,一般用来表示函数执行成功与否,0表示成功,其他值表示失败。如果 return 其他数据,比如一个字符串,往往会得到错误提示:“numeric argument required”。
  • 如果一定要让函数返回字符串,那么可以先定义一个变量,用来接收函数的计算结果,脚本在需要的时候访问这个变量来获得函数返回值。

例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash
funWithReturn(){
echo "The function is to get the sum of two numbers..."
echo -n "Input first number: "
read aNum
echo -n "Input another number: "
read anotherNum
echo "The two numbers are $aNum and $anotherNum !"
return $(($aNum+$anotherNum))
}
funWithReturn
# Capture value returnd by last command
ret=$?
echo "The sum of two numbers is $ret !"

函数删除

删除函数也可以使用 unset 命令,不过要加上 .f 选项,如下所示:

$unset .f function_name

函数调用

调用只需要给出函数名,不需要加括号。

1
2
3
4
5
6
# Define your function here
Hello () {
echo "Url is http://giveme5.top"
}
# Invoke your function
Hello

函数参数

调用函数时可以向其传递参数。在函数体内部,通过 $n 的形式来获取参数的值,例如,$1表示第一个参数,$2表示第二个参数…
注意,当n>=10时,需要使用 ${n} 来获取参数。 获取第十个参数需要 ${10} , $10 不能获取第十个参数。

例如:

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
funWithParam(){
echo "The value of the first parameter is $1 !"
echo "The value of the second parameter is $2 !"
echo "The value of the tenth parameter is $10 !"
echo "The value of the tenth parameter is ${10} !"
echo "The value of the eleventh parameter is ${11} !"
echo "The amount of the parameters is $# !" # 参数个数
echo "The string of the parameters is $* !" # 传递给函数的所有参数
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73

输出:

he value of the first parameter is 1 !
The value of the second parameter is 2 !
The value of the tenth parameter is 10 !
The value of the tenth parameter is 34 !
The value of the eleventh parameter is 73 !
The amount of the parameters is 12 !
The string of the parameters is 1 2 3 4 5 6 7 8 9 34 73 !"

另外:
几个特殊变量用来处理参数(更多特殊变量):

特殊变量 说明
$# 传递给函数的参数个数。
$* 显示所有传递给函数的参数。
$@ 与$*相同,但是略有区别,请查看更多特殊变量
$? 函数的返回值。



shift

shift: 参数左移指令, 每执行一次,参数序列顺次左移一个位置,$#的值减1,用于分别处理每个参数,移出去的参数,不再可用。

例:
加法计算器,通过 shift 指令使参数左移,求出所有参数的和

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
if [ $# -le 0 ]
then
echo "err!:Not enough parameters"
exit 124
fi
sum=0
while [ $# -gt 0 ]
do
sum=`expr $sum + $1`

shift #参数左移

done
echo $sum

文件包含

文件包含即将外部脚本的内容合并到当前脚本。

可以使用:

. filename

source filename

两种方式的效果相同,简单起见,一般使用点号(.),但是注意点号(.)和文件名中间有一空格。

例如:
创建两个脚本,一个是被调用脚本 subscript.sh,内容如下:

1
url="http://see.xidian.edu.cn/cpp/view/2738.html"

一个是主文件 main.sh,内容如下:

1
2
3
#!/bin/bash
. ./subscript.sh
echo $url

注意:被包含脚本不需要有执行权限。

dos2unix

由于编码问题,在windows中开发的脚本导入到Linux系统后执行报错,主要是因为在windows开发保存时没注意编码(UTF8)和换行符(LF)。
在Linux中可以用工具dos2unix解决。

  • 安装
    [root@localhost test]#rpm -ivh /mnt/Packages/dos2unix-6.0.3-4.el7.x86_64.rpm

  • 使用
    [root@localhost ]# dos2unix test.sh
    dos2unix: converting file test.sh to Unix format …