一、IF 判断
1. 判断文件夹是否存在
if [ ! -d "$dir" ]; then
mkdir "$dir"
fi
2. 判断文件是否存在
if [ ! -f "$file" ]; then
touch "$file"
fi
3. 判断一个变量是否有值
if [ ! -n "$var" ]; then
echo "$var is empty"
exit 0
fi
4. 判断两个变量是否相等
if [ "$var1" = "$var2" ]; then
echo '$var1 eq $var2'
else
echo '$var1 not eq $var2'
fi
5. 检查目录是否包含文件
if [ -e /test/dir/* ]; then
echo "cnbai";
fi;
if find /test/dir/ -maxdepth 0 -empty | read v; then
echo "Empty dir";
fi
6. IF ELSE 语句
if [ $1 == "ab" ]; then
echo "you ab"
elif [ $1 == "cd" ]; then
echo "you cd"
else
echo "you word"
fi
7. 判断参数是否为空
if [ ! -n "$1" ]; then
echo "参数不能为空"
fi
二、CASE 判断
case $1 in
"start"){
for i in 192.168.1.1 192.168.1.2 192.168.1.3
do
echo " --------启动 $i Kafka-------"
ssh $i "/use/local/kafka/bin/kafka-server-start.sh -daemon /use/local/kafka/config/server.properties"
done
};;
"stop"){
for i in 192.168.1.1 192.168.1.2 192.168.1.3
do
echo " --------停止 $i Kafka-------"
ssh $i "/use/local/kafka/bin/kafka-server-stop.sh "
done
};;
esac
三、拷贝文件及文件夹
cp $(find /user/ -type f -name "*.sh") /test
find /user/ -type f -name "*.sh" | xargs -i cp {} /test
四、判断进程
1. 脚本判断杀死进程
tomcat_id=`ps -ef | grep tomcat | grep -v "grep" | awk '{print $2}'`
echo $tomcat_id
for id in $tomcat_id
do
kill -9 $id
echo "killed $id"
done
2. 单行命令判断杀死进程
ps -ef | grep tomcat | grep -v grep | awk '{print $2}' | xargs -n1 kill -9
注:
ps -ef | grep tomcat | grep -v grep | awk '{print \$2}' | xargs -n1 kill -9
3. 获取进程查询结果行数
status_tomcat(){
result=`ps -ef | grep tomcat | grep -v grep | wc -l`
return $result
}
judge_tomcat(){
status_tomcat
if [[ $? -lt 1 ]]; then
echo "tomcat 正在运行"
elif [[ $? -gt 0 ]]; then
echo "tomcat 停止运行"
else
echo "tomcat 有多个"
fi
}
五、FOR 循环
for id in a b c
do
echo $id
done
六、获取当前目录
workdir=$(cd $(dirname $0); pwd)
echo $workdir
七、FTP上传和下载
1. 批量下载文件
HOST='192.168.186.128:10001'
USER='admin'
PASSWD='123456'
downDir="/u01/down"
ftpDir="/u01/ftp"
ftp -v -n $HOST<<EOF
user $USER $PASSWD
binary
cd $ftpDir
lcd $downDir
prompt
mget *
bye
EOF
2. 单个下载文件
HOST='192.168.186.128:10001'
USER='admin'
PASSWD='123456'
downDir="/u01/down"
ftpDir="/u01/ftp"
ftp -v -n $HOST<<EOF
user $USER $PASSWD
binary
cd $ftpDir
lcd $downDir
prompt
get test.txt
bye
EOF
3. 批量上传文件
HOST='192.168.186.128:10001'
USER='admin'
PASSWD='123456'
sourceDir="/u01/source"
ftpDir="/u01/ftp"
ftp -v -n $HOST<<EOF
user $USER $PASSWD
binary
hash
cd $ftpDir
lcd $sourceDir
prompt
mput *
bye
EOF
4. 单个上传文件
HOST='192.168.186.128:10001'
USER='admin'
PASSWD='123456'
sourceDir="/u01/source"
ftpDir="/u01/ftp"
ftp -v -n $HOST<<EOF
user $USER $PASSWD
binary
cd $ftpDir
lcd $sourceDir
prompt
put test.txt
bye
EOF
八、Java 服务的启停脚本
APP_NAME="myapp.jar"
APP_DIR="/opt/myapp"
JAVA_OPTS="-Xms512m -Xmx1024m"
PID_FILE="$APP_DIR/myapp.pid"
start() {
if [ -f $PID_FILE ]; then
echo "$APP_NAME is already running."
exit 1
fi
echo "Starting $APP_NAME..."
nohup java $JAVA_OPTS -jar --spring.profiles.active=prod $APP_DIR/$APP_NAME > /dev/null 2>&1 &
echo $! > $PID_FILE
echo "$APP_NAME started with PID $(cat $PID_FILE)."
}
stop() {
if [ ! -f $PID_FILE ]; then
echo "$APP_NAME is not running."
exit 1
fi
echo "Stopping $APP_NAME..."
kill $(cat $PID_FILE)
rm -f $PID_FILE
echo "$APP_NAME stopped."
}
restart() {
stop
start
}
help() {
echo "Usage: $0 {start|stop|restart|help}"
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
help)
help
;;
*)
echo "Invalid option."
help
exit 1
;;
esac