ssh 脚本从文件读取变量

ssh 脚本从文件读取变量

我有一个脚本,用于根据站点编号在多个站点上运行各种命令(请参阅下面的脚本),我通过./script-name 121在站点 121 上运行来做到这一点,我也可以通过添加更多数字同时在多个站点上运行它。我希望能够做的是创建包含站点编号组的简单文本文件,并让脚本读取它,而不是每次都输入数字,有什么想法吗

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

您想从文本文件循环:使用 while read
示例:

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

您的脚本可能看起来像这样(我没有测试过
跑步:./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

相关内容