I think that you are looking for
pacmd set-default-sink <sink_name|index>
as outlined in https://askubuntu.com/questions/690711/change-sound-output-from-command-line and https://askubuntu.com/questions/14077/how-can-i-change-the-default-audio-device-from-command-line
You can get the available index with
pacmd list-sinks|grep index|awk ''
If you want to control the volume you can do that on the commandline with alsamixer (ncurses) or amixer.
If you are going to play with pulseaudio settings then
cp -i /etc/pulse/default.pa ~/.config/pulse/
will give you your own copy that is safer to edit
pulseaudio -k #should restart pulseaudio after you change settings.
For your script you may want to check that the available audio rates are compatible with your sink-device:
grep rates $(grep -ci hdmi /proc/asound/card*/codec#0|grep -v :0|sed 's,:.$,,'|head -n1)
and depending on which profile each card is attached to
pactl --server "unix:/run/user/$(id -u)/pulse/native" set-card-profile 1 output:analog-stereo
and
pactl --server "unix:/run/user/$(id -u)/pulse/native" set-card-profile 0 output:hdmi-stereo
may help create useful variables. It looks like someone has already tried to do what you are doing:
But some of their variables are either archlinux specific or out-of-date. (Though their path to triggering your script may be of help.)