Modifying bash script to take each line in a file and execute command

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

#!/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 named "pfile":

username password
       username2 password2
              username3 password3

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

192.168.0.1
192.168.0.2
192.168.0.3

Any suggestions how I can do this? I really have no clue.

---------- Post updated at 04:50 PM ---------- Previous update was at 04:46 PM ----------

192.168.0.1
192.168.0.2
192.168.0.3

For some reason I cannot edit the above post. The host file is in this format, line-by-line.

Your script will not work, as the shebang is on the same line as everything else.

So called 'One-Liners' are nice, if they are short, but yours is not.
Please structure your code for easier reading/debuging.

Thank you

EDIT:
Furthermore, please use the forum its search function, just recently (within the last 30 days) we've discussed such a topic.

in a file named like scr.sh try something like:

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

Actually it works when I execute it for one host per command.

[/home/virtual/mysqlt]# sh sql2 localhost
[/home/virtual/mysqlt]# ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

I want it to make automatic to use the full lines of the hosts file.

So what it needs to do...

  • load the hosts file
  • read each line and execute ./mysql etc etc which the procedure is already there.

I have searched google and came up with this:

for file in *foo*
do   
if [ -f "$file" ];then 
mv "$file" /destination    
fi 
done

I really am new to this and I have no clue where to insert that and adapt it to suit my needs.

---------- Post updated at 05:21 PM ---------- Previous update was at 05:15 PM ----------

Yep, its working. Thank you. Also thank you for making this clean.