赞
踩
目录
【写在前面】
问题的原因和解决方案的内容,参考自下方文章,可直接看原文:
shell脚本执行报错:/bin/bash^M: bad interpreter: No such file or directory
为了处理性能诊断的一些问题,T准备了一份topcpu.sh(内容见文末),用来自动获取某个进程的多个线程信息,并自动完成一些转换(比如进制转换等): ./topcpu.sh -p ${pid} 5 (5表示要展示的个数)。
文档是在在Win10环境下编写的,我直接使用这个脚本在centOS7中运行,结果报错了:/bin/bash^M: bad interpreter: No such file or directory......
(1)报错原因?
经过搜索相关文章和资料,获知:shell脚本格式必须是unix,但这个脚本是在win10上编写,然后直接使用在Linux上,所以报错。
因为:windows环境下的文件是dos格式,即每行结尾以\r\n来标识,而linux下的文件是unix格式,行尾则以\n来标识。
(2)如何判断某脚本是dos格式还是unix格式?
1)方式一:cat -A filename
解析:输出结果中行末尾是^M$,则是dos格式;行末尾只是$,则是unix格式。
2)方式二:vim filename
解析:编辑文件,执行“:set ff”。若执行结果为fileformat=dos则为dos格式,若执行结果为fileformat=unix则为unix格式。
3)方式三:od -t x1 filename
解析:以16进制查看文件,若输出结果中存在“0d 0a”则为dos格式,若只有“0a”则为unix格式。其中“0d”即为回车符“\r”,“0a”即为换行符“\n”。
(1)方法一:sed -i "s/\r//" filename 或 sed -i "s/^M//" filename
解析:直接将回车符替换为空字符串。
(2)方法二:vim filename,编辑文件,执行“: set ff=unix”
解析:将文件设置为unix格式,执行“:wq”,保存退出。
(3)方法三:dos2unix filename 或 busybox dos2unix filename
解析:如果提示command not found,可以优先考虑使用前两种方法。
【附】 topcpu.sh脚本内容如下:
- #!/bin/bash
- # @Function
- # TOP CPU Thread INFO.
- #
- #
- #
-
- PROG=`basename $0`
-
- usage() {
- cat <<EOF
- Usage: ${PROG} [OPTION]...
- Java top cpu print the stack of these threads.
- Example: ${PROG} -c 10
- Options:
- -p, --pid java process(use jps find)
- -c, --count set the thread count to show, default is 5
- -h, --help display this help and exit
- EOF
- exit $1
- }
-
- ARGS=`getopt -n "$PROG" -a -o c:p:h -l count:,pid:,help -- "$@"`
- [ $? -ne 0 ] && usage 1
- eval set -- "${ARGS}"
-
- while true; do
- case "$1" in
- -c|--count)
- count="$2"
- shift 2
- ;;
- -p|--pid)
- pid="$2"
- shift 2
- ;;
- -h|--help)
- usage
- ;;
- --)
- shift
- break
- ;;
- esac
- done
- count=${count:-5}
-
- redEcho() {
- [ -c /dev/stdout ] && {
- # if stdout is console, turn on color output.
- echo -ne "\033[1;31m"
- echo -n "$@"
- echo -e "\033[0m"
- } || echo "$@"
- }
-
- ## Check the existence of jstack command!
- if ! which jstack &> /dev/null; then
- [ -n "$JAVA_HOME" ] && [ -f "$JAVA_HOME/bin/jstack" ] && [ -x "$JAVA_HOME/bin/jstack" ] && {
- export PATH="$JAVA_HOME/bin:$PATH"
- } || {
- redEcho "Error: jstack not found on PATH and JAVA_HOME!"
- exit 1
- }
- fi
-
- uuid=`date +%s`_${RANDOM}_$$
-
- cleanupWhenExit() {
- rm /tmp/${uuid}_* &> /dev/null
- }
- trap "cleanupWhenExit" EXIT
-
- printStackOfThread() {
- while read threadLine ; do
- pid=`echo ${threadLine} | awk '{print $1}'`
- threadId=`echo ${threadLine} | awk '{print $2}'`
- threadId0x=`printf %x ${threadId}`
- user=`echo ${threadLine} | awk '{print $3}'`
- pcpu=`echo ${threadLine} | awk '{print $5}'`
-
- jstackFile=/tmp/${uuid}_${pid}
-
- [ ! -f "${jstackFile}" ] && {
- jstack ${pid} > ${jstackFile} || {
- redEcho "Fail to jstack java process ${pid}!"
- rm ${jstackFile}
- continue
- }
- }
-
- redEcho "The stack of busy(${pcpu}%) thread(${threadId}/0x${threadId0x}) of java process(${pid}) of user(${user}):"
- sed "/nid=0x${threadId0x}/,/^$/p" -n ${jstackFile}
- done
- }
-
- [ -z "${pid}" ] && {
- ps -Leo pid,lwp,user,comm,pcpu --no-headers | awk '$4=="java"{print $0}' |
- sort -k5 -r -n | head --lines "${count}" | printStackOfThread
- } || {
- ps -Leo pid,lwp,user,comm,pcpu --no-headers | awk -v "pid=${pid}" '$1==pid,$4=="java"{print $0}' |
- sort -k5 -r -n | head --lines "${count}" | printStackOfThread
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。