修改bash脚本以获取文件中的每一行并执行命令

时间:2022-04-27 04:45:35

I need to modify a bash script to to take each line in a file and execute command. I currently have this:

我需要修改一个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

The password file is in format and name pfile:

密码文件的格式和名称为pfile:

username password

The intranet hosts file is in this format (line-by-line) and name hlist:

Intranet hosts文件采用此格式(逐行)和名称hlist:

192.168.0.1
192.168.0.2
192.168.0.3

Any suggestions?

有什么建议么?

2 个解决方案

#1


0  

I don't understand what you want to do which you are not already doing. Do you want to use the ip number file in some fashion?

我不明白你想做什么,你还没有做过。你想以某种方式使用ip号文件吗?

Anyway, the way you extract the username and password from the password file is unnecessarily complicated (to put it politely); you can iterate over the lines in a file in a much simpler fashion. Instead of:

无论如何,从密码文件中提取用户名和密码的方式不必要地复杂化(礼貌地说);您可以以更简单的方式迭代文件中的行。代替:

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

Just use the bash (Posix) read command:

只需使用bash(Posix)读取命令:

while read -r user pass __; do
  # ...
done < pass_file

(The __ is in case there is a line in the pass_file with more than two values; the last variable name in the read command receives "the rest of the line").

(__是因为pass_file中有一行有两个以上的值; read命令中的最后一个变量名称接收“该行的其余部分”)。

#2


0  

I searched the web again and found a cleaner code which I adapted to suit my needs.

我再次搜索网页,发现了一个更清洁的代码,我根据自己的需要进行了调整。

#!/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

#1


0  

I don't understand what you want to do which you are not already doing. Do you want to use the ip number file in some fashion?

我不明白你想做什么,你还没有做过。你想以某种方式使用ip号文件吗?

Anyway, the way you extract the username and password from the password file is unnecessarily complicated (to put it politely); you can iterate over the lines in a file in a much simpler fashion. Instead of:

无论如何,从密码文件中提取用户名和密码的方式不必要地复杂化(礼貌地说);您可以以更简单的方式迭代文件中的行。代替:

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

Just use the bash (Posix) read command:

只需使用bash(Posix)读取命令:

while read -r user pass __; do
  # ...
done < pass_file

(The __ is in case there is a line in the pass_file with more than two values; the last variable name in the read command receives "the rest of the line").

(__是因为pass_file中有一行有两个以上的值; read命令中的最后一个变量名称接收“该行的其余部分”)。

#2


0  

I searched the web again and found a cleaner code which I adapted to suit my needs.

我再次搜索网页,发现了一个更清洁的代码,我根据自己的需要进行了调整。

#!/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