我需要修改bash脚本,以获取文件中的每一行并执行命令。我目前有这个:
#!/bin/bash
if [ -z "$1" ] ; then
echo "Lipsa IP";
exit;
fi
i=1
ip=$1
while [ $i -le `wc -l pass_file | awk '{print $1}'` ] ; do
if [ -n "$ip" ]; then
rand=`head -$i pass_file | tail -1`
user=`echo $rand | awk '{print $1}'`
pass=`echo $rand | awk '{print $2}'`
CMD=`ps -eaf | grep -c mysql`
if [ "$CMD" -lt "50" ]; then
./mysql $ip $user $pass &
else
sleep 15
fi
i=`expr $i + 1`
fi
done
密码文件的格式和名称为pfile:
username password
intranet主机文件的格式为(逐行),名称为hlist:
192.168.0.1
192.168.0.2
192.168.0.3
有什么建议吗?
发布于 2016-03-09 21:51:17
我不明白你想做什么,你还没有做什么。您想以某种方式使用ip号码文件吗?
无论如何,从密码文件中提取用户名和密码的方法是不必要的复杂(礼貌地说);您可以用一种简单得多的方式遍历文件中的每一行。而不是:
while [ $i -le `wc -l pass_file | awk '{print $1}'` ] ; do
rand=`head -$i pass_file | tail -1`
user=`echo $rand | awk '{print $1}'`
pass=`echo $rand | awk '{print $2}'`
# ...
i=`expr $i + 1`
fi
只需使用bash (Posix) read命令:
while read -r user pass __; do
# ...
done < pass_file
(如果pass_file中有一行具有两个以上的值,则使用__
;read
命令中的最后一个变量名接收“该行的其余部分”)。
发布于 2016-03-09 22:22:42
我再次在网上搜索,发现了一个更干净的代码,我根据自己的需要进行了调整。
#!/bin/bash
while read ip
do
if [ -n "$ip" ]
then
while read user pass
do
CMD=`ps -eaf | grep -c mysql`
if [ "$CMD" -gt "50" ]
then
sleep 15
fi
./mysql $ip $user $pass &
done < pass_file
fi
done < host_file
https://stackoverflow.com/questions/35903150
复制