0

I'm trying to run a command multiple times, changing only the value of a variable (text).

Logic would be like:

myvariables='test1, test2, test3'

echo "my variable is $myvariables"

And then it will be "executed" 3 times, changing the value of the variable...

  • my variable is test1
  • my variable is test2
  • my variable is test3

Of course this is just an example, I do not know if it is possible.

my real code looks like:

#!/bin/sh

SERVER='server1'

if ps ax | grep -v grep | grep $SERVER > /dev/null
then
    echo "$SERVER is online" | echo "<h1>$SERVER is online</h1>" > $SERVER.html
else
    echo "$SERVER is offline" | echo "<h1>$SERVER is offline</h1>" > $SERVER.html
fi

2 Answers 2

1

since your variables consist of single words, it can easily be done by:

#!/bin/sh

SERVERS='server1 server2 server3' # note name in plural

for SERVER in $SERVERS; do
    if ps ax | grep -v grep | grep $SERVER > /dev/null
    then
        echo "$SERVER is online" | echo "<h1>$SERVER is online</h1>" > $SERVER.html
    else
        echo "$SERVER if offline" | echo "<h1>$SERVER is offline</h1>" > $SERVER.html
    fi
done

You might want to change $SERVER.html into something else, if you want a single file with all the online/offline statements

Sign up to request clarification or add additional context in comments.

Comments

1

Try to use array and loop: Here I create array $SERVERS and run through it with for loop:

#!/bin/sh

SERVERS=( server1 server2 serverN )

for server in "${SERVERS[@]}"
do
    if ps ax | grep -v grep | grep $server > /dev/null
    then
        echo "$server is online" | echo "<h1>$server is online</h1>" > $server.html
    else
        echo "$server if offline" | echo "<h1>$server is offline</h1>" > $server.html
    fi
done

I don't understand, what means your echo "$server is online | echo "....

If you need write < h1>...< /h1> in file, also print info in console, don't user pipe, write like that:

...
then
    echo "$server is online" 
    echo "<h1>$server is online</h1>" > $server.html
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.