0

My script will be like this when I pass all servers in the array it will work. If I pass a single server as well it should go through loop and start/stop single one only. Can someone help me on this?

Input : ./script.sh shut_all_vm $2 $3 $VM Ex : ./script.sh shut_all_vm perf-loadgen2

If i pass the "perf-loadgen2" as param to script like above example it would start that VM only by entering into the loop.

If I didn't pass any args to script (just calling ./script.sh) it would go through each server in the array and start all VM's

shut_all_vm(){    
    cd $WORKSPACE/$SCRIPTS_DIR
    declare -a VMs=("$@" $@-"perf-ld" $@-"perf-ld2" $@-"perf-loadgen2")


     for i in "${VMs[@]}"
     do
         echo "$i" 
         az login $2 $3
         az vm stop --resource-group $RESOURCE_GROUP --name $i
         az vm deallocate -g $RESOURCE_GROUP -n $i     
        echo "$i server has been stopped successfully..."
    done      
 }

$1 $2 $3 $VM
  • It's not clear what you mean. Does the code not work if you put a single server in the array? Or do you mean replace the array with a command-line argument if one is present? – tripleee Nov 30 '21 at 07:26
  • Sorry about that. My script is working fine. If i pass only one argument (Ex - "perf-loadmgr") ./script.sh perf-loadmgr it should go through that loop and start/stop that VM only and shouldn't act on other VM's in the array even all the servers available in the array. How can I get it ? – Kandikuppa Vinod Nov 30 '21 at 07:30
  • The command-line arguments are in `"$@"`. You might set your array to that if set, and fall back to the default list if it's empty. – tripleee Nov 30 '21 at 07:41
  • declare -a VMs=("$@" $@-"perf-loadmgr" $@-"perf-loadgen1" $@-"perf-loadgen2") for i in "${VMs[@]}" do logic didn't worked. Can you pls give other solution ? – Kandikuppa Vinod Nov 30 '21 at 07:45
  • I don't understand why the question is closed. Can some one give solution to my query pls ? – Kandikuppa Vinod Nov 30 '21 at 09:24
  • 1
    Because the question is unclear. For an understandable question, you would have to explain what is on the parameters $1 and $2. You would have to explain which of the commands in the script did not work in the expected way (i.e. explain what effect you expected, and what effect you got instead). Your script is only handling two parameters, and I don't understand what effect you expect to see with more than two parameters. Finally, adding important details to the questions should not be made in comments, but by editing the question itself. – user1934428 Nov 30 '21 at 09:38
  • Hi @user1934428, I edited my question. Hope the question will understand to you. Please check and give the proper solution – Kandikuppa Vinod Nov 30 '21 at 09:47
  • 1
    You still have not explained one point I asked: If you run it as `./script.sh perf-loadgen2`, what exactly is to go into $1 and $2 in the `az login` command? – user1934428 Nov 30 '21 at 09:51
  • I edited my script. I am calling function here where $1 is function name and the $2,$3 are azure creds, $VM is the server which i want to perform start/stop. (Ex: ./script.sh shut_all_vm uid pwd perf-loadgen2). If I pass perf-loadgen2 it should start/stop that server from array by entering into loop and if i pass 2 server names it should start/stop one after one. If i didn't pass any argumenr in $3 it should start/stop all VM's – Kandikuppa Vinod Nov 30 '21 at 10:27

1 Answers1

0

You can let your function accept zero or more arguments, and fall back to a set of default arguments when there are none.

If you want to pass in mandatory arguments for login and password, I would make those the first and second arguments instead of tack them on at the end.

shut_all_vm(){    
    local user=$1
    local pass=$2
    shift
    shift
    local VMs
    declare -a VMs=("perf-ld" "perf-ld2" "perf-loadgen2")

    az login "$user" "$pass"

    for i in "${@-${VMs[@]}}"
    do
         echo "$i" >&2
         az vm stop --resource-group "$RESOURCE_GROUP" --name "$i"
         az vm deallocate -g "$RESOURCE_GROUP" -n "$i"
         echo "$i server has been stopped successfully..." >&2
    done      
}

shift removes the first argument from $@ and slides down the rest of the list so that the previous $2 is now $1, etc.

I'm imagining it only makes sense to log in once, so I moved that outside the loop.

Nothing in the code appears to be using the current working directory, so I took out the spurious cd, too. See also What exactly is current working directory?

I also made the script print diagnostic messages to standard error instead of standard output (>&2) and added quoting.

The parameter expansion ${variable-value} expands to variable if it is set, otherwise to value. (The syntax $@-value simply concatenates $@ with the static string -value.)

Arrays are not available in the Bourne shell; I have assumed you are using Bash, though this could probably be made to work in ksh or zsh as well.

tripleee
  • 175,061
  • 34
  • 275
  • 318