一些shell脚本

1.判断目录是否为空

DIRECTORY=$1
#在此加上是不是目录的判断。
if [ "ls -A $DIRECTORY" = "" ]; then
echo "$DIRECTORY 目录是空的"
else
echo "$DIRECTORY 目录非空"
fi

2.判断文件夹是否存在

if [ -d "/data/" ];then
  echo "文件夹存在"
  else
  echo "文件夹不存在"
fi

3.判断文件是否存在

if [ -f "/data/filename" ];then
  echo "文件存在"
  else
  echo "文件不存在"
fi
-e 判断对象是否存在
-d 判断对象是否存在,并且为目录
-f 判断对象是否存在,并且为常规文件
-L 判断对象是否存在,并且为符号链接
-h 判断对象是否存在,并且为软链接
-s 判断对象是否存在,并且长度不为0
-r 判断对象是否存在,并且可读
-w 判断对象是否存在,并且可写
-x 判断对象是否存在,并且可执行
-O 判断对象是否存在,并且属于当前用户
-G 判断对象是否存在,并且属于当前用户组
-nt 判断file1是否比file2新  [ "/data/file1" -nt "/data/file2" ]
-ot 判断file1是否比file2旧  [ "/data/file1" -ot "/data/file2" ]

4.堡垒机自动登录脚本

#!/bin/bash
# 需要先安装expect
expect -c "
    # 开启新会话
    spawn -noecho ssh 用户名@堡垒机ip -p 12024;  
    # 等待屏幕上出现password,然后模拟键盘输入发送密码
    expect *password:*;  
    send -- 输入密码密码\r;
    # 等待屏幕上出现堡垒机Shell界面,然后发送跳转符
    expect *NUM*; 
    #send :;
    sleep 1
    # 发送跳转的编号和回车
    send 3\r;
    sleep 1
    send \r;
    send /deploy/app/bin/test.sh\r;   #执行服务器上的脚本
    send \r;

    sleep 3;
    send exit\r;
    # 交还交互式控制权给用户
    #interact;
"
  1. 查找当前目录下指定后缀的文件
FILE=""

function getfile(){
 for file in `ls`;
 do
 if [[ $file =~ \.jar$  ]];then
 FILE=$file
fi
done
}
posted @ 2023-10-13 13:23  怀里的懒猫  阅读(13)  评论(0)    收藏  举报