0

i have a script that i use for running various commands across multiple sites based on site number (see script below) i do this by running ./script-name 121 to run at site 121 i can also run it at multiple sites at once just by adding more numbers. what i would like to be able to do is create simple text files containing groups of site number and have the script read that instead of typing out the number each time, any ideas

#!/bin/bash
while [ "TT$1" != "TT" ]
do
  if [ "$1" == "6" -o "$1" == "33" -o "$1" == "55" -o "$1" == "74" -o "$1" == "80" -o "$1" == "91" -o "$1" == "169" ]
  then
     NET=4
     ST=$1
  else
    if [ "$1" -lt "251" ]
    then
      NET=1
      ST=$1
    else
      NET=2
      ST=`expr $1 - 250`
    fi
  fi
echo $NET
echo $ST

ssh root@10.$NET.$ST.210 -C "service xvfbd stop && service xvfbd start && service yespayd start && service yespayd status"

shift
done 
  • 1
    You should maybe consider replacing your if-else constructs with a case statement. Especially for large numbers of values to compare against, I find this more readable and more easy to extend. – Byte Commander Jun 11 '18 at 11:02
0

You want to loop from a text file : use while read
Example :

while read line
do
  echo -e "$line\n"
done <file.txt

Your script may looks like this (i did not test it)
run: ./myscript.sh inpufile.txt

#!/bin/bash
#
# This script takes a filename as argument
# The FILE contains a number per line

# Test argument
if [ -z "$1" ]; then
  echo "No argument supplied"
  exit
else
  inputfile=$1
  if [ ! -f $inputfile ]; then
    echo "InputFile "$inputfile" not found"
    exit
  fi
fi
# Loop
while read line
do
  if [ "$line" == "6" -o "$line" == "33" -o "$line" == "55" -o "$line" == "74" -o "$line" == "80" -o "$line" == "91" -o "$line" == "169" ]
  then
     NET=4
     ST=$line
  else
     if [ "$line" -lt "251" ]; then
       NET=1
       ST=$line
     else
       NET=2
       ST=`expr $line - 250`
     fi
  fi
  echo $NET
  echo $ST
  ssh root@10.$NET.$ST.210 -C "service xvfbd stop && service xvfbd start && service yespayd start && service yespayd status"
done <$inputfile
  • Hi Cmak.fr the script works but only against the first number in the file then it just stops? – Steven Jun 12 '18 at 7:50
  • the loop while-do-done thru file is ok, i tested it with filename stored in inputfile variable like made in the script. you should review your tests if [ "$line" == "6" -o "$line" == "33" -o "$line... possibly need to replace == by -eq – cmak.fr Jun 12 '18 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.