报错代码
binary operator expected
解决方法
将 if 判断中的变量,打上双引号 “”””
将以下内容:
......
if [ -z $1 ];then
......
修改为:
......
if [ -z "$1" ];then
......
(补充:这里以给 $1 变量打上双引号 “””” 为例)
binary operator expected
将 if 判断中的变量,打上双引号 “”””
将以下内容:
......
if [ -z $1 ];then
......
修改为:
......
if [ -z "$1" ];then
......
(补充:这里以给 $1 变量打上双引号 “””” 为例)
作者:朱明宇
名称:批量检测指定用户是否可以登录本地服务器
作用:批量检测指定用户是否可以登录本地服务器
1. 在此脚本的分割线内写入相应的内容
2. 给此脚本添加执行权限
3. 执行此脚本
directorylist=”daemon bin sys adm uucp guest nobody lpd lp” #要被检测的用户
#!/bin/bash
####################### Separator ########################
directorylist="daemon bin sys adm uucp guest nobody lpd lp"
####################### Separator ########################
directorycheck=
for i in `echo $directorylist`
do
directorycheck="$directorycheck `cat /etc/passwd | egrep "^$i:" | egrep -v '/sbin/nologin|false|/bin/false' | awk -F: '{print $1}'`"
done
echo $directorycheck
#!/bin/bash
while ((1));
do
sleep 1
echo "infinite loop"
done
或者:
#!/bin/bash
while :
do
sleep 1
echo "infinite loop"
done
#!/bin/bash
i=1;n=5
while ((i <= n))
do
echo $i
let i++
done
或者:
#!/bin/bash
i=1;n=5
while [[ $i -le $n ]]
do
echo $i
let i++
done
#!/bin/bash
i=1
while ((i <= 5));
do
let sum=$sum+$i
let i++
done
echo $sum
或者:
#!/bin/bash
i=1
while [[ $i -le 5 ]];
do
let sum=$sum+$i
let i++
done
echo $sum
#!/bin/bash
sum=0
echo 'Please input number, you must press 'enter' after inputting each number, press 'ctrl' and 'd' at the same time to end the input"
while read num
do
let sum=$sum+$num
done
echo $sum
#!/bin/bash
while read line
do
echo $line
done < test.txt
或者:
#!/bin/bash
cat test.txt | {
while read line
do
echo $line
done }
或者:
#!/bin/bash
cat test.txt | while read line
do
echo $line
done
(补充:这里以阅读 test.txt 文件为例)
# vi test.txt
创建以下内容:
no1 user1 test1.txt
no2 user2 test2.txt
no3 user3 test3.txt
no4 user4 test4.txt
no5 uesr5 test5.txt
# cat test.txt | while read no user file ;do echo $no $file;done
no1 test1.txt
no2 test2.txt
no3 test3.txt
no4 test4.txt
no5 test5.txt
作者:朱明宇
名称:测试 SFTP 服务
作用:测试 SFTP 服务
1. 在此脚本的分割线内写入相应的内容
2. 给此脚本添加执行权限
3. 执行此脚本
IP=10.0.0.8 #要测试 SFTP 的服务器 IP 地址
此脚本执行前必须要先保证执行脚本的主机能无秘钥远程需要测试 SFTP 服务的服务器
#!/bin/bash
####################### Separator ########################
IP=10.0.0.8
####################### Separator ########################
set timeout 3
rpm -q expect &> /dev/null
if [ $? -ne 0 ];then
echo "Expect needs to be installed first"
fi
expect << EOF
spawn sftp $IP
expect "sftp>" {send "cd /tmp\r" }
expect "sftp>" {send "ls -l\r"}
expect "sftp>" {send "quit\r"}
expect ">" {send "\r"}
EOF
作者:朱明宇
名称:显示系统常用信息
作用:显示系统常用信息
1. 在此脚本的分割线内写入相应的内容
2. 给此脚本添加执行权限
3. 执行此脚本
1. times=5 #显示系统常用信息的次数
2. sleeptime=0.3 #大部分行与行之间显示的间隔时间
1. 需要安装 sysstat 软件
2. 执行此脚本的用户能够使用 sudo ip a s 命令
3. 执行此脚本的用户能够使用 sudo ss -ntulap 命令
4. 搭建了 KVM 虚拟化平台后执行此脚本的用户能够使用 sudo virsh list 命令后才能实现
#!/bin/bash
####################### Separator ########################
times=5
sleeptime=0.3
####################### Separator ########################
nowtime=1
while (( nowtime <= times))
do
echo -e "Start Monitoring: \c"
for i in {1..94}
do
echo -e "#\c"
sleep 0.01
done
echo
sleep $sleeptime
host=`hostname`
echo -e "Name:\t\t\t\t\t\t\t \033[1m$host\033[0m"
ip=`sudo ip a s | awk '/[1-2]?[0-9]{0,2}\.[1-2]?[0-9]{0,2}/&&!/127.0.0.1/{print $2}' | awk -F/ '{print $1}'`
for iip in $(echo $ip)
do
sleep $sleeptime
echo -e "IP Address:\t\t\t\t\t\t \033[1m$iip\033[0m"
done
sleep $sleeptime
cpu=`top -bn 1 | awk -F',' '/^%Cpu/{print $4 }' | awk '{print $1}' | awk '{print 100-$1}'`
echo -e "CPU Usage (Total):\t\t\t\t\t \033[1m$cpu%\033[0m"
sleep $sleeptime
mem=`free | grep Mem | awk '{print $3/$2 * 100.0}' | egrep -o "[1]?[0-9]{0,2}\.[0-9]"`
echo -e "Memory Usage (Total):\t\t\t\t\t \033[1m$mem%\033[0m"
directory=`df -h | grep -v run | grep -v boot | awk '$1~/\/dev/{print $6}'`
for idirectory in `echo $directory`
do
sleep $sleeptime
directoryusage=`df -h | grep -v run | grep -v boot | awk '$1~/\/dev/{print}' | grep $idirectory$ | awk '{print $5}'`
if [ $idirectory == / -o $idirectory == /sda -o $idirectory == /sdb ];then
echo -e "Directory Usage ($idirectory):\t\t\t\t\t \033[1m$directoryusage\033[0m"
else
echo -e "Directory Usage ($idirectory):\t\t\t\t \033[1m$directoryusage\033[0m"
fi
done
sudo -l | grep 'virsh list' &> /dev/null
if [ $? -eq 0 ];then
sleep $sleeptime
virtual=`sudo virsh list | egrep [0-9] | wc -l`
echo -e "Number of Virtual Machines (Total):\t\t\t \033[1m$virtual\033[0m"
fi
sleep $sleeptime
user=`who | wc -l`
echo -e "Number of User Logins (Total):\t\t\t\t \033[1m$user\033[0m"
soft=`rpm -qa | wc -l`
echo -e "Number of Softwares (Total):\t\t\t\t \033[1m$soft\033[0m"
sleep $sleeptime
port=`sudo ss -ntulap | wc -l`
echo -e "Number of Open Ports (Total):\t\t\t\t \033[1m$port\033[0m"
which sar &> /dev/null
if [ $? -eq 0 ];then
networkcard=`ifconfig | awk -F: '/flags/&&!/lo/{print $1}'`
for inetworkcard in `echo $networkcard`
do
networkread="`sar -n DEV 1 1 | grep $inetworkcard | awk '/[0-9][0-9]:[0-9][0-9]/{print $3/1000}'` m/s"
networkwrite="`sar -n DEV 1 1 | grep $inetworkcard | awk '/[0-9][0-9]:[0-9][0-9]/{print $4/1000}'` m/s"
echo $inetworkcard | grep eth &> /dev/null
if [ $? -ne 0 ];then
echo -e "Network Card IO ($inetworkcard):\t\t\t\t \033[1m$networkread\033[0m (Read)\t\033[1m$networkwrite\033[0m (Write)"
else
echo -e "Network Card IO ($inetworkcard):\t\t\t\t\t \033[1m$networkread\033[0m (Read)\t\033[1m$networkwrite\033[0m (Write)"
fi
done
fi
which iostat &> /dev/null
if [ $? -eq 0 ];then
disk=`iostat -d -k 1 1 | awk '!/^$/&&!/Device/&&!/Linux/{print $1}'`
for idisk in `echo $disk`
do
sleep $sleeptime
diskread="`iostat -d -k 1 1 | grep $idisk | awk '{print $3/1000}'` m/s"
diskwrite="`iostat -d -k 1 1 | grep $idisk | awk '{print $4/1000}'` m/s"
echo $idisk | grep 'nvme' &> /dev/null
if [ $? -eq 0 ];then
echo -e "Disk IO (/dev/$idisk):\t\t\t\t\t \033[1m$diskread\033[0m (Read)\t\033[1m$diskwrite\033[0m (Write)"
else
echo -e "Disk IO (/dev/$idisk):\t\t\t\t\t \033[1m$diskread\033[0m (Read)\t\033[1m$diskwrite\033[0m (Write)"
fi
done
fi
echo -e "Complete Monitoring: \c"
for i in {1..91}
do
echo -e "#\c"
sleep 0.01
done
echo
sleep $sleeptime
let nowtime++
done
echo -e "Terminal Monitoring: \c"
for i in {1..91}
do
echo -e "#\c"
sleep 0.01
done
exit