test1.sh
#!/bin/bash
#注释
echo "begin test1.sh"
read -p "请输入num的值:" num
read data1 data2
echo "num=$num"
echo "num1=$1"
echo "num2=$2"
echo "data1=$data1"
echo "data2=$data2"
readonly num=1
echo "num=$num"
num=2
echo "num=$num"
echo "env_data=$LOGNAME"
echo "==================="
echo 'num=$num'
echo "参数的个数=$#"
echo "参数的内容=$*"
echo "第一个参数=$1"
echo "第二个参数=$2"
echo "第三个参数=$3"
echo "进程名=$0"
echo "进程ID=$$"
readonly num=2
num=3
echo "上一个命令的结果=$?"
test2.sh
#!/bin/bash
echo "begin test2.sh"
echo -e "today is \n `date`"
ls
num=10
(
num=100
echo "()num=$num"
)
echo "num1=$num"
{
num=200
echo "{}num=$num"
}
echo "num2=$num"
echo "=========变量的扩展"
#判断变量是否存在
echo ${num11:-100}
num11=200
echo ${num11:-100}
echo ${num22:=50}
echo "num22:=$num22"
str="haha:xixi:hehe"
echo "str的长度为:${#str}"
echo ${str/:/@}
#条件测试
read -p "请输入一个文件名" fileName
result=test -e $fileName
echo "result=$result"
[ -e $fileName ]
echo $?
test -z $sttr
echo $?
read -p "please input y/n:" sttr
[ -z sttr ]
echo "1:$?"
[ $sttr = "y" ]
echo "2:$?"
read -p "请输入第一个字符串" str1
read -p "请输入第二个字符串" str2
[ $str1 = $str2 ]
echo "3:$?"
read -p "first int num" int1
read -p "second int num " int2
[ $int1 -eq $int2 ]
result1=$?
echo "result1=$result1"
echo "命令执行控制====="
test 2 -lt 3 && test 5 -ne 6 && echo "&&success"
test "aaa" = "1" || test "22" = "22" || echo "||end"
test 3 -eq 3 -a 4 -eq 4
test3.sh
#!/bin/bash
if [ $1 -eq $2 ]; then
echo '$1 is equal $2'
else
echo 'aaaa'
fi
read -p "请输入文件夹的名字:" folderName
if [ -e $folderName ]; then
echo "$folderName 文件夹是存在的,即将进入文件夹"
cd $folderName
read -p "是否要创建文件:y/n: " yn
if [ $yn = "y" ]; then
read -p "please input file name: " fileName
touch $fileName
echo "create $fileName success"
else
echo "ok, we'll not create file"
fi
else
echo "this $folderName is not exist"
fi
test4.sh
#!/bin/bash
echo "case demo"
read -p "please input yes/no: " yn
case $yn in
yes | y* | Y*)
echo "is yes"
;;
no|n*|N*)
echo "is no"
;;
*)
echo "is other"
;;
esac
echo "for demo"
declare -i sum=0
declare -i i=0
for (( i=0; i<=100; i++ ))
do
sum=$sum+i
done
echo "sum=$sum"
echo "导入函数"
function my_max ()
{
if [ $1 -gt $2 ]; then
return $1;
else
return $2
fi
}
test5.sh
#!/bin/bash
source ./folder1/test4.sh
read -p "first data is: " data1
read -p "second data is: " data2
my_max $data1 $data2
echo "$data1和$data2的最大值是: $?"
test6.sh
#查看filename文件,如果存在,将标准输出(1)重定向到/dev/null(黑洞),如果不存在,将报错信息标准错误(2)重定向到/dev/null,屏幕不输出内容
cat filename 1>>/dev/null 2>>/dev/null
echo $(date)" is env date" >>$(dirname $0)/testlog.txt
echo $?
==========================分割线===============
#!/bin/bash
# Copyright Huawei Technologies Co., Ltd. 2019-2020. All rights reserved.
#你写的每个脚本都应该在文件开头加上set -e(只能作用于当前脚本),告诉bash如果任何语句的执行结果非0则应该退出。
#set -e 命令用法总结如下:
#1. 当命令的返回值为非零状态时,则立即退出脚本的执行。
#2. 作用范围只限于脚本执行的当前进程(. test.sh 这样执行的test.sh存在当前进程),不作用于其创建的子进程(test.sh 这样是创建的子进程)。
#3. 另外,当想根据命令执行的返回值,输出对应的log时,最好不要采用set -e选项,而是通过配合exit 命令来达到输出log并退出执行的目的。
set -e
PROCESS_CMD="cloudcvengine-focusocr"
#$0 表示当前执行的命令
#readlink -f $0 找出当前脚本所在目录./start.sh(符号链接)找到真正的可执行文件,再用dirname取父目录路径
CUR_DIR="$(dirname $(readlink -f $0))"
#以进程名关键字"cloudcvengine-focusocr"、jar类型过滤找到进程并显示文件的行数wc -l
num=$(ps -ef | grep "${PROCESS_CMD}" | grep jar | grep -v grep | wc -l)
if [ $num -ne 0 ]; then
#awk '{print $2}' $fileName : 一行一行的读取指定的文件, 以空格作为分隔符,打印第二个字段
#-F' ' :规定以什么作为分隔符,这里表示的以' '空格
pid=$(ps -ef | grep "${PROCESS_CMD}" | grep jar | grep -v grep | awk -F' ' '{print $2}')
echo "focusocr tf service had been started, pid=${pid}"
#exit -1/1 非正常运行导致退出shell脚本 exit 0 正常运行并退出脚本
exit 0
fi
#cd .;pwd 获取当前脚本所在地绝对路径
SCRIPT_PATH=$(cd $(dirname $0);pwd)
#判断绝对路径上上级目录是否存在logs 不存在就-p递归创建目录
START_LOG_DIR=${SCRIPT_PATH}/../../logs/focusocr-service
if [ ! -d "${START_LOG_DIR}" ]; then
mkdir -p ${START_LOG_DIR}
fi
cd ${SCRIPT_PATH}/..
#递归.目录为所有文件夹/文件在u(当前用户)赋予7(r+w+x:读+写+执行)的权限
chmod -R 700 .
WATCH_DOG=/
CUDA_HOME=/usr/local/cuda
JAVA_OPS="-Xms4096M -Xmx4096M -XX:NewRatio=1"
APP_ROOT=${SCRIPT_PATH}/..
bash ${APP_ROOT}/init.d/install_auto.sh
JAVA_OPTS="$JAVA_OPTS"
#为当前脚本设置环境变量
export SEC_RASP_CUSTOM_CONF=$APP_ROOT/conf/rasp
[[ ! -d $SEC_RASP_HOME ]] && SEC_RASP_HOME=/opt/huawei/secRASP
if [ -f "$SEC_RASP_HOME/slave_agent/bin/set_java_opts.sh" ]; then
#引入其他脚本
source $SEC_RASP_HOME/slave_agent/bin/set_java_opts.sh
fi
LD_LIBRARY_PATH=${CUDA_HOME}/lib64:./jni:$LD_LIBRARY_PATH java ${JAVA_OPTS} ${JAVA_OPS} -jar ${PROCESS_CMD}*.jar 2>>/dev/null >>/dev/null &
echo $(date)" Execute {$0} command!" >>${START_LOG_DIR}/start.log
sleep 1
num=$(ps -ef | grep "${PROCESS_CMD}" | grep jar | grep -v grep | wc -l)
if [ $num -ne 0 ]; then
pid=$(ps -ef | grep "${PROCESS_CMD}" | grep jar | grep -v grep | awk -F' ' '{print $2}')
echo "focusocr tf service has been starting, pid=${pid}"
else
echo "focusocr tf service start failed, there is no such pid"
fi
if [[ $# -ne 1 || "$1" != "watchdog" ]]; then
bash $CUR_DIR/../watchdog/bin/watchdog.sh start > /dev/null 2>&1
bash $CUR_DIR/../watchdog/bin/watchdog.sh mon > /dev/null 2>&1
fi
|