Basic usage looks like this:
printf '%s\n' '" some commands...' 'wq' | ex -s file
Or: ex -s file <<'EOF'
" some commands...
wq
EOF
By the way, these commands are the ones that you use in your vimrc or after a colon in vim — at least, the POSIX subset of that — so any ex commands you learn translate naturally to your normal editor.