-1

Working script for single host

I sourced the following bash scripts inside .bashrc and is working fine with single hostname host1. I can do scp, rsync and other remote commands without any problem. But I want to use it for multiple hostnames eg. host1, host2, host3.

HPC_HOST=${HPC_HOST:-host1}

lastarg() {
    # get the last argument
    for last; do true; done; echo $last;
}

hpc() {
    HERE="~${PWD#$HOME}"
    ssh -t $HPC_HOST "cd $HERE; bash";
}

scp_to_hpc() {
    # Usage: scp_to_hpc files
    # Purpose: will copy file to crysden:$(pwd)/file
    #hpc_mkcwd
    HERE="~${PWD#$HOME}"
    scp $@ $HPC_HOST:$HERE
}


Current Solution

Currentlty I created three copies of the same script and modified the function names accordingly with respect to host name.

Expected Solution

I expect there must be a way to call the multiple hostname in the above script. and execute the commands with anyhost.( may be we need to define function varaible as well for host name.

I also apreciate the possible ideas to use multiple host in the above bash script.

sai
  • 87
  • 8
  • Do you mean you want to run the same command on three hosts in one go, or just generalize the script so it doesn't hard-code `host1`? – tripleee Jul 19 '21 at 08:34
  • As an aside, an unquoted `$@` is basically always a bug; it will break if you have arguments which contain spaces. In general, try http://shellcheck.net/ before asking for human assistance. – tripleee Jul 19 '21 at 08:35
  • @tripleee Thanks for your suggestion regarding `$@`. I will look into it. Currently, I managed to solved my problem by putting everything inside the heredoc and changed all functions name into a variable, e.g `scp_to_hpc` into `scp_to_${hpc}`. So for every host it creates separate files. Sorry for the late reply. And thanks for your suggestion. – sai Aug 10 '21 at 04:43

1 Answers1

0

You can use a dictionary in bash, have multiple keys and you set the value, when you want to use a specific key, then you can retrieve this.

To create a dictionary declare -A test_var

Add key/value string literals without quote test_var[key1]=value1

Add key/value string literals with quote test_var['key2']='value2'

Add key/value pair using bash variables

another_key_var='key3'
another_value_var='value3'
test_var[$another_key_var]=$another_value_var

to retrieve key-value pairs in bash

echo ${test_var[key1]}
echo ${test_var[key2]}
echo ${test_var[$another_key_var]}

Update an Existing Key-Value in a Dictionary in Bash `test_var[key1]='another_value1'

Check if a Key Exists in a Dictionary in Bash

if [ -v test_var[key1] ]; then
    echo "key1 exists in a dictionary"
fi

if [ ! -v test_var[key2] ]; then
    echo "key2 does not exists in a dictionary"
fi

Remove a Key-Value Pair from a Dictionary in Bash

unset test_var[key1]
unset test_var['key2']
unset test_var[$another_key_var]

Iterate a Dictionary in Bash

for key in "${!test_var[@]}"; do
    echo "$key ${test_var[$key]}"
done
Shaqil Ismail
  • 1,794
  • 1
  • 4
  • 5
  • Perhaps note that associative arrays are a Bash 4+ feature, though, which means e.g. MacOS users still get a too old Bash version out of the box. – tripleee Jul 19 '21 at 08:31
  • @Shaqil Ismail, thanks a lot for your idea. I will look in this way while implementing the script. Currently, I managed to write a working script, but later I will implement it. Sorry for the late response, and thanks a lot for your suggestions. – sai Aug 10 '21 at 04:47