0

First, please forgive me for my inexperience.

I am trying to create a script that upgrades multiple machines through ssh, and I am receiving errors, primarily "upgrade-all.sh: 7: Bad substitution"

The original script that I am trying to modify mine from is found from Arjun G's answer from post text

My script is:

#!/bin/bash

USERNAMES="jed", "root", "cord"
HOSTS="***.***.***.***", "***.***.***.***", "***.***.***.***"
PASSWORDS="***", "***", "***"
SCRIPT="pwd; echo -e 'PASSWORD' | sudo apt update | sudo apt upgrade -y && sudo apt autoremove -y"
for i in ${!HOSTS[@]} ; do
        echo ${HOSTS[i]}
        SCR=${SCRIPT/PASSWORD/${PASSWORDS[i]}}
        sshpass -p ${PASSWORDS[i]} ssh -l ${USERNAMES[i]} ${HOSTS[i]} "${SCR}"
done

I tried swapping variables in ${!HOSTS[@]}, but it did not clear up the error. I have found myself out of my depth to make educated guess with substitutions.

Thanks for any help!

11
  • 3
    The original script you referenced shows the correct way to create arrays. Why did you change it? Commented Nov 14, 2023 at 18:01
  • 1
    You should have gotten an error on line 3 saying the root, is not a valid command. Commented Nov 14, 2023 at 18:03
  • 3
    Try shellcheck to syntax check, and and the GNU Bash Manual for array syntax. Commented Nov 14, 2023 at 18:03
  • 3
    Maybe consider ansible which is designed for this kind of thing. Commented Nov 14, 2023 at 19:13
  • 1
    Whereas I (cc: @MarkSetchell) consider ansible horrid, but would encourage anyone to consider Chef. (Yes, it's Ruby; I have a deeply-seated distaste for Ruby, but I dislike having tools that aren't powerful enough for my use cases even more; as for ansible, its design isn't rigorous -- the configuration format has ambiguities that make some things impossible to represent -- and the agentless design makes for a steep performance penalty). Mind, I consider ansible (and puppet, and saltstack, and other contemporaries) horrid, but not using any orchestration tool is even worse. Commented Nov 15, 2023 at 3:22

1 Answer 1

0

Here is the same script, with bash syntax corrected (arrays creation) and basic array usage (mostly quoting).

I did not care any security issue (password on command-line), discussed on question comments.

#!/bin/bash

USERNAMES=("jed" "root" "cord")
HOSTS=("***.***.***.***" "***.***.***.***" "***.***.***.***")
PASSWORDS=("***" "***" "***")
SCRIPT="pwd; echo -e 'PASSWORD' | sudo apt update | sudo apt upgrade -y && sudo apt autoremove -y"
for i in "${!HOSTS[@]}" ; do
        echo "${HOSTS[i]}"
        SCR="${SCRIPT/PASSWORD/${PASSWORDS[i]}}"
        sshpass -p "${PASSWORDS[i]}" ssh -l "${USERNAMES[i]}" "${HOSTS[i]}" "${SCR}"
done
Sign up to request clarification or add additional context in comments.

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.